loophp /
collection
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace loophp\collection\Operation; |
||
| 6 | |||
| 7 | use Closure; |
||
| 8 | use Generator; |
||
| 9 | |||
| 10 | use function in_array; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @immutable |
||
| 14 | * |
||
| 15 | * @template TKey |
||
| 16 | * @template T |
||
| 17 | */ |
||
| 18 | final class Compact extends AbstractOperation |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @return Closure(array<T>): Closure(iterable<TKey, T>): Generator<TKey, T> |
||
| 22 | */ |
||
| 23 | 12 | public function __invoke(): Closure |
|
| 24 | { |
||
| 25 | 12 | return |
|
| 26 | /** |
||
| 27 | * @param array<T> $values |
||
| 28 | * |
||
| 29 | * @return Closure(iterable<TKey, T>): Generator<TKey, T> |
||
| 30 | */ |
||
| 31 | 12 | static function (array $values): Closure { |
|
| 32 | 12 | $filterCallback = |
|
| 33 | /** |
||
| 34 | * @param non-empty-array<int, (null|array|int|bool|string|T)> $values |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 35 | */ |
||
| 36 | 12 | static fn (array $values): Closure => |
|
| 37 | /** |
||
| 38 | * @param T $value |
||
| 39 | */ |
||
| 40 | 12 | static fn (mixed $value): bool => !in_array($value, $values, true); |
|
| 41 | |||
| 42 | 12 | return (new Filter())()( |
|
| 43 | 12 | $filterCallback( |
|
| 44 | 12 | [] === $values ? |
|
| 45 | 10 | Nullsy::VALUES : |
|
| 46 | 12 | $values |
|
| 47 | 12 | ) |
|
| 48 | 12 | ); |
|
| 49 | 12 | }; |
|
| 50 | } |
||
| 51 | } |
||
| 52 |