-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathExpr.php
538 lines (494 loc) · 13.3 KB
/
Expr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<?php
/*
* This file is part of the webmozart/expression package.
*
* (c) Bernhard Schussek <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Webmozart\Expression;
use ArrayAccess;
use InvalidArgumentException;
use Traversable;
use Webmozart\Expression\Constraint\Contains;
use Webmozart\Expression\Constraint\EndsWith;
use Webmozart\Expression\Constraint\Equals;
use Webmozart\Expression\Constraint\GreaterThan;
use Webmozart\Expression\Constraint\GreaterThanEqual;
use Webmozart\Expression\Constraint\In;
use Webmozart\Expression\Constraint\IsEmpty;
use Webmozart\Expression\Constraint\IsInstanceOf;
use Webmozart\Expression\Constraint\KeyExists;
use Webmozart\Expression\Constraint\KeyNotExists;
use Webmozart\Expression\Constraint\LessThan;
use Webmozart\Expression\Constraint\LessThanEqual;
use Webmozart\Expression\Constraint\Matches;
use Webmozart\Expression\Constraint\NotEquals;
use Webmozart\Expression\Constraint\NotSame;
use Webmozart\Expression\Constraint\Same;
use Webmozart\Expression\Constraint\StartsWith;
use Webmozart\Expression\Logic\AlwaysFalse;
use Webmozart\Expression\Logic\AlwaysTrue;
use Webmozart\Expression\Logic\AndX;
use Webmozart\Expression\Logic\Not;
use Webmozart\Expression\Logic\OrX;
use Webmozart\Expression\Selector\All;
use Webmozart\Expression\Selector\AtLeast;
use Webmozart\Expression\Selector\AtMost;
use Webmozart\Expression\Selector\Count;
use Webmozart\Expression\Selector\Exactly;
use Webmozart\Expression\Selector\Key;
use Webmozart\Expression\Selector\Method;
use Webmozart\Expression\Selector\Property;
/**
* Factory for {@link Expression} instances.
*
* Use this class to build expressions:ons:
*
* ```php
* $expr = Expr::greaterThan(20)->orLessThan(10);
* ```
*
* You can evaluate the expression with another value {@link Expression::evaluate()}:
*
* ```php
* if ($expr->evaluate($value)) {
* // do something...
* }
* ```
*
* You can also evaluate expressions by arrays by passing the array keys as
* second argument:
*
* ```php
* $expr = Expr::greaterThan(20, 'age')
* ->andStartsWith('Thomas', 'name');
*
* $values = array(
* 'age' => 35,
* 'name' => 'Thomas Edison',
* );
*
* if ($expr->evaluate($values)) {
* // do something...
* }
* ```
*
* @since 1.0
*
* @author Bernhard Schussek <[email protected]>
*/
class Expr
{
/**
* Filter a collection for entries matching the expression.
*
* @param array|ArrayAccess|Traversable $collection An array or an object
* implementing Traversable
* and ArrayAccess.
* @param Expression $expr The expression to
* evaluate for each entry.
*
* @return array|ArrayAccess|Traversable The filtered collection.
*/
public static function filter($collection, Expression $expr)
{
if (is_array($collection)) {
return array_filter($collection, array($expr, 'evaluate'));
}
if (!($collection instanceof Traversable && $collection instanceof ArrayAccess)) {
throw new InvalidArgumentException(sprintf(
'Expected an array or an instance of Traversable and ArrayAccess. Got: %s',
is_object($collection) ? get_class($collection) : gettype($collection)
));
}
$clone = clone $collection;
foreach ($collection as $key => $value) {
if (!$expr->evaluate($value)) {
unset($clone[$key]);
}
}
return $clone;
}
/**
* Returns the expression.
*
* Facilitates usage of expressions on PHP < 7.
*
* @param Expression $expr An expression.
*
* @return Expression The expression.
*/
public static function expr(Expression $expr)
{
return $expr;
}
/**
* Negate an expression.
*
* @param Expression $expr The negated expression.
*
* @return Not The created negation.
*/
public static function not(Expression $expr)
{
return new Not($expr);
}
/**
* Create a conjunction.
*
* @param Expression[] $conjuncts The conjuncts.
*
* @return AndX The created conjunction.
*/
public static function andX(array $conjuncts)
{
return new AndX($conjuncts);
}
/**
* Create a disjunction.
*
* @param Expression[] $disjuncts The disjuncts.
*
* @return OrX The created disjunction.
*/
public static function orX(array $disjuncts)
{
return new OrX($disjuncts);
}
/**
* Always true (tautology).
*
* @return AlwaysTrue The created expression.
*/
public static function true()
{
return new AlwaysTrue();
}
/**
* Always false (contradiction).
*
* @return AlwaysFalse The created expression.
*/
public static function false()
{
return new AlwaysFalse();
}
/**
* Check that the value of an array key matches an expression.
*
* @param string|int $key The array key.
* @param Expression $expr The evaluated expression.
*
* @return Key The created expression.
*/
public static function key($key, Expression $expr)
{
return new Key($key, $expr);
}
/**
* Check that the result of a method call matches an expression.
*
* @param string $methodName The name of the method to call.
* @param mixed $args... The method arguments.
* @param Expression $expr The evaluated expression.
*
* @return Method The created expression.
*/
public static function method($methodName, $args)
{
$args = func_get_args();
$methodName = array_shift($args);
$expr = array_pop($args);
return new Method($methodName, $args, $expr);
}
/**
* Check that the value of a property matches an expression.
*
* @param string $propertyName The name of the property.
* @param Expression $expr The evaluated expression.
*
* @return Method The created expression.
*/
public static function property($propertyName, Expression $expr)
{
return new Property($propertyName, $expr);
}
/**
* Check that at least N entries of a traversable value match an expression.
*
* @param int $count The minimum number of entries that need to match.
* @param Expression $expr The evaluated expression.
*
* @return AtLeast The created expression.
*/
public static function atLeast($count, Expression $expr)
{
return new AtLeast($count, $expr);
}
/**
* Check that at most N entries of a traversable value match an expression.
*
* @param int $count The maximum number of entries that need to match.
* @param Expression $expr The evaluated expression.
*
* @return AtMost The created expression.
*/
public static function atMost($count, Expression $expr)
{
return new AtMost($count, $expr);
}
/**
* Check that exactly N entries of a traversable value match an expression.
*
* @param int $count The number of entries that need to match.
* @param Expression $expr The evaluated expression.
*
* @return Exactly The created expression.
*/
public static function exactly($count, Expression $expr)
{
return new Exactly($count, $expr);
}
/**
* Check that all entries of a traversable value match an expression.
*
* @param Expression $expr The evaluated expression.
*
* @return All The created expression.
*/
public static function all(Expression $expr)
{
return new All($expr);
}
/**
* Check that the count of a collection matches an expression.
*
* @param Expression $expr The evaluated expression.
*
* @return Count The created expression.
*/
public static function count(Expression $expr)
{
return new Count($expr);
}
/**
* Check that a value is null.
*
* @return Same The created expression.
*/
public static function null()
{
return new Same(null);
}
/**
* Check that a value is not null.
*
* @return NotSame The created expression.
*/
public static function notNull()
{
return new NotSame(null);
}
/**
* Check that a value is empty.
*
* @return IsEmpty The created expression.
*/
public static function isEmpty()
{
return new IsEmpty();
}
/**
* Check that a value is not empty.
*
* @return Not The created expression.
*/
public static function notEmpty()
{
return new Not(new IsEmpty());
}
/**
* Check that a value is an instance of a given class.
*
* @param string $className The class name.
*
* @return IsEmpty The created expression.
*/
public static function isInstanceOf($className)
{
return new IsInstanceOf($className);
}
/**
* Check that a value equals another value.
*
* @param mixed $value The compared value.
*
* @return Equals The created expression.
*/
public static function equals($value)
{
return new Equals($value);
}
/**
* Check that a value does not equal another value.
*
* @param mixed $value The compared value.
*
* @return NotEquals The created expression.
*/
public static function notEquals($value)
{
return new NotEquals($value);
}
/**
* Check that a value is identical to another value.
*
* @param mixed $value The compared value.
*
* @return Same The created expression.
*/
public static function same($value)
{
return new Same($value);
}
/**
* Check that a value is not identical to another value.
*
* @param mixed $value The compared value.
*
* @return NotSame The created expression.
*/
public static function notSame($value)
{
return new NotSame($value);
}
/**
* Check that a value is greater than another value.
*
* @param mixed $value The compared value.
*
* @return GreaterThan The created expression.
*/
public static function greaterThan($value)
{
return new GreaterThan($value);
}
/**
* Check that a value is greater than or equal to another value.
*
* @param mixed $value The compared value.
*
* @return GreaterThanEqual The created expression.
*/
public static function greaterThanEqual($value)
{
return new GreaterThanEqual($value);
}
/**
* Check that a value is less than another value.
*
* @param mixed $value The compared value.
*
* @return LessThan The created expression.
*/
public static function lessThan($value)
{
return new LessThan($value);
}
/**
* Check that a value is less than or equal to another value.
*
* @param mixed $value The compared value.
*
* @return LessThanEqual The created expression.
*/
public static function lessThanEqual($value)
{
return new LessThanEqual($value);
}
/**
* Check that a value occurs in a list of values.
*
* @param array $values The compared values.
*
* @return In The created expression.
*/
public static function in(array $values)
{
return new In($values);
}
/**
* Check that a value matches a regular expression.
*
* @param string $regExp The regular expression.
*
* @return Matches The created expression.
*/
public static function matches($regExp)
{
return new Matches($regExp);
}
/**
* Check that a value starts with a given string.
*
* @param string $prefix The prefix string.
*
* @return StartsWith The created expression.
*/
public static function startsWith($prefix)
{
return new StartsWith($prefix);
}
/**
* Check that a value ends with a given string.
*
* @param string $suffix The suffix string.
*
* @return EndsWith The created expression.
*/
public static function endsWith($suffix)
{
return new EndsWith($suffix);
}
/**
* Check that a value contains a given string.
*
* @param string $string The sub-string.
*
* @return Contains The created expression.
*/
public static function contains($string)
{
return new Contains($string);
}
/**
* Check that a value key exists.
*
* @param string $keyName The key name.
*
* @return KeyExists The created expression.
*/
public static function keyExists($keyName)
{
return new KeyExists($keyName);
}
/**
* Check that a value key does not exist.
*
* @param string $keyName The key name.
*
* @return KeyNotExists The created expression.
*/
public static function keyNotExists($keyName)
{
return new KeyNotExists($keyName);
}
/**
* This class cannot be instantiated.
*/
private function __construct()
{
}
}