|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Improved\IteratorPipeline\Traits; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Filtering methods for iterator pipeline. |
|
9
|
|
|
*/ |
|
10
|
|
|
trait FilteringTrait |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Define the next step via a callback that returns an array or Traversable object. |
|
14
|
|
|
* |
|
15
|
|
|
* @param callable $callback |
|
16
|
|
|
* @param mixed ...$args |
|
17
|
|
|
* @return static |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract public function then(callable $callback, ...$args); |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Eliminate elements based on a criteria. |
|
24
|
|
|
* |
|
25
|
|
|
* @param callable $matcher |
|
26
|
|
|
* @return static |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function filter(callable $matcher) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
1 |
|
return $this->then("Improved\iterable_filter", $matcher); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Filter out `null` values from iteratable. |
|
35
|
|
|
* |
|
36
|
|
|
* @return static |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function cleanup() |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
1 |
|
return $this->then("Improved\iterable_cleanup"); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Filter on unique elements. |
|
45
|
|
|
* |
|
46
|
|
|
* @param callable|null $grouper If provided, filtering will be based on return value. |
|
47
|
|
|
* @return static |
|
48
|
|
|
*/ |
|
49
|
6 |
|
public function unique(?callable $grouper = null) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
6 |
|
return $this->then("Improved\iterable_unique", $grouper); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Filter our duplicate keys. |
|
56
|
|
|
* Unlike associative arrays, the keys of iterators don't have to be unique. |
|
57
|
|
|
* |
|
58
|
|
|
* @return static |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function uniqueKeys() |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->then("Improved\iterable_unique", function ($value, $key) { |
|
|
|
|
|
|
63
|
1 |
|
return $key; |
|
64
|
1 |
|
}); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get only the first elements of an iterator. |
|
69
|
|
|
* |
|
70
|
|
|
* @param int $size |
|
71
|
|
|
* @return static |
|
72
|
|
|
*/ |
|
73
|
3 |
|
public function limit(int $size) |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
3 |
|
return $this->then("Improved\iterable_slice", 0, $size); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get a limited subset of the elements using an offset. |
|
80
|
|
|
* |
|
81
|
|
|
* @param int $offset |
|
82
|
|
|
* @param int|null $size size limit |
|
83
|
|
|
* @return static |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function slice(int $offset, ?int $size = null) |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
1 |
|
return $this->then("Improved\iterable_slice", $offset, $size); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get elements until a match is found. |
|
92
|
|
|
* |
|
93
|
|
|
* @param callable $matcher |
|
94
|
|
|
* @param bool $include |
|
95
|
|
|
* @return static |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function before(callable $matcher, bool $include = false) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
2 |
|
return $this->then("Improved\iterable_before", $matcher, $include); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get elements after a match is found. |
|
104
|
|
|
* |
|
105
|
|
|
* @param callable $matcher |
|
106
|
|
|
* @param bool $include |
|
107
|
|
|
* @return static |
|
108
|
|
|
*/ |
|
109
|
2 |
|
public function after(callable $matcher, bool $include = false) |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
2 |
|
return $this->then("Improved\iterable_after", $matcher, $include); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|