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 notEq() { |
72
|
|
|
return apply(curry(function($a, $b){ |
73
|
|
|
return $a != $b; |
74
|
|
|
}), func_get_args()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns `$x === $y`. |
79
|
|
|
* |
80
|
|
|
* @signature * -> * -> Boolean |
81
|
|
|
* @param mixed $a |
|
|
|
|
82
|
|
|
* @param mixed $b |
|
|
|
|
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
function eqq() { |
86
|
|
|
return apply(curry(function($a, $b){ |
87
|
|
|
return $a === $b; |
88
|
|
|
}), func_get_args()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns `$x !== $y`. |
93
|
|
|
* |
94
|
|
|
* @signature * -> * -> Boolean |
95
|
|
|
* @param mixed $a |
|
|
|
|
96
|
|
|
* @param mixed $b |
|
|
|
|
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
function notEqq() { |
100
|
|
|
return apply(curry(function($a, $b){ |
101
|
|
|
return $a !== $b; |
102
|
|
|
}), func_get_args()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns `true` if the two elements have the same type and are deeply equivalent. |
107
|
|
|
* ```php |
108
|
|
|
* $a = (object) ['a' => 1, 'b' => (object) ['c' => 'Hello'], 'd' => false]; |
109
|
|
|
* $b = (object) ['a' => 1, 'b' => (object) ['c' => 'Hi'], 'd' => false]; |
110
|
|
|
* $c = (object) ['a' => 1, 'b' => ['c' => 'Hello'], 'd' => false]; |
111
|
|
|
* equals(5, '5'); // false (should have the same type) |
112
|
|
|
* equals([1, 2, 3], [1, 2, 3]); // true |
113
|
|
|
* equals([1, 3, 2], [1, 2, 3]); // false (should have the same order) |
114
|
|
|
* equals($a, $b); // false |
115
|
|
|
* equals($a, $c); // false |
116
|
|
|
* $b->b->c = 'Hello'; |
117
|
|
|
* equals($a, $b); // true |
118
|
|
|
* ``` |
119
|
|
|
* |
120
|
|
|
* @signature * -> * -> Boolean |
121
|
|
|
* @param mixed $a |
|
|
|
|
122
|
|
|
* @param mixed $b |
|
|
|
|
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
function equals() { |
126
|
|
|
$equals = function($a, $b) { |
127
|
|
|
$type = type($a); |
128
|
|
|
if ($type != type($b)) |
129
|
|
|
return false; |
130
|
|
|
switch ($type) { |
131
|
|
|
case 'Null': |
132
|
|
|
case 'Boolean': |
133
|
|
|
case 'String': |
134
|
|
|
case 'Number': |
135
|
|
|
case 'Unknown': |
136
|
|
|
case 'Function': |
137
|
|
|
case 'Resource': |
138
|
|
|
case 'Error': |
139
|
|
|
case 'Stream': |
140
|
|
|
return $a == $b; |
141
|
|
|
case 'List': |
142
|
|
|
$length = length($a); |
143
|
|
|
return length($b) != $length ? false : |
144
|
|
|
0 == $length ? true : |
145
|
|
|
equals(head($a), head($b)) && equals(tail($a), tail($b)); |
146
|
|
|
case 'Array': |
147
|
|
|
case 'ArrayObject': |
148
|
|
|
case 'Object': |
149
|
|
|
return equals(keys($a), keys($b)) && equals(values($a), values($b)); |
150
|
|
|
} |
151
|
|
|
}; |
152
|
|
|
return apply(curry($equals), func_get_args()); |
153
|
|
|
} |
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 lt() { |
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 lte() { |
179
|
|
|
return apply(curry(function($a, $b){ |
180
|
|
|
return $a <= $b; |
181
|
|
|
}), func_get_args()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns `$a > $b`. |
186
|
|
|
* |
187
|
|
|
* @signature * -> * -> Boolean |
188
|
|
|
* @param mixed $a |
|
|
|
|
189
|
|
|
* @param mixed $b |
|
|
|
|
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
|
function gt() { |
193
|
|
|
return apply(curry(function($a, $b){ |
194
|
|
|
return $a > $b; |
195
|
|
|
}), func_get_args()); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Returns `$a >= $b`. |
200
|
|
|
* |
201
|
|
|
* @signature * -> * -> Boolean |
202
|
|
|
* @param mixed $a |
|
|
|
|
203
|
|
|
* @param mixed $b |
|
|
|
|
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
|
function gte() { |
207
|
|
|
return apply(curry(function($a, $b){ |
208
|
|
|
return $a >= $b; |
209
|
|
|
}), func_get_args()); |
210
|
|
|
} |
211
|
|
|
|
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.