Skip to content

Commit

Permalink
feat. EnumArtist, feat. better String artist, feat. PHP 8.1 required now
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackbam committed Jun 23, 2022
1 parent e5f9b3f commit 8b7a299
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
@@ -1,3 +1,9 @@
= V 0.9.3 =

- Requires PHP 8.1 now
- Enum Artist
- Various minor improvements

= V 0.8.2 =

- Remove deprecations
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php": ">=8.0",
"php": ">=8.1",
"ext-curl": "*",
"ext-iconv": "*",
"ext-mbstring": "*",
Expand Down
47 changes: 47 additions & 0 deletions src/EnumArtist.php
@@ -0,0 +1,47 @@
<?php

namespace CisTools;

use BackedEnum;
use InvalidArgumentException;

class EnumArtist
{

/**
* Get all values from a backend enum.
* @param array $enums
* @return array
*/
public static function getBackedEnumNames(array $enums): array
{
$result = [];
foreach($enums as $enum) {
if(!($enum instanceof BackedEnum)) {
throw new InvalidArgumentException("You have to pass an array of backed enums.");
}
$result[] = $enum->name;
}
return $result;
}


/**
* Get all values from a backend enum.
*
* @param array $enums
* @return array
*/
public static function getBackedEnumValues(array $enums): array
{
$result = [];
foreach($enums as $enum) {
if(!($enum instanceof BackedEnum)) {
throw new InvalidArgumentException("You have to pass an array of backed enums.");
}
$result[] = $enum->value;
}
return $result;
}

}
32 changes: 31 additions & 1 deletion src/IterableArtist.php
Expand Up @@ -61,7 +61,7 @@ public static function flatten(array $array, int $maxDepth = -1): array
$result = [];
foreach ($array as $key => $value) {
if (is_array($value) && $maxDepth !== 0) {
$result = array_merge(...[$result, self::flatten($value, ($maxDepth > 0) ? $maxDepth - 1 : -1)]);
$result = [...$result, ...self::flatten($value, ($maxDepth > 0) ? $maxDepth - 1 : -1)];
} else {
$result[$key] = $value;
}
Expand Down Expand Up @@ -196,4 +196,34 @@ public static function toHtmlTable(array $twoDim, array $head = [], bool $useRow
return '<table>' . $thead . $tbody . '</table>';
}

/**
* Counts no matter what.
*
* @param mixed $whatever
* @return int: 0 if false, null or empty countable, 1 if whatever non countable item or if exactly one item, number of items in countable otherwise.
*/
public static function realCount(mixed $whatever): int
{
if ($whatever === false || is_null($whatever)){
return 0;
}
return is_countable($whatever) ? count($whatever) : 1;
}

/**
* Converts an object to a multidimensional array recursively. For non-array/non-object inputs simply the same value is returned.
*
* @param mixed $subject
* @return array
*/
public static function objectToArrayRecursive(mixed $subject): mixed
{
if(is_object($subject)) {
$subject = get_object_vars($subject);
}
if(is_array($subject)) {
return array_map([__CLASS__,__FUNCTION__],$subject);
}
return $subject;
}
}
15 changes: 15 additions & 0 deletions src/StringArtist.php
Expand Up @@ -308,4 +308,19 @@ public static function charTrimWrap(string $subject, string $wrap, int $times =
}
return $preparedWrap.trim($subject,$wrap).$preparedWrap;
}


/**
* Split a text string into its single lines.
*
* @param string $subject: A text with multiple lines (e.g. a validation file).
* @return array: The lines as array without line delimiter characters \r and \n
*/
public static function stringToLines(string $subject): array
{
return array_map(static function ($line) {
return preg_replace("/[\r\n]/", "", $line);
}, explode(PHP_EOL, $subject));
}

}

0 comments on commit 8b7a299

Please sign in to comment.