-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathRules.php
72 lines (53 loc) · 1.23 KB
/
Rules.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
<?php
namespace AC\ListScreenRepository;
use InvalidArgumentException;
final class Rules {
const MATCH_ALL = 'all';
const MATCH_ANY = 'any';
/**
* @var string
*/
private $match_decision;
/**
* @var Rule[]
*/
private $rules = [];
public function __construct( $match_decision = null ) {
if ( null === $match_decision ) {
$match_decision = self::MATCH_ANY;
}
$this->match_decision = $match_decision;
$this->validate();
}
private function validate() {
$match_decisions = [ self::MATCH_ANY, self::MATCH_ALL ];
if ( ! in_array( $this->match_decision, $match_decisions, true ) ) {
throw new InvalidArgumentException( 'Invalid match decision.' );
}
}
/**
* @param Rule $rule
*
* @return $this
*/
public function add_rule( Rule $rule ) {
$this->rules[] = $rule;
return $this;
}
public function match( array $args ) {
$matches = 0;
foreach ( $this->rules as $rule ) {
if ( $rule->match( $args ) ) {
$matches++;
}
}
$has_as_least_one_match = $matches > 0;
switch ( $this->match_decision ) {
case self::MATCH_ANY:
return $has_as_least_one_match;
case self::MATCH_ALL:
return $has_as_least_one_match && $matches === count( $this->rules );
}
return false;
}
}