1
|
|
|
<?php namespace Tarsana\Functional; |
2
|
|
|
/** |
3
|
|
|
* Useful functions to handle objects (associative arrays are considered objects). |
4
|
|
|
* @file |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Returns a deep copy of the given value. |
9
|
|
|
* |
10
|
|
|
* `Callable`s are not copied but returned by reference. |
11
|
|
|
* ```php |
12
|
|
|
* $data = (object) [ |
13
|
|
|
* 'content' => (object) ['name' => 'foo'], |
14
|
|
|
* 'other' => 'value' |
15
|
|
|
* ]; |
16
|
|
|
* |
17
|
|
|
* $clonedData = F\clone_($data); |
18
|
|
|
* $clonedData->content->name = 'bar'; |
19
|
|
|
* |
20
|
|
|
* $clonedData; //=> (object) ['content' => (object) ['name' => 'bar'], 'other' => 'value'] |
21
|
|
|
* $data; //=> (object) ['content' => (object) ['name' => 'foo'], 'other' => 'value'] |
22
|
|
|
* ``` |
23
|
|
|
* |
24
|
|
|
* @signature a -> a |
25
|
|
|
* @param mixed $value |
|
|
|
|
26
|
|
|
* @return mixed |
27
|
|
|
*/ |
28
|
|
|
function clone_() { |
29
|
|
|
static $clone = false; |
30
|
|
|
$clone = $clone ?: curry(function($value) use(&$clone) { |
31
|
|
|
switch (type($value)) { |
32
|
|
|
case 'Null': |
33
|
|
|
case 'Boolean': |
34
|
|
|
case 'String': |
35
|
|
|
case 'Function': |
36
|
|
|
case 'Resource': |
37
|
|
|
case 'Number': |
38
|
|
|
return $value; |
39
|
|
|
case 'ArrayObject': |
40
|
|
|
case 'Array': |
41
|
|
|
case 'List': |
42
|
|
|
return array_map($clone, $value); |
43
|
|
|
case 'Error': |
44
|
|
|
case 'Stream': |
45
|
|
|
case 'Object': |
46
|
|
|
$result = clone $value; |
47
|
|
|
foreach (keys($value) as $key) { |
48
|
|
|
$result->{$key} = $clone($result->{$key}); |
49
|
|
|
} |
50
|
|
|
return $result; |
51
|
|
|
} |
52
|
|
|
return $value; |
53
|
|
|
}); |
54
|
|
|
return _apply($clone, func_get_args()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Converts an object to an associative array containing public non-static attributes. |
59
|
|
|
* |
60
|
|
|
* If `$object` is not an object, it is returned unchanged. |
61
|
|
|
* |
62
|
|
|
* ```php |
63
|
|
|
* class AttributesTestClass { |
64
|
|
|
* private $a; |
65
|
|
|
* public $b = 1; |
66
|
|
|
* public $c; |
67
|
|
|
* private $d; |
68
|
|
|
* static $e; |
69
|
|
|
* } |
70
|
|
|
* |
71
|
|
|
* $test = new AttributesTestClass; |
72
|
|
|
* F\attributes($test); //=> ['b' => 1, 'c' => null] |
73
|
|
|
* ``` |
74
|
|
|
* |
75
|
|
|
* @stream |
76
|
|
|
* @signature {k: v} -> {k: v} |
77
|
|
|
* @param object|array $object |
|
|
|
|
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
function attributes() { |
81
|
|
|
static $attrs = false; |
82
|
|
|
$attrs = $attrs ?: curry(function($object) { |
83
|
|
|
if (is_object($object)) |
84
|
|
|
return get_object_vars($object); |
85
|
|
|
return $object; |
86
|
|
|
}); |
87
|
|
|
return _apply($attrs, func_get_args()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns a list of array's keys or object's public attributes names. |
92
|
|
|
* |
93
|
|
|
* ```php |
94
|
|
|
* F\keys([1, 2, 3]); //=> [0, 1, 2] |
95
|
|
|
* F\keys(['name' => 'foo', 'age' => 11]); //=> ['name', 'age'] |
96
|
|
|
* F\keys((object)['name' => 'foo', 'age' => 11]); //=> ['name', 'age'] |
97
|
|
|
* ``` |
98
|
|
|
* |
99
|
|
|
* @stream |
100
|
|
|
* @signature [*] -> [Number] |
101
|
|
|
* @signature {k: v} -> [k] |
102
|
|
|
* @param object|array $object |
|
|
|
|
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
function keys() { |
|
|
|
|
106
|
|
|
static $keys = false; |
107
|
|
|
$keys = $keys ?: curry(function($object) { |
108
|
|
|
return is_object($object) |
109
|
|
|
? array_keys(get_object_vars($object)) |
110
|
|
|
: array_keys($object); |
111
|
|
|
}); |
112
|
|
|
return _apply($keys, func_get_args()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns a list of array's values or object's public attributes values. |
117
|
|
|
* |
118
|
|
|
* ```php |
119
|
|
|
* F\values([1, 2, 3]); //=> [1, 2, 3] |
120
|
|
|
* F\values(['name' => 'foo', 'age' => 11]); //=> ['foo', 11] |
121
|
|
|
* F\values((object)['name' => 'foo', 'age' => 11]); //=> ['foo', 11] |
122
|
|
|
* ``` |
123
|
|
|
* |
124
|
|
|
* @stream |
125
|
|
|
* @signature [a] -> [a] |
126
|
|
|
* @signature {k: v} -> [v] |
127
|
|
|
* @param object|array $object |
|
|
|
|
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
function values() { |
|
|
|
|
131
|
|
|
static $values = false; |
132
|
|
|
$values = $values ?: curry(function($object) { |
133
|
|
|
return is_object($object) |
134
|
|
|
? array_values(get_object_vars($object)) |
135
|
|
|
: array_values($object); |
136
|
|
|
}); |
137
|
|
|
return _apply($values, func_get_args()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Checks if the given array or object has a specific key or public attribute. |
142
|
|
|
* |
143
|
|
|
* ```php |
144
|
|
|
* class HasTestClass { |
145
|
|
|
* public $a = 1; |
146
|
|
|
* private $b = 2; |
147
|
|
|
* protected $c = 3; |
148
|
|
|
* public $d; |
149
|
|
|
* } |
150
|
|
|
* $array = [ |
151
|
|
|
* 'type' => 'Array', |
152
|
|
|
* 'length' => 78 |
153
|
|
|
* ]; |
154
|
|
|
* $array[3] = 'three'; |
155
|
|
|
* $object = (object) ['name' => 'ok']; |
156
|
|
|
* |
157
|
|
|
* $hasName = F\has('name'); |
158
|
|
|
* |
159
|
|
|
* F\has('type', $array); //=> true |
160
|
|
|
* F\has(3, $array); //=> true |
161
|
|
|
* $hasName($array); //=> false |
162
|
|
|
* $hasName($object); //=> true |
163
|
|
|
* F\has('length', $object); //=> false |
164
|
|
|
* F\has('a', new HasTestClass); //=> true |
165
|
|
|
* F\has('b', new HasTestClass); //=> false |
166
|
|
|
* ``` |
167
|
|
|
* |
168
|
|
|
* @stream |
169
|
|
|
* @signature k -> {k: v} -> Boolean |
170
|
|
|
* @param string|int $name |
|
|
|
|
171
|
|
|
* @param mixed $object |
|
|
|
|
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
function has() { |
175
|
|
|
static $has = false; |
176
|
|
|
$has = $has ?: curry(function($name, $object){ |
177
|
|
|
if (is_object($object)) return isset($object->{$name}); |
178
|
|
|
if (is_array($object)) return isset($object[$name]); |
179
|
|
|
return false; |
180
|
|
|
}); |
181
|
|
|
return _apply($has, func_get_args()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Gets the value of a key from an array or the |
186
|
|
|
* value of an public attribute from an object. |
187
|
|
|
* |
188
|
|
|
* If the key/attribute is missing, `null` is returned. |
189
|
|
|
* ```php |
190
|
|
|
* $data = [ |
191
|
|
|
* ['name' => 'foo', 'type' => 'test'], |
192
|
|
|
* ['name' => 'bar', 'type' => 'test'], |
193
|
|
|
* (object) ['name' => 'baz'], |
194
|
|
|
* [1, 2, 3] |
195
|
|
|
* ]; |
196
|
|
|
* $nameOf = F\get('name'); |
197
|
|
|
* F\get(0, $data); //=> ['name' => 'foo', 'type' => 'test'] |
198
|
|
|
* $nameOf($data[1]); //=> 'bar' |
199
|
|
|
* $nameOf($data[2]); //=> 'baz' |
200
|
|
|
* $nameOf($data[3]); //=> null |
201
|
|
|
* ``` |
202
|
|
|
* |
203
|
|
|
* @stream |
204
|
|
|
* @signature k -> {k: v} -> Maybe(v) |
205
|
|
|
* @param string $name |
|
|
|
|
206
|
|
|
* @param array $object |
|
|
|
|
207
|
|
|
* @return mixed |
208
|
|
|
*/ |
209
|
|
|
function get() { |
210
|
|
|
static $get = false; |
211
|
|
|
$get = $get ?: curry(function($name, $object){ |
212
|
|
|
return is_object($object) |
213
|
|
|
? (isset($object->{$name}) ? $object->{$name} : null) |
214
|
|
|
: (isset($object[$name]) ? $object[$name] : null); |
215
|
|
|
}); |
216
|
|
|
return _apply($get, func_get_args()); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Gets a value from an array/object using a path of keys/attributes. |
221
|
|
|
* |
222
|
|
|
* ```php |
223
|
|
|
* $data = [ |
224
|
|
|
* ['name' => 'foo', 'type' => 'test'], |
225
|
|
|
* ['name' => 'bar', 'type' => 'test'], |
226
|
|
|
* (object) ['name' => 'baz', 'scores' => [1, 2, 3]] |
227
|
|
|
* ]; |
228
|
|
|
* $nameOfFirst = F\getPath([0, 'name']); |
229
|
|
|
* $nameOfFirst($data); //=> 'foo' |
230
|
|
|
* F\getPath([2, 'scores', 1], $data); //=> 2 |
231
|
|
|
* F\getPath([2, 'foo', 1], $data); //=> null |
232
|
|
|
* ``` |
233
|
|
|
* |
234
|
|
|
* @stream |
235
|
|
|
* @signature [k] -> {k: v} -> v |
236
|
|
|
* @param array $path |
|
|
|
|
237
|
|
|
* @param mixed $object |
|
|
|
|
238
|
|
|
* @return mixed |
239
|
|
|
*/ |
240
|
|
View Code Duplication |
function getPath() { |
|
|
|
|
241
|
|
|
static $getPath = false; |
242
|
|
|
$getPath = $getPath ?: curry(function($path, $object) { |
243
|
|
|
$result = $object; |
244
|
|
|
foreach ($path as &$attr) { |
245
|
|
|
$result = get($attr, $result); |
246
|
|
|
} |
247
|
|
|
return $result; |
248
|
|
|
}); |
249
|
|
|
return _apply($getPath, func_get_args()); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Returns a new array or object with the value of a key or a public attribute set |
254
|
|
|
* to a specific value. |
255
|
|
|
* |
256
|
|
|
* if the key/attribute is missing and `$object` is an `array` |
257
|
|
|
* or `stdClass`; the key/attribute is added. Otherwise `null` is returned. |
258
|
|
|
* ```php |
259
|
|
|
* $task = ['name' => 'test', 'complete' => false]; |
260
|
|
|
* $done = F\set('complete', true); |
261
|
|
|
* $done($task); //=> ['name' => 'test', 'complete' => true] |
262
|
|
|
* $done((object) $task); //=> (object) ['name' => 'test', 'complete' => true] |
263
|
|
|
* F\set('description', 'Some text here', $task); //=> ['name' => 'test', 'complete' => false, 'description' => 'Some text here'] |
264
|
|
|
* ``` |
265
|
|
|
* |
266
|
|
|
* @stream |
267
|
|
|
* @signature k -> v -> {k: v} -> {k: v} |
268
|
|
|
* @param string|int $name |
|
|
|
|
269
|
|
|
* @param mixed $value |
|
|
|
|
270
|
|
|
* @param mixed $object |
|
|
|
|
271
|
|
|
* @return mixed |
272
|
|
|
*/ |
273
|
|
|
function set() { |
274
|
|
|
static $set = false; |
275
|
|
|
$set = $set ?: curry(function($name, $value, $object) { |
276
|
|
|
if (is_object($object)) { |
277
|
|
|
$object = clone_($object); |
278
|
|
|
$object->{$name} = $value; |
279
|
|
|
} else |
280
|
|
|
$object[$name] = $value; |
281
|
|
|
return $object; |
282
|
|
|
}); |
283
|
|
|
return _apply($set, func_get_args()); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Updates the value of a key or public attribute using a callable. |
288
|
|
|
* |
289
|
|
|
* ```php |
290
|
|
|
* $person = [ |
291
|
|
|
* 'name' => 'foo', |
292
|
|
|
* 'age' => 11 |
293
|
|
|
* ]; |
294
|
|
|
* $growUp = F\update('age', F\plus(1)); |
295
|
|
|
* $growUp($person); //=> ['name' => 'foo', 'age' => 12] |
296
|
|
|
* // updating a missing attribute has no effect |
297
|
|
|
* F\update('wow', F\plus(1), $person); //=> ['name' => 'foo', 'age' => 11] |
298
|
|
|
* ``` |
299
|
|
|
* |
300
|
|
|
* @stream |
301
|
|
|
* @signature k -> (v -> v) -> {k: v} -> {k: v} |
302
|
|
|
* @param string|int $name |
|
|
|
|
303
|
|
|
* @param callable $fn |
|
|
|
|
304
|
|
|
* @param mixed $object |
|
|
|
|
305
|
|
|
* @return mixed |
306
|
|
|
*/ |
307
|
|
View Code Duplication |
function update() { |
|
|
|
|
308
|
|
|
static $update = false; |
309
|
|
|
$update = $update ?: curry(function($name, $fn, $object) { |
310
|
|
|
$value = get($name, $object); |
311
|
|
|
return (null === $value) ? $object : set($name, $fn($value), $object); |
|
|
|
|
312
|
|
|
}); |
313
|
|
|
return _apply($update, func_get_args()); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Checks if an attribute/value of an object/array passes the given predicate. |
318
|
|
|
* |
319
|
|
|
* ```php |
320
|
|
|
* $foo = ['name' => 'foo', 'age' => 11]; |
321
|
|
|
* $isAdult = F\satisfies(F\lte(18), 'age'); |
322
|
|
|
* F\satisfies(F\startsWith('f'), 'name', $foo); //=> true |
323
|
|
|
* F\satisfies(F\startsWith('g'), 'name', $foo); //=> false |
324
|
|
|
* F\satisfies(F\startsWith('g'), 'friends', $foo); //=> false |
325
|
|
|
* $isAdult($foo); //=> false |
326
|
|
|
* ``` |
327
|
|
|
* |
328
|
|
|
* @stream |
329
|
|
|
* @signature (a -> Boolean) -> k -> {k : a} -> Boolean |
330
|
|
|
* @param callable $predicate |
|
|
|
|
331
|
|
|
* @param string|int $key |
|
|
|
|
332
|
|
|
* @param mixed $object |
|
|
|
|
333
|
|
|
* @return bool |
334
|
|
|
*/ |
335
|
|
|
function satisfies() { |
336
|
|
|
static $satisfies = false; |
337
|
|
|
$satisfies = $satisfies ?: curry(function($predicate, $key, $object) { |
338
|
|
|
return has($key, $object) && $predicate(get($key, $object)); |
339
|
|
|
}); |
340
|
|
|
return _apply($satisfies, func_get_args()); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Checks if a list of attribute/value of an object/array passes all the given predicates. |
345
|
|
|
* |
346
|
|
|
* ```php |
347
|
|
|
* $persons = [ |
348
|
|
|
* ['name' => 'foo', 'age' => 11], |
349
|
|
|
* ['name' => 'bar', 'age' => 9], |
350
|
|
|
* ['name' => 'baz', 'age' => 16], |
351
|
|
|
* ['name' => 'zeta', 'age' => 33], |
352
|
|
|
* ['name' => 'beta', 'age' => 25] |
353
|
|
|
* ]; |
354
|
|
|
* |
355
|
|
|
* $isValid = F\satisfiesAll([ |
356
|
|
|
* 'name' => F\startsWith('b'), |
357
|
|
|
* 'age' => F\lte(15) |
358
|
|
|
* ]); |
359
|
|
|
* |
360
|
|
|
* F\filter($isValid, $persons); //=> [['name' => 'baz', 'age' => 16], ['name' => 'beta', 'age' => 25]] |
361
|
|
|
* ``` |
362
|
|
|
* |
363
|
|
|
* @stream |
364
|
|
|
* @signature {String: (a -> Boolean)} -> {k : a} -> Boolean |
365
|
|
|
* @param array $predicates |
|
|
|
|
366
|
|
|
* @param mixed $object |
|
|
|
|
367
|
|
|
* @return bool |
368
|
|
|
*/ |
369
|
|
View Code Duplication |
function satisfiesAll() { |
|
|
|
|
370
|
|
|
static $satisfiesAll = false; |
371
|
|
|
$satisfiesAll = $satisfiesAll ?: curry(function($predicates, $object) { |
372
|
|
|
foreach ($predicates as $key => $predicate) { |
373
|
|
|
if (!satisfies($predicate, $key, $object)) |
374
|
|
|
return false; |
375
|
|
|
} |
376
|
|
|
return true; |
377
|
|
|
}); |
378
|
|
|
return _apply($satisfiesAll, func_get_args()); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Checks if a list of attribute/value of an object/array passes any of the given predicates. |
383
|
|
|
* |
384
|
|
|
* ```php |
385
|
|
|
* $persons = [ |
386
|
|
|
* ['name' => 'foo', 'age' => 11], |
387
|
|
|
* ['name' => 'bar', 'age' => 9], |
388
|
|
|
* ['name' => 'baz', 'age' => 16], |
389
|
|
|
* ['name' => 'zeta', 'age' => 33], |
390
|
|
|
* ['name' => 'beta', 'age' => 25] |
391
|
|
|
* ]; |
392
|
|
|
* |
393
|
|
|
* $isValid = F\satisfiesAny([ |
394
|
|
|
* 'name' => F\startsWith('b'), |
395
|
|
|
* 'age' => F\lte(15) |
396
|
|
|
* ]); |
397
|
|
|
* |
398
|
|
|
* F\filter($isValid, $persons); //=> [['name' => 'bar', 'age' => 9], ['name' => 'baz', 'age' => 16], ['name' => 'zeta', 'age' => 33], ['name' => 'beta', 'age' => 25]] |
399
|
|
|
* ``` |
400
|
|
|
* |
401
|
|
|
* @stream |
402
|
|
|
* @signature {String: (a -> Boolean)} -> {k : a} -> Boolean |
403
|
|
|
* @param array $predicates |
|
|
|
|
404
|
|
|
* @param mixed $object |
|
|
|
|
405
|
|
|
* @return bool |
406
|
|
|
*/ |
407
|
|
View Code Duplication |
function satisfiesAny() { |
|
|
|
|
408
|
|
|
static $satisfiesAny = false; |
409
|
|
|
$satisfiesAny = $satisfiesAny ?: curry(function($predicates, $object) { |
410
|
|
|
foreach ($predicates as $key => $predicate) { |
411
|
|
|
if (satisfies($predicate, $key, $object)) |
412
|
|
|
return true; |
413
|
|
|
} |
414
|
|
|
return false; |
415
|
|
|
}); |
416
|
|
|
return _apply($satisfiesAny, func_get_args()); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Converts an object or associative array to an array of [key, value] pairs. |
421
|
|
|
* |
422
|
|
|
* ```php |
423
|
|
|
* $list = ['key' => 'value', 'number' => 53, 'foo', 'bar']; |
424
|
|
|
* F\toPairs($list); //=> [['key', 'value'], ['number', 53], [0, 'foo'], [1, 'bar']] |
425
|
|
|
* ``` |
426
|
|
|
* |
427
|
|
|
* @stream |
428
|
|
|
* @signature {k: v} -> [(k,v)] |
429
|
|
|
* @signature [v] -> [(Number,v)] |
430
|
|
|
* @param array $object |
|
|
|
|
431
|
|
|
* @return array |
432
|
|
|
*/ |
433
|
|
|
function toPairs() { |
434
|
|
|
static $toPairs = false; |
435
|
|
|
$toPairs = $toPairs ?: curry(function($object) { |
436
|
|
|
if (is_object($object)) |
437
|
|
|
$object = get_object_vars($object); |
438
|
|
|
$result = []; |
439
|
|
|
foreach ($object as $key => $value) { |
440
|
|
|
$result[] = [$key, $value]; |
441
|
|
|
} |
442
|
|
|
return $result; |
443
|
|
|
}); |
444
|
|
|
return _apply($toPairs, func_get_args()); |
445
|
|
|
} |
446
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.