|
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(i\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(i\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(i\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(i\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(i\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(i\iterable_flatten, $preserveKeys); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Set all values of the iterable. |
|
95
|
|
|
* |
|
96
|
|
|
* @param mixed $value |
|
97
|
|
|
* @return static |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function fill($value) |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $this->then(i\iterable_fill, $value); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Return the values from a single column / property. |
|
106
|
|
|
* Create key/value pairs by specifying the key. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string|int|null $valueColumn null to keep values |
|
109
|
|
|
* @param string|int|null $keyColumn null to keep keys |
|
110
|
|
|
* @return static |
|
111
|
|
|
*/ |
|
112
|
2 |
|
public function column($valueColumn, $keyColumn = null) |
|
113
|
|
|
{ |
|
114
|
2 |
|
return $this->then(i\iterable_column, $valueColumn, $keyColumn); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Project each element of an iterator to an associated (or numeric) array. |
|
119
|
|
|
* Each element should be an array or object. |
|
120
|
|
|
* |
|
121
|
|
|
* @param array $mapping [new key => old key, ...] |
|
122
|
|
|
* @return static |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public function project(array $mapping) |
|
125
|
|
|
{ |
|
126
|
1 |
|
return $this->then(i\iterable_project, $mapping); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Reshape each element of an iterator, adding or removing properties or keys. |
|
131
|
|
|
* |
|
132
|
|
|
* @param array $columns [key => bool|string|int, ...] |
|
133
|
|
|
* @return static |
|
134
|
|
|
*/ |
|
135
|
1 |
|
public function reshape(array $columns) |
|
136
|
|
|
{ |
|
137
|
1 |
|
return $this->then(i\iterable_reshape, $columns); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Keep the values, drop the keys. The keys become an incremental number. |
|
143
|
|
|
* |
|
144
|
|
|
* @return static |
|
145
|
|
|
*/ |
|
146
|
8 |
|
public function values() |
|
147
|
|
|
{ |
|
148
|
8 |
|
return $this->then(i\iterable_values); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Use the keys as values. The keys become an incremental number. |
|
153
|
|
|
* |
|
154
|
|
|
* @return static |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public function keys() |
|
157
|
|
|
{ |
|
158
|
1 |
|
return $this->then(i\iterable_keys); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Use another iterator as keys and the current iterator as values. |
|
163
|
|
|
* |
|
164
|
|
|
* @param iterable $keys |
|
165
|
|
|
* @return static |
|
166
|
|
|
*/ |
|
167
|
4 |
|
public function setKeys(iterable $keys) |
|
168
|
|
|
{ |
|
169
|
|
|
$combine = function ($values, $keys) { |
|
170
|
4 |
|
return new CombineIterator($keys, $values); |
|
171
|
4 |
|
}; |
|
172
|
|
|
|
|
173
|
4 |
|
return $this->then($combine, $keys); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Use values as keys and visa versa. |
|
178
|
|
|
* |
|
179
|
|
|
* @return static |
|
180
|
|
|
*/ |
|
181
|
1 |
|
public function flip() |
|
182
|
|
|
{ |
|
183
|
1 |
|
return $this->then(i\iterable_flip); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|