1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ipl\IteratorPipeline\Traits; |
6
|
|
|
|
7
|
|
|
use Ipl as i; |
8
|
|
|
use Ipl\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
|
2 |
|
public function map(callable $callback) |
32
|
|
|
{ |
33
|
2 |
|
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
|
2 |
|
public function apply(callable $callback) |
55
|
|
|
{ |
56
|
2 |
|
return $this->then(i\iterable_apply, $callback); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Group elements of an iterator, with the group name as key and an array of elements as value. |
61
|
|
|
* |
62
|
|
|
* @param callable $grouping |
63
|
|
|
* @return static |
64
|
|
|
*/ |
65
|
1 |
|
public function group(callable $grouping) |
66
|
|
|
{ |
67
|
1 |
|
return $this->then(i\iterable_group, $grouping); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Walk through all sub-iterables and concatenate them. |
72
|
|
|
* |
73
|
|
|
* @return static |
74
|
|
|
*/ |
75
|
1 |
|
public function flatten() |
76
|
|
|
{ |
77
|
1 |
|
return $this->then(i\iterable_flatten); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return the values from a single column / property. |
83
|
|
|
* Create key/value pairs by specifying the key. |
84
|
|
|
* |
85
|
|
|
* @param string|int|null $valueColumn null to keep values |
86
|
|
|
* @param string|int|null $keyColumn null to keep keys |
87
|
|
|
* @return static |
88
|
|
|
*/ |
89
|
2 |
|
public function column($valueColumn, $keyColumn = null) |
90
|
|
|
{ |
91
|
2 |
|
return $this->then(i\iterable_column, $valueColumn, $keyColumn); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Project each element of an iterator to an associated (or numeric) array. |
96
|
|
|
* Each element should be an array or object. |
97
|
|
|
* |
98
|
|
|
* @param array $mapping [new key => old key, ...] |
99
|
|
|
* @return static |
100
|
|
|
*/ |
101
|
1 |
|
public function project(array $mapping) |
102
|
|
|
{ |
103
|
1 |
|
return $this->then(i\iterable_project, $mapping); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Reshape each element of an iterator, adding or removing properties or keys. |
108
|
|
|
* |
109
|
|
|
* @param array $columns [key => bool|string|int, ...] |
110
|
|
|
* @return static |
111
|
|
|
*/ |
112
|
1 |
|
public function reshape(array $columns) |
113
|
|
|
{ |
114
|
1 |
|
return $this->then(i\iterable_reshape, $columns); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Keep the values, drop the keys. The keys become an incremental number. |
120
|
|
|
* |
121
|
|
|
* @return static |
122
|
|
|
*/ |
123
|
3 |
|
public function values() |
124
|
|
|
{ |
125
|
3 |
|
return $this->then(i\iterable_values); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Use the keys as values. The keys become an incremental number. |
130
|
|
|
* |
131
|
|
|
* @return static |
132
|
|
|
*/ |
133
|
1 |
|
public function keys() |
134
|
|
|
{ |
135
|
1 |
|
return $this->then(i\iterable_keys); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Use another iterator as keys and the current iterator as values. |
140
|
|
|
* |
141
|
|
|
* @param iterable $keys |
142
|
|
|
* @return static |
143
|
|
|
*/ |
144
|
4 |
|
public function setKeys(iterable $keys) |
145
|
|
|
{ |
146
|
|
|
$combine = function ($values, $keys) { |
147
|
4 |
|
return new CombineIterator($keys, $values); |
148
|
4 |
|
}; |
149
|
|
|
|
150
|
4 |
|
return $this->then($combine, $keys); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Use values as keys and visa versa. |
155
|
|
|
* |
156
|
|
|
* @return static |
157
|
|
|
*/ |
158
|
1 |
|
public function flip() |
159
|
|
|
{ |
160
|
1 |
|
return $this->then(i\iterable_flip); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|