/
Pluck.php
122 lines (109 loc) · 5.55 KB
/
Pluck.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
namespace loophp\collection\Operation;
use ArrayAccess;
use ArrayIterator;
use Closure;
use Generator;
use Iterator;
use loophp\collection\Contract\Collection;
use ReflectionClass;
use function array_key_exists;
use function in_array;
use function is_array;
use function is_object;
/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
* phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
*/
final class Pluck extends AbstractOperation
{
/**
* @psalm-return Closure(T): Closure(T): Closure(Iterator<TKey, T>): Generator<int, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param T $key
*
* @psalm-return Closure(T): Closure(Iterator<TKey, T>): Generator<int, T|iterable<int, T>, mixed, void>
*
* @param mixed $key
*/
static function ($key): Closure {
return
/**
* @psalm-param T $default
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, T|iterable<int, T>, mixed, void>
*
* @param mixed $default
*/
static function ($default) use ($key): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, T|iterable<int, T>, mixed, void>
*/
static function (Iterator $iterator) use ($key, $default): Generator {
$pick =
/**
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-param T|iterable<TKey, T> $target
* @psalm-param array<int, string> $key
* @psalm-param T $default
*
* @psalm-return T|iterable<int, T>
*
* @param mixed $target
* @param mixed|null $default
*/
static function (Iterator $iterator, $target, array $key, $default = null) use (&$pick) {
while (null !== $segment = array_shift($key)) {
if ('*' === $segment) {
if (false === is_iterable($target)) {
return $default;
}
/** @psalm-var array<int, T> $result */
$result = [];
foreach ($target as $item) {
$result[] = $pick($iterator, $item, $key);
}
/** @psalm-var Generator<TKey, T> $collapse */
$collapse = Collapse::of()(new ArrayIterator($result));
return in_array('*', $key, true) ? $collapse : $result;
}
if ((true === is_array($target)) && (true === array_key_exists($segment, $target))) {
/** @psalm-var T $target */
$target = $target[$segment];
} elseif (($target instanceof ArrayAccess) && (true === $target->offsetExists($segment))) {
/** @psalm-var T $target */
$target = $target[$segment];
} elseif ($target instanceof Collection) {
/** @psalm-var T $target */
$target = (Get::of()($segment)($default)($target->getIterator()))->current();
} elseif ((true === is_object($target)) && (true === property_exists($target, $segment))) {
/** @psalm-var T $target */
$target = (new ReflectionClass($target))->getProperty($segment)->getValue($target);
} else {
/** @psalm-var T $target */
$target = $default;
}
}
return $target;
};
$key = true === is_scalar($key) ? explode('.', trim((string) $key, '.')) : $key;
foreach ($iterator as $value) {
yield $pick($iterator, $value, $key, $default);
}
};
};
};
}
}