1
|
|
|
<?php namespace Cornford\Logical; |
2
|
|
|
|
3
|
|
|
use Cornford\Logical\Contracts\LogicalStatementInterface; |
4
|
|
|
|
5
|
|
|
class LogicalStatement implements LogicalStatementInterface { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Array of custom logical statements |
9
|
|
|
* |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $customStatements = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Define a custom logical statement |
16
|
|
|
* |
17
|
|
|
* @param string $name The statement name |
18
|
|
|
* @param callable $callback The callback method to execute |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function defineCustomStatement($name, callable $callback) |
23
|
|
|
{ |
24
|
|
|
$this->customStatements[$name] = $callback; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Checks that a custom logical statement exists |
29
|
|
|
* |
30
|
|
|
* @param string $name The statement name |
31
|
|
|
* |
32
|
|
|
* @return boolean |
33
|
|
|
*/ |
34
|
|
|
public function customStatementExists($name) |
35
|
|
|
{ |
36
|
|
|
if (!array_key_exists($name, $this->customStatements)) { |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return true; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns all custom logical statements |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function getCustomStatements() |
49
|
|
|
{ |
50
|
|
|
return $this->customStatements; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Define a custom logical statement |
55
|
|
|
* |
56
|
|
|
* @param string $name The statement name |
57
|
|
|
* @param string|integer|boolean $input The input value |
58
|
|
|
* @param string|integer|boolean $expected The expected type |
59
|
|
|
* |
60
|
|
|
* @return boolean |
61
|
|
|
*/ |
62
|
|
|
public function callCustomStatement($name, $input, $expected) |
63
|
|
|
{ |
64
|
|
|
return call_user_func_array($this->customStatements[$name], [$input, $expected]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The input value equals the expected value |
69
|
|
|
* |
70
|
|
|
* @param string $input The input value |
71
|
|
|
* @param string $expected The expected value |
72
|
|
|
* |
73
|
|
|
* @return boolean |
74
|
|
|
*/ |
75
|
|
|
public function equals($input, $expected) |
76
|
|
|
{ |
77
|
|
|
if ($input == $expected) { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* The input value doesn't equal the expected value |
86
|
|
|
* |
87
|
|
|
* @param string $input The input value |
88
|
|
|
* @param string $expected The expected value |
89
|
|
|
* |
90
|
|
|
* @return boolean |
91
|
|
|
*/ |
92
|
|
|
public function notEquals($input, $expected) |
93
|
|
|
{ |
94
|
|
|
return !$this->equals($input, $expected); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* The input value has is the same length as the expected value |
99
|
|
|
* |
100
|
|
|
* @param string $input The input value |
101
|
|
|
* @param integer $expected The expected length value |
102
|
|
|
* |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
|
|
public function isLength($input, $expected) |
106
|
|
|
{ |
107
|
|
|
if (strlen($input) === $expected) { |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* The input value isn't the same length as the expected value |
116
|
|
|
* |
117
|
|
|
* @param string $input The input value |
118
|
|
|
* @param integer $expected The expected length value |
119
|
|
|
* |
120
|
|
|
* @return boolean |
121
|
|
|
*/ |
122
|
|
|
public function isNotLength($input, $expected) |
123
|
|
|
{ |
124
|
|
|
return !$this->isLength($input, $expected); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* The input value is of the expected type |
129
|
|
|
* |
130
|
|
|
* @param string $input The input value |
131
|
|
|
* @param string $expected The expected type |
132
|
|
|
* |
133
|
|
|
* @return boolean |
134
|
|
|
*/ |
135
|
|
|
public function is($input, $expected) |
136
|
|
|
{ |
137
|
|
|
$method = "is_$expected"; |
138
|
|
|
|
139
|
|
|
if (function_exists($method)) |
140
|
|
|
{ |
141
|
|
|
if ($method($input)) { |
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* The input value doesn't equal the expected value |
151
|
|
|
* |
152
|
|
|
* @param string $input The input value |
153
|
|
|
* @param string $expected The expected value |
154
|
|
|
* |
155
|
|
|
* @return boolean |
156
|
|
|
*/ |
157
|
|
|
public function isNot($input, $expected) |
158
|
|
|
{ |
159
|
|
|
return !$this->is($input, $expected); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* The input value contains the expected value |
164
|
|
|
* |
165
|
|
|
* @param string $input The input value |
166
|
|
|
* @param string $expected The expected value |
167
|
|
|
* |
168
|
|
|
* @return boolean |
169
|
|
|
*/ |
170
|
|
|
public function contains($input, $expected) |
171
|
|
|
{ |
172
|
|
|
if (stristr($input, $expected)) { |
173
|
|
|
return true; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return false; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* The input value doesn't contain the expected value |
181
|
|
|
* |
182
|
|
|
* @param string $input The input value |
183
|
|
|
* @param string $expected The expected value |
184
|
|
|
* |
185
|
|
|
* @return boolean |
186
|
|
|
*/ |
187
|
|
|
public function notContains($input, $expected) |
188
|
|
|
{ |
189
|
|
|
return !$this->contains($input, $expected); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* The input value is contained in the expected value array |
194
|
|
|
* |
195
|
|
|
* @param string $input The input value |
196
|
|
|
* @param array $expected The expected value array |
197
|
|
|
* |
198
|
|
|
* @return boolean |
199
|
|
|
*/ |
200
|
|
|
public function containedIn($input, $expected) |
201
|
|
|
{ |
202
|
|
|
foreach($expected as $value) { |
203
|
|
|
if (stristr($input, $value)) { |
204
|
|
|
return true; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return false; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* The input value is not contained in the expected value array |
213
|
|
|
* |
214
|
|
|
* @param string $input The input value |
215
|
|
|
* @param array $expected The expected value array |
216
|
|
|
* |
217
|
|
|
* @return boolean |
218
|
|
|
*/ |
219
|
|
|
public function notContainedIn($input, $expected) |
220
|
|
|
{ |
221
|
|
|
return !$this->containedIn($input, $expected); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* The input value is in the expected value array |
226
|
|
|
* |
227
|
|
|
* @param string $input The input value |
228
|
|
|
* @param array $expected The expected value array |
229
|
|
|
* |
230
|
|
|
* @return boolean |
231
|
|
|
*/ |
232
|
|
|
public function in($input, $expected) |
233
|
|
|
{ |
234
|
|
|
if (in_array($input, $expected)) { |
235
|
|
|
return true; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return false; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* The input value is not in the expected value array |
243
|
|
|
* |
244
|
|
|
* @param string $input The input value |
245
|
|
|
* @param array $expected The expected value array |
246
|
|
|
* |
247
|
|
|
* @return boolean |
248
|
|
|
*/ |
249
|
|
|
public function notIn($input, $expected) |
250
|
|
|
{ |
251
|
|
|
return !$this->in($input, $expected); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* The input value is between expected values |
256
|
|
|
* |
257
|
|
|
* @param string $input The input value |
258
|
|
|
* @param array $expected The expected values |
259
|
|
|
* |
260
|
|
|
* @return boolean |
261
|
|
|
*/ |
262
|
|
|
public function between($input, $expected) |
263
|
|
|
{ |
264
|
|
|
if (($expected[0] <= $input) && ($input <= $expected[1])) { |
265
|
|
|
return true; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return false; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* The input value is not between the expected values |
273
|
|
|
* |
274
|
|
|
* @param string $input The input value |
275
|
|
|
* @param array $expected The expected values |
276
|
|
|
* |
277
|
|
|
* @return boolean |
278
|
|
|
*/ |
279
|
|
|
public function notBetween($input, $expected) |
280
|
|
|
{ |
281
|
|
|
return !$this->between($input, $expected); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* The input value is null |
286
|
|
|
* |
287
|
|
|
* @param string $input The input value |
288
|
|
|
* |
289
|
|
|
* @return boolean |
290
|
|
|
*/ |
291
|
|
|
public function null($input) |
292
|
|
|
{ |
293
|
|
|
if ($input === null) { |
294
|
|
|
return true; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return false; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* The input value is not null |
302
|
|
|
* |
303
|
|
|
* @param string $input The input value |
304
|
|
|
* |
305
|
|
|
* @return boolean |
306
|
|
|
*/ |
307
|
|
|
public function notNull($input) |
308
|
|
|
{ |
309
|
|
|
return !$this->null($input); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* The input value is less than the expected value |
314
|
|
|
* |
315
|
|
|
* @param string $input The input value |
316
|
|
|
* @param string $expected The expected value |
317
|
|
|
* |
318
|
|
|
* @return boolean |
319
|
|
|
*/ |
320
|
|
|
public function lessThan($input, $expected) |
321
|
|
|
{ |
322
|
|
|
if ($input < $expected) { |
323
|
|
|
return true; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return false; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* The input value isn't less than the expected value |
331
|
|
|
* |
332
|
|
|
* @param string $input The input value |
333
|
|
|
* @param string $expected The expected value |
334
|
|
|
* |
335
|
|
|
* @return boolean |
336
|
|
|
*/ |
337
|
|
|
public function notLessThan($input, $expected) |
338
|
|
|
{ |
339
|
|
|
return !$this->lessThan($input, $expected); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* The input value is greater than the expected value |
344
|
|
|
* |
345
|
|
|
* @param string $input The input value |
346
|
|
|
* @param string $expected The expected value |
347
|
|
|
* |
348
|
|
|
* @return boolean |
349
|
|
|
*/ |
350
|
|
|
public function greaterThan($input, $expected) |
351
|
|
|
{ |
352
|
|
|
if ($input > $expected) { |
353
|
|
|
return true; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
return false; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* The input value isn't greater than the expected value |
361
|
|
|
* |
362
|
|
|
* @param string $input The input value |
363
|
|
|
* @param string $expected The expected value |
364
|
|
|
* |
365
|
|
|
* @return boolean |
366
|
|
|
*/ |
367
|
|
|
public function notGreaterThan($input, $expected) |
368
|
|
|
{ |
369
|
|
|
return !$this->lessThan($input, $expected); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* The input value is less than or equal to the expected value |
374
|
|
|
* |
375
|
|
|
* @param string $input The input value |
376
|
|
|
* @param string $expected The expected value |
377
|
|
|
* |
378
|
|
|
* @return boolean |
379
|
|
|
*/ |
380
|
|
|
public function lessThanOrEqual($input, $expected) |
381
|
|
|
{ |
382
|
|
|
if ($input <= $expected) { |
383
|
|
|
return true; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
return false; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* The input value isn't less than or equal to the expected value |
391
|
|
|
* |
392
|
|
|
* @param string $input The input value |
393
|
|
|
* @param string $expected The expected value |
394
|
|
|
* |
395
|
|
|
* @return boolean |
396
|
|
|
*/ |
397
|
|
|
public function notLessThanOrEqual($input, $expected) |
398
|
|
|
{ |
399
|
|
|
return !$this->lessThanOrEqual($input, $expected); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* The input value is greater than or equal to the expected value |
404
|
|
|
* |
405
|
|
|
* @param string $input The input value |
406
|
|
|
* @param string $expected The expected value |
407
|
|
|
* |
408
|
|
|
* @return boolean |
409
|
|
|
*/ |
410
|
|
|
public function greaterThanOrEqual($input, $expected) |
411
|
|
|
{ |
412
|
|
|
if ($input >= $expected) { |
413
|
|
|
return true; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
return false; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* The input value isn't greater than or equal to the expected value |
421
|
|
|
* |
422
|
|
|
* @param string $input The input value |
423
|
|
|
* @param string $expected The expected value |
424
|
|
|
* |
425
|
|
|
* @return boolean |
426
|
|
|
*/ |
427
|
|
|
public function notGreaterThanOrEqual($input, $expected) |
428
|
|
|
{ |
429
|
|
|
return !$this->greaterThanOrEqual($input, $expected); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
} |