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
|
|
|
* Gets the value of a key from an array or the |
8
|
|
|
* value of a public attribute from an object. |
9
|
|
|
* ```php |
10
|
|
|
* $data = [ |
11
|
|
|
* ['name' => 'foo', 'type' => 'test'], |
12
|
|
|
* ['name' => 'bar', 'type' => 'test'], |
13
|
|
|
* (object) ['name' => 'baz'] |
14
|
|
|
* ]; |
15
|
|
|
* $nameOf = value('name'); |
16
|
|
|
* value(0, $data) // ['name' => 'foo', 'type' => 'test'] |
17
|
|
|
* $nameOf($data[1]) // 'bar' |
18
|
|
|
* $nameOf($data[2]) // 'baz' |
19
|
|
|
* ``` |
20
|
|
|
* |
21
|
|
|
* @signature String|Number -> [key => *] -> * |
22
|
|
|
* @param string $name |
|
|
|
|
23
|
|
|
* @param array $array |
|
|
|
|
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
function value() { |
|
|
|
|
27
|
|
|
$value = function($name, $array){ |
28
|
|
|
return ('Object' == type($array)) |
29
|
|
|
? $array->{$name} |
30
|
|
|
: $array[$name]; |
31
|
|
|
}; |
32
|
|
|
return apply(curry($value), func_get_args()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Checks if the given array or object has a specific key or public attribute. |
37
|
|
|
* ```php |
38
|
|
|
* $array = [ |
39
|
|
|
* 'type' => 'Array', |
40
|
|
|
* 'length' => 78 |
41
|
|
|
* ]; |
42
|
|
|
* $array[3] = 'three'; |
43
|
|
|
* $object = (object) ['name' => 'ok']; |
44
|
|
|
* |
45
|
|
|
* $hasName = F\has('name'); |
46
|
|
|
* |
47
|
|
|
* F\has('type', $array); // true |
48
|
|
|
* F\has(3, $array); // true |
49
|
|
|
* $hasName($array); // false |
50
|
|
|
* $hasName($object); // true |
51
|
|
|
* F\has('length', $object); // false |
52
|
|
|
* ``` |
53
|
|
|
* |
54
|
|
|
* @signature String|Number -> [key => *] -> Boolean |
55
|
|
|
* @param string $name |
|
|
|
|
56
|
|
|
* @param array $array |
|
|
|
|
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
function has() { |
|
|
|
|
60
|
|
|
$has = function($name, $array){ |
61
|
|
|
return ('Object' == type($array)) |
62
|
|
|
? isset($array->{$name}) |
63
|
|
|
: isset($array[$name]); |
64
|
|
|
}; |
65
|
|
|
return apply(curry($has), func_get_args()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Checks if an attribute or value of an object or |
70
|
|
|
* associative array passes the given predicate. |
71
|
|
|
* ```php |
72
|
|
|
* $data = [ |
73
|
|
|
* 'name' => '' |
74
|
|
|
* ] |
75
|
|
|
* ``` |
76
|
|
|
*/ |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Converts an associative array to an array of [key, value] pairs. |
80
|
|
|
* ```php |
81
|
|
|
* $list = ['key' => 'value', 'number' => 53, 'foo', 'bar']; |
82
|
|
|
* toPairs($list); // [['key', 'value'], ['number', 53], [0, 'foo'], [1, 'bar']] |
83
|
|
|
* ``` |
84
|
|
|
* |
85
|
|
|
* @signature [a => b] -> [(a,b)] |
86
|
|
|
* @param array $list |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
function toPairs($list) { |
90
|
|
|
return map(function($key) use($list) { |
91
|
|
|
return [$key, $list[$key]]; |
92
|
|
|
}, array_keys($list)); |
93
|
|
|
} |
94
|
|
|
|
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.