|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Zicht\Itertools\mappings; |
|
7
|
|
|
|
|
8
|
|
|
use Zicht\Itertools\util\Mappings; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Returns a closure that strips any matching $CHARS from the left of the input string |
|
12
|
|
|
* |
|
13
|
|
|
* @param string $chars |
|
14
|
|
|
* @return \Closure |
|
15
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::lstrip($chars), will be removed in version 3.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
function lstrip($chars = " \t\n\r\0\x0B") |
|
18
|
|
|
{ |
|
19
|
|
|
return Mappings::lstrip($chars); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Returns a closure that strips any matching $CHARS from the right of the input string |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $chars |
|
26
|
|
|
* @return \Closure |
|
27
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::lstrip($chars), will be removed in version 3.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
function rstrip($chars = " \t\n\r\0\x0B") |
|
30
|
|
|
{ |
|
31
|
|
|
return Mappings::rstrip($chars); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Returns a closure that strips any matching $CHARS from the left and right of the input string |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $chars |
|
38
|
|
|
* @return \Closure |
|
39
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::strip($chars), will be removed in version 3.0 |
|
40
|
|
|
*/ |
|
41
|
|
|
function strip($chars = " \t\n\r\0\x0B") |
|
42
|
|
|
{ |
|
43
|
|
|
return Mappings::strip($chars); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns a closure that returns the length of the input |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Closure |
|
50
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::length(), will be removed in version 3.0 |
|
51
|
|
|
*/ |
|
52
|
|
|
function length() |
|
53
|
|
|
{ |
|
54
|
|
|
return Mappings::length(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns a closure that returns the key |
|
59
|
|
|
* |
|
60
|
|
|
* @return \Closure |
|
61
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::key(), will be removed in version 3.0 |
|
62
|
|
|
*/ |
|
63
|
|
|
function key() |
|
64
|
|
|
{ |
|
65
|
|
|
return Mappings::key(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns a closure that returns the string value lower cased |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Closure |
|
72
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::lower(), will be removed in version 3.0 |
|
73
|
|
|
*/ |
|
74
|
|
|
function lower() |
|
75
|
|
|
{ |
|
76
|
|
|
return Mappings::lower(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns a closure that returns the string value upper cased |
|
81
|
|
|
* |
|
82
|
|
|
* @return \Closure |
|
83
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::upper(), will be removed in version 3.0 |
|
84
|
|
|
*/ |
|
85
|
|
|
function upper() |
|
86
|
|
|
{ |
|
87
|
|
|
return Mappings::upper(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns a closure that returns the value cast to a string |
|
92
|
|
|
* |
|
93
|
|
|
* @return \Closure |
|
94
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::string(), will be removed in version 3.0 |
|
95
|
|
|
*/ |
|
96
|
|
|
function string() |
|
97
|
|
|
{ |
|
98
|
|
|
return Mappings::string(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns a closure that returns the value as a json_encoded string |
|
103
|
|
|
* |
|
104
|
|
|
* @param int $options |
|
105
|
|
|
* @param int $depth |
|
106
|
|
|
* @return \Closure |
|
107
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::jsonEncode($options, $depth), will be removed in version 3.0 |
|
108
|
|
|
*/ |
|
109
|
|
|
function json_encode($options = 0, $depth = 512) |
|
110
|
|
|
{ |
|
111
|
|
|
return Mappings::jsonEncode($options, $depth); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns a closure that returns the json_encoded value as decoded value |
|
116
|
|
|
* |
|
117
|
|
|
* @param boolean $assoc |
|
118
|
|
|
* @param int $depth |
|
119
|
|
|
* @param int $options |
|
120
|
|
|
* @return \Closure |
|
121
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::jsonDecode($assoc, $options, $depth), will be removed in version 3.0 |
|
122
|
|
|
*/ |
|
123
|
|
|
function json_decode($assoc = false, $depth = 512, $options = 0) |
|
124
|
|
|
{ |
|
125
|
|
|
return Mappings::jsonDecode($assoc, $options, $depth); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Returns a closure that applies multiple $STRATEGIES to the value and returns the results |
|
130
|
|
|
* |
|
131
|
|
|
* > $compute = function ($value, $key) { |
|
132
|
|
|
* > return 'some computation result'; |
|
133
|
|
|
* > }; |
|
134
|
|
|
* > $list = iter\iterable([new Data(1), new Data(2), new Data(3)]); |
|
135
|
|
|
* > $list->map(select(['data' => null, 'id' => 'Identifier', 'desc' => 'Value.DescriptionName', 'comp' => $compute])); |
|
136
|
|
|
* [ |
|
137
|
|
|
* [ |
|
138
|
|
|
* 'data' => Data(1), |
|
139
|
|
|
* 'id' => Data(1)->Identifier, |
|
140
|
|
|
* 'desc' => Data(1)->Value->DescriptionName, |
|
141
|
|
|
* 'comp' => $compute(Data(1), 0), |
|
142
|
|
|
* ], |
|
143
|
|
|
* ... |
|
144
|
|
|
* [ |
|
145
|
|
|
* 'data' => Data(3), |
|
146
|
|
|
* 'id' => Data(3)->Identifier, |
|
147
|
|
|
* 'desc' => Data(3)->Value->DescriptionName, |
|
148
|
|
|
* 'comp' => $compute(Data(3), 2), |
|
149
|
|
|
* ], |
|
150
|
|
|
* ] |
|
151
|
|
|
* |
|
152
|
|
|
* @param array|object $mappings |
|
153
|
|
|
* @param null|string|\Closure $strategy |
|
154
|
|
|
* @param boolean $discardNull |
|
155
|
|
|
* @param boolean $discardEmptyContainer |
|
156
|
|
|
* @return \Closure |
|
157
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::select($mappings, $strategy, $discardNull, $discardEmptyContainer), will be removed in version 3.0 |
|
158
|
|
|
*/ |
|
159
|
|
|
function select($mappings, $strategy = null, $discardNull = false, $discardEmptyContainer = false) |
|
160
|
|
|
{ |
|
161
|
|
|
return Mappings::select($mappings, $strategy, $discardNull, $discardEmptyContainer); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns a closure that returns random integer numbers between $MIN and $MAX |
|
166
|
|
|
* |
|
167
|
|
|
* @param int $min |
|
168
|
|
|
* @param null|int $max |
|
169
|
|
|
* @return \Closure |
|
170
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::random($min, $max), will be removed in version 3.0 |
|
171
|
|
|
*/ |
|
172
|
|
|
function random($min = 0, $max = null) |
|
173
|
|
|
{ |
|
174
|
|
|
return Mappings::random($min, $max); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Returns a closure that returns either the class name, given an object, or otherwise the type |
|
179
|
|
|
* |
|
180
|
|
|
* @param null|string|\Closure $strategy |
|
181
|
|
|
* @return \Closure |
|
182
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::type($strategy), will be removed in version 3.0 |
|
183
|
|
|
*/ |
|
184
|
|
|
function type($strategy = null) |
|
185
|
|
|
{ |
|
186
|
|
|
return Mappings::type($strategy); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Returns a closure that calls the mapping on each element once. |
|
191
|
|
|
* |
|
192
|
|
|
* > $compute = function ($value, $key) { |
|
193
|
|
|
* > return 'some expensive computation result'; |
|
194
|
|
|
* > }; |
|
195
|
|
|
* > $list = iter\iterable([['id' => 42, ...], ['id' => 43, ...], ['id' => 42, ...]]); |
|
196
|
|
|
* > $list->map(cache($compute, 'id')); |
|
197
|
|
|
* [ |
|
198
|
|
|
* $compute(['id' => 42, ...]), // <-- calls the $compute method |
|
199
|
|
|
* $compute(['id' => 43, ...]), // <-- calls the $compute method |
|
200
|
|
|
* $compute(['id' => 42, ...]) // <-- does not call, instead, populates with cached values |
|
201
|
|
|
* ] |
|
202
|
|
|
* |
|
203
|
|
|
* @param null|string|\Closure $mapping |
|
204
|
|
|
* @param null|string|\Closure $strategy |
|
205
|
|
|
* @return \Closure |
|
206
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::cache($mapping, $strategy), will be removed in version 3.0 |
|
207
|
|
|
*/ |
|
208
|
|
|
function cache($mapping, $strategy = null) |
|
209
|
|
|
{ |
|
210
|
|
|
return Mappings::cache($mapping, $strategy); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Returns a closure that returns the same value every time it is called. |
|
215
|
|
|
* |
|
216
|
|
|
* @param null|string|int|float|bool|object|array $value |
|
217
|
|
|
* @return \Closure |
|
218
|
|
|
* @deprecated Use \Zicht\Itertools\util\Mappings::constant($value), will be removed in version 3.0 |
|
219
|
|
|
*/ |
|
220
|
|
|
function constant($value) |
|
221
|
|
|
{ |
|
222
|
|
|
return Mappings::constant($value); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Returns a mapping closure |
|
227
|
|
|
* |
|
228
|
|
|
* @param string $name |
|
229
|
|
|
* @return \Closure |
|
230
|
|
|
* @throws \InvalidArgumentException |
|
231
|
|
|
* @deprecated please use the mapping functions directly, will be removed in version 3.0 |
|
232
|
|
|
*/ |
|
233
|
|
|
function get_mapping($name /* [argument, [arguments, ...] */) |
|
234
|
|
|
{ |
|
235
|
|
|
if (is_string($name)) { |
|
236
|
|
|
switch ($name) { |
|
237
|
|
|
case 'ltrim': |
|
238
|
|
|
case 'lstrip': |
|
239
|
|
|
return call_user_func_array('\Zicht\Itertools\mappings\lstrip', array_slice(func_get_args(), 1)); |
|
240
|
|
|
|
|
241
|
|
|
case 'rtrim': |
|
242
|
|
|
case 'rstrip': |
|
243
|
|
|
return call_user_func_array('\Zicht\Itertools\mappings\rstrip', array_slice(func_get_args(), 1)); |
|
244
|
|
|
|
|
245
|
|
|
case 'trim': |
|
246
|
|
|
case 'strip': |
|
247
|
|
|
return call_user_func_array('\Zicht\Itertools\mappings\strip', array_slice(func_get_args(), 1)); |
|
248
|
|
|
|
|
249
|
|
|
case 'length': |
|
250
|
|
|
return length(); |
|
251
|
|
|
|
|
252
|
|
|
case 'key': |
|
253
|
|
|
return key(); |
|
254
|
|
|
|
|
255
|
|
|
case 'select': |
|
256
|
|
|
return call_user_func_array('\Zicht\Itertools\mappings\select', array_slice(func_get_args(), 1)); |
|
257
|
|
|
|
|
258
|
|
|
case 'random': |
|
259
|
|
|
return call_user_func_array('\Zicht\Itertools\mappings\random', array_slice(func_get_args(), 1)); |
|
260
|
|
|
|
|
261
|
|
|
case 'type': |
|
262
|
|
|
return call_user_func_array('\Zicht\Itertools\mappings\type', array_slice(func_get_args(), 1)); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
throw new \InvalidArgumentException(sprintf('$NAME "%s" is not a valid mapping.', $name)); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @param string $name |
|
271
|
|
|
* @return \Closure |
|
272
|
|
|
* @throws \InvalidArgumentException |
|
273
|
|
|
* @deprecated please use the mapping functions directly, will be removed in version 3.0 |
|
274
|
|
|
*/ |
|
275
|
|
|
function getMapping($name /* [argument, [arguments, ...] */) // phpcs:ignore Zicht.NamingConventions.Functions.GlobalNaming |
|
|
|
|
|
|
276
|
|
|
{ |
|
277
|
|
|
return call_user_func_array('\Zicht\Itertools\mappings\get_mapping', func_get_args()); |
|
278
|
|
|
} |
|
279
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.