1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Improved\IteratorPipeline\Traits; |
6
|
|
|
|
7
|
|
|
use Improved as i; |
8
|
|
|
use Improved\Iterator\CombineIterator; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Mapping and projection methods for iterator pipeline |
12
|
|
|
*/ |
13
|
|
|
trait MappingTrait |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Define the next step via a callback that returns an array or Traversable object. |
17
|
|
|
* |
18
|
|
|
* @param callable $callback |
19
|
|
|
* @param mixed ...$args |
20
|
|
|
* @return static |
21
|
|
|
*/ |
22
|
|
|
abstract public function then(callable $callback, ...$args); |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Map each element to a value using a callback function. |
27
|
|
|
* |
28
|
|
|
* @param callable $callback |
29
|
|
|
* @return static |
30
|
|
|
*/ |
31
|
3 |
|
public function map(callable $callback) |
|
|
|
|
32
|
|
|
{ |
33
|
3 |
|
return $this->then("Improved\iterable_map", $callback); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Map the key of each element to a new key using a callback function. |
38
|
|
|
* |
39
|
|
|
* @param callable $callback |
40
|
|
|
* @return static |
41
|
|
|
*/ |
42
|
1 |
|
public function mapKeys(callable $callback) |
|
|
|
|
43
|
|
|
{ |
44
|
1 |
|
return $this->then("Improved\iterable_map_keys", $callback); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Apply a callback to each element of an iterator. |
49
|
|
|
* Any value returned by the callback is ignored. |
50
|
|
|
* |
51
|
|
|
* @param callable $callback |
52
|
|
|
* @return static |
53
|
|
|
*/ |
54
|
3 |
|
public function apply(callable $callback) |
|
|
|
|
55
|
|
|
{ |
56
|
3 |
|
return $this->then("Improved\iterable_apply", $callback); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Divide iterable into chunks of specified size. |
61
|
|
|
* |
62
|
|
|
* @param int $size |
63
|
|
|
* @return static |
64
|
|
|
*/ |
65
|
1 |
|
public function chunk(int $size) |
|
|
|
|
66
|
|
|
{ |
67
|
1 |
|
return $this->then("Improved\iterable_chunk", $size); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Group elements of an iterator, with the group name as key and an array of elements as value. |
72
|
|
|
* |
73
|
|
|
* @param callable $grouping |
74
|
|
|
* @return static |
75
|
|
|
*/ |
76
|
1 |
|
public function group(callable $grouping) |
|
|
|
|
77
|
|
|
{ |
78
|
1 |
|
return $this->then("Improved\iterable_group", $grouping); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Walk through all sub-iterables and concatenate them. |
83
|
|
|
* |
84
|
|
|
* @param bool $preserveKeys |
85
|
|
|
* @return static |
86
|
|
|
*/ |
87
|
2 |
|
public function flatten(bool $preserveKeys = false) |
|
|
|
|
88
|
|
|
{ |
89
|
2 |
|
return $this->then("Improved\iterable_flatten", $preserveKeys); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Deconstruct an iterable property/item for each element. The result is one element for each item in the iterable |
94
|
|
|
* property. |
95
|
|
|
* |
96
|
|
|
* @param string $column |
97
|
|
|
* @param string|null $mapKey The name of a new property to hold the array index of the element |
98
|
|
|
* @param bool $preserveKeys Preserve the keys of the iterable (will result in duplicate keys) |
99
|
|
|
* @return static |
100
|
|
|
*/ |
101
|
1 |
|
public function unwind(string $column, ?string $mapKey = null, bool $preserveKeys = false) |
|
|
|
|
102
|
|
|
{ |
103
|
1 |
|
return $this->then("Improved\iterable_unwind", $column, $mapKey, $preserveKeys); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set all values of the iterable. |
108
|
|
|
* |
109
|
|
|
* @param mixed $value |
110
|
|
|
* @return static |
111
|
|
|
*/ |
112
|
1 |
|
public function fill($value) |
|
|
|
|
113
|
|
|
{ |
114
|
1 |
|
return $this->then("Improved\iterable_fill", $value); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Return the values from a single column / property. |
119
|
|
|
* Create key/value pairs by specifying the key. |
120
|
|
|
* |
121
|
|
|
* @param string|int|null $valueColumn null to keep values |
122
|
|
|
* @param string|int|null $keyColumn null to keep keys |
123
|
|
|
* @return static |
124
|
|
|
*/ |
125
|
2 |
|
public function column($valueColumn, $keyColumn = null) |
|
|
|
|
126
|
|
|
{ |
127
|
2 |
|
return $this->then("Improved\iterable_column", $valueColumn, $keyColumn); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Project each element of an iterator to an associated (or numeric) array. |
132
|
|
|
* Each element should be an array or object. |
133
|
|
|
* |
134
|
|
|
* @param array $mapping [new key => old key, ...] |
135
|
|
|
* @return static |
136
|
|
|
*/ |
137
|
1 |
|
public function project(array $mapping) |
|
|
|
|
138
|
|
|
{ |
139
|
1 |
|
return $this->then("Improved\iterable_project", $mapping); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Reshape each element of an iterator, adding or removing properties or keys. |
144
|
|
|
* |
145
|
|
|
* @param array $columns [key => bool|string|int, ...] |
146
|
|
|
* @return static |
147
|
|
|
*/ |
148
|
1 |
|
public function reshape(array $columns) |
|
|
|
|
149
|
|
|
{ |
150
|
1 |
|
return $this->then("Improved\iterable_reshape", $columns); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Keep the values, drop the keys. The keys become an incremental number. |
156
|
|
|
* |
157
|
|
|
* @return static |
158
|
|
|
*/ |
159
|
8 |
|
public function values() |
|
|
|
|
160
|
|
|
{ |
161
|
8 |
|
return $this->then("Improved\iterable_values"); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Use the keys as values. The keys become an incremental number. |
166
|
|
|
* |
167
|
|
|
* @return static |
168
|
|
|
*/ |
169
|
1 |
|
public function keys() |
|
|
|
|
170
|
|
|
{ |
171
|
1 |
|
return $this->then("Improved\iterable_keys"); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Use another iterator as keys and the current iterator as values. |
176
|
|
|
* |
177
|
|
|
* @param iterable $keys |
178
|
|
|
* @return static |
179
|
|
|
*/ |
180
|
4 |
|
public function setKeys(iterable $keys) |
|
|
|
|
181
|
|
|
{ |
182
|
4 |
|
$combine = function ($values, $keys) { |
|
|
|
|
183
|
4 |
|
return new CombineIterator($keys, $values); |
184
|
4 |
|
}; |
185
|
|
|
|
186
|
4 |
|
return $this->then($combine, $keys); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Use values as keys and visa versa. |
191
|
|
|
* |
192
|
|
|
* @return static |
193
|
|
|
*/ |
194
|
1 |
|
public function flip() |
|
|
|
|
195
|
|
|
{ |
196
|
1 |
|
return $this->then("Improved\iterable_flip"); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|