| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Stdlib; |
| 4 |
|
| 5 |
use Generator; |
| 6 |
use IteratorIterator; |
| 7 |
use stdClass; |
| 8 |
use Traversable; |
| 9 |
|
| 10 |
/** |
| 11 |
* Detect and return the PHP type of the given subject |
| 12 |
* |
| 13 |
* If subject is an object, the name of the object's class is returned, otherwise the subject's type. |
| 14 |
* |
| 15 |
* @param mixed $subject |
| 16 |
* |
| 17 |
* @return string |
| 18 |
*/ |
| 19 |
function get_php_type(mixed $subject): string |
| 20 |
{ |
| 21 |
return is_object($subject) ? get_class($subject) : gettype($subject); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get the array value of the given subject |
| 26 |
* |
| 27 |
* @param iterable|stdClass $subject |
| 28 |
* |
| 29 |
* @return array |
| 30 |
*/ |
| 31 |
function arrayval(iterable|stdClass $subject): array |
| 32 |
{ |
| 33 |
if (is_array($subject)) { |
| 34 |
return $subject; |
| 35 |
} |
| 36 |
|
| 37 |
if ($subject instanceof stdClass) { |
| 38 |
return (array) $subject; |
| 39 |
} |
| 40 |
|
| 41 |
// Also works for generators. |
| 42 |
return iterator_to_array($subject); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get the first key of an iterable |
| 47 |
* |
| 48 |
* @param iterable $iterable |
| 49 |
* |
| 50 |
* @return int|string|null The first key of the iterable if it is not empty, null otherwise |
| 51 |
*/ |
| 52 |
function iterable_key_first(iterable $iterable): int|string|null |
| 53 |
{ |
| 54 |
foreach ($iterable as $key => $_) { |
| 55 |
return $key; |
| 56 |
} |
| 57 |
|
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get the first value of an iterable |
| 63 |
* |
| 64 |
* @param iterable $iterable |
| 65 |
* |
| 66 |
* @return mixed|null The first value of the iterable if it is not empty, null otherwise |
| 67 |
*/ |
| 68 |
function iterable_value_first(iterable $iterable): mixed |
| 69 |
{ |
| 70 |
foreach ($iterable as $value) { |
| 71 |
return $value; |
| 72 |
} |
| 73 |
|
| 74 |
return null; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Yield sets of items from a sorted traversable grouped by a specific criterion gathered from a callback |
| 79 |
* |
| 80 |
* The traversable must be sorted by the criterion. The callback must return at least the criterion, |
| 81 |
* but can also return value and key in addition. |
| 82 |
* |
| 83 |
* @param Traversable<mixed, mixed> $traversable |
| 84 |
* @param callable(mixed $value, mixed $key): array{0: mixed, 1?: mixed, 2?: mixed} $groupBy |
| 85 |
* |
| 86 |
* @return Generator<mixed, array> |
| 87 |
*/ |
| 88 |
function yield_groups(Traversable $traversable, callable $groupBy): Generator |
| 89 |
{ |
| 90 |
$iterator = new IteratorIterator($traversable); |
| 91 |
$iterator->rewind(); |
| 92 |
|
| 93 |
if (! $iterator->valid()) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
[$criterion, $v, $k] = array_pad((array) $groupBy($iterator->current(), $iterator->key()), 3, null); |
| 98 |
$group = [$k ?? $iterator->key() => $v ?? $iterator->current()]; |
| 99 |
|
| 100 |
$iterator->next(); |
| 101 |
for (; $iterator->valid(); $iterator->next()) { |
| 102 |
[$c, $v, $k] = array_pad((array) $groupBy($iterator->current(), $iterator->key()), 3, null); |
| 103 |
if ($c !== $criterion) { |
| 104 |
yield $criterion => $group; |
| 105 |
|
| 106 |
$group = []; |
| 107 |
$criterion = $c; |
| 108 |
} |
| 109 |
|
| 110 |
$group[$k ?? $iterator->key()] = $v ?? $iterator->current(); |
| 111 |
} |
| 112 |
|
| 113 |
yield $criterion => $group; |
| 114 |
} |