1
|
|
|
<?php namespace Tarsana\Functional; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file contains operators as functions. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Returns `$a && $b`. |
9
|
|
|
* |
10
|
|
|
* @signature Boolean -> Boolean -> Boolean |
11
|
|
|
* @param bool $a |
|
|
|
|
12
|
|
|
* @param bool $b |
|
|
|
|
13
|
|
|
* @return bool |
14
|
|
|
*/ |
15
|
|
|
function and_() { |
16
|
|
|
return apply(curry(function($a, $b){ |
17
|
|
|
return $a && $b; |
18
|
|
|
}), func_get_args()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns `$a || $b`. |
23
|
|
|
* |
24
|
|
|
* @signature Boolean -> Boolean -> Boolean |
25
|
|
|
* @param bool $a |
|
|
|
|
26
|
|
|
* @param bool $b |
|
|
|
|
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
|
|
function or_() { |
30
|
|
|
return apply(curry(function($a, $b){ |
31
|
|
|
return $a || $b; |
32
|
|
|
}), func_get_args()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns `!$x`. |
37
|
|
|
* |
38
|
|
|
* @signature Boolean -> Boolean |
39
|
|
|
* @param bool $x |
|
|
|
|
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
function not() { |
43
|
|
|
$not = function($x) { |
44
|
|
|
return !$x; |
45
|
|
|
}; |
46
|
|
|
return apply(curry($not), func_get_args()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns `$x == $y`. |
51
|
|
|
* |
52
|
|
|
* @signature * -> * -> Boolean |
53
|
|
|
* @param mixed $a |
|
|
|
|
54
|
|
|
* @param mixed $b |
|
|
|
|
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
function eq() { |
58
|
|
|
return apply(curry(function($a, $b){ |
59
|
|
|
return $a == $b; |
60
|
|
|
}), func_get_args()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns `$x === $y`. |
65
|
|
|
* |
66
|
|
|
* @signature * -> * -> Boolean |
67
|
|
|
* @param mixed $a |
|
|
|
|
68
|
|
|
* @param mixed $b |
|
|
|
|
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
function eqq() { |
72
|
|
|
return apply(curry(function($a, $b){ |
73
|
|
|
return $a === $b; |
74
|
|
|
}), func_get_args()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns `true` if the two elements have the same type and are deeply equivalent. |
79
|
|
|
* ```php |
80
|
|
|
* $a = (object) ['a' => 1, 'b' => (object) ['c' => 'Hello'], 'd' => false]; |
81
|
|
|
* $b = (object) ['a' => 1, 'b' => (object) ['c' => 'Hi'], 'd' => false]; |
82
|
|
|
* $c = (object) ['a' => 1, 'b' => ['c' => 'Hello'], 'd' => false]; |
83
|
|
|
* equals(5, '5'); // false (should have the same type) |
84
|
|
|
* equals([1, 2, 3], [1, 2, 3]); // true |
85
|
|
|
* equals([1, 3, 2], [1, 2, 3]); // false (should have the same order) |
86
|
|
|
* equals($a, $b); // false |
87
|
|
|
* equals($a, $c); // false |
88
|
|
|
* $b->b->c = 'Hello'; |
89
|
|
|
* equals($a, $b); // true |
90
|
|
|
* ``` |
91
|
|
|
* |
92
|
|
|
* @signature * -> * -> Boolean |
93
|
|
|
* @param mixed $a |
|
|
|
|
94
|
|
|
* @param mixed $b |
|
|
|
|
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
function equals() { |
98
|
|
|
$equals = function($a, $b) { |
99
|
|
|
$type = type($a); |
100
|
|
|
if ($type != type($b)) |
101
|
|
|
return false; |
102
|
|
|
switch ($type) { |
103
|
|
|
case 'Null': |
104
|
|
|
case 'Boolean': |
105
|
|
|
case 'String': |
106
|
|
|
case 'Number': |
107
|
|
|
case 'Unknown': |
108
|
|
|
case 'Function': |
109
|
|
|
case 'Resource': |
110
|
|
|
case 'Error': |
111
|
|
|
case 'Stream': |
112
|
|
|
return $a == $b; |
113
|
|
|
case 'List': |
114
|
|
|
$length = length($a); |
115
|
|
|
return length($b) != $length ? false : |
116
|
|
|
0 == $length ? true : |
117
|
|
|
equals(head($a), head($b)) && equals(tail($a), tail($b)); |
118
|
|
|
case 'Array': |
119
|
|
|
case 'ArrayObject': |
120
|
|
|
case 'Object': |
121
|
|
|
return equals(keys($a), keys($b)) && equals(values($a), values($b)); |
122
|
|
|
} |
123
|
|
|
}; |
124
|
|
|
return apply(curry($equals), func_get_args()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns `$a < $b`. |
130
|
|
|
* |
131
|
|
|
* @signature * -> * -> Boolean |
132
|
|
|
* @param mixed $a |
|
|
|
|
133
|
|
|
* @param mixed $b |
|
|
|
|
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
function lt() { |
137
|
|
|
return apply(curry(function($a, $b){ |
138
|
|
|
return $a < $b; |
139
|
|
|
}), func_get_args()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns `$a <= $b`. |
144
|
|
|
* |
145
|
|
|
* @signature * -> * -> Boolean |
146
|
|
|
* @param mixed $a |
|
|
|
|
147
|
|
|
* @param mixed $b |
|
|
|
|
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
function lte() { |
151
|
|
|
return apply(curry(function($a, $b){ |
152
|
|
|
return $a <= $b; |
153
|
|
|
}), func_get_args()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns `$a > $b`. |
158
|
|
|
* |
159
|
|
|
* @signature * -> * -> Boolean |
160
|
|
|
* @param mixed $a |
|
|
|
|
161
|
|
|
* @param mixed $b |
|
|
|
|
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
function gt() { |
165
|
|
|
return apply(curry(function($a, $b){ |
166
|
|
|
return $a > $b; |
167
|
|
|
}), func_get_args()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns `$a >= $b`. |
172
|
|
|
* |
173
|
|
|
* @signature * -> * -> Boolean |
174
|
|
|
* @param mixed $a |
|
|
|
|
175
|
|
|
* @param mixed $b |
|
|
|
|
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
|
|
function gte() { |
179
|
|
|
return apply(curry(function($a, $b){ |
180
|
|
|
return $a >= $b; |
181
|
|
|
}), func_get_args()); |
182
|
|
|
} |
183
|
|
|
|
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.