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