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