forked from CodeMeme/RulesBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rule.php
186 lines (142 loc) · 4.9 KB
/
Rule.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
<?php
namespace CodeMeme\RulesBundle;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
class Rule
{
private $name;
private $aliases;
private $conditions;
private $actions;
private $fallbacks;
public function __construct($conditions = array(), $actions = array(), $fallbacks = array(), $aliases = array())
{
$this->conditions = is_array($conditions) ? new ArrayCollection($conditions) : $conditions;
$this->actions = is_array($actions) ? new ArrayCollection($actions) : $actions;
$this->fallbacks = is_array($fallbacks) ? new ArrayCollection($fallbacks) : $fallbacks;
$this->aliases = is_array($aliases) ? new ArrayCollection($aliases) : $aliases;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getAliases()
{
return $this->aliases;
}
public function setAliases($aliases)
{
$this->aliases = $aliases;
return $this;
}
public function getConditions()
{
return $this->conditions;
}
public function setCondition($key, $value)
{
$this->getConditions()->set($key, $value);
}
public function setConditions($conditions)
{
$this->conditions = is_array($conditions) ? new ArrayCollection($conditions) : $conditions;
return $this;
}
public function getActions()
{
return $this->actions;
}
public function setAction($key, $value)
{
$this->getActions()->set($key, $value);
}
public function setActions($actions)
{
$this->actions = is_array($actions) ? new ArrayCollection($actions) : $actions;
return $this;
}
public function getFallbacks()
{
return $this->fallbacks;
}
public function setFallbacks($fallbacks)
{
$this->fallbacks = $fallbacks;
return $this;
}
public function evaluate($targets)
{
if ($supported = $this->supports($targets)) {
$this->modify($supported, $this->getActions());
} else if (! $this->getFallbacks()->isEmpty()) {
$this->modify($this->alias($targets), $this->getFallbacks());
}
return (Boolean) $supported;
}
public function modify($targets, $actions)
{
foreach ($actions as $action) {
foreach ($targets as $target) {
if ($action->supports($target)) {
$action->modify($target);
}
}
}
}
public function supports($targets)
{
if (!is_array($targets) && !$targets instanceof \IteratorAggregate) {
$targets = array($targets);
}
if ($this->getConditions()->isEmpty()) {
throw new \InvalidArgumentException(sprintf('Rule %s has no conditions', $this->getName()));
} else if ($this->getActions()->isEmpty()) {
throw new \InvalidArgumentException(sprintf('Rule %s has no actions', $this->getName()));
}
$aliased = $this->alias($targets);
$supported = new ArrayCollection;
$unsupported = new ArrayCollection($aliased->toArray());
// Find targets that match all conditions or none
$passed = $this->getConditions()->forAll(function($i, $condition) use ($aliased, &$supported, &$unsupported) {
$matches = $aliased->filter(function($target) use ($condition, &$unsupported) {
if ($condition->supports($target)) {
$unsupported->removeElement($target);
}
return $condition->supports($target) && $condition->evaluate($target);
});
foreach ($matches as $match) {
$supported->add($match);
}
return !$matches->isEmpty();
});
if ($passed) {
foreach ($unsupported as $target) {
$supported->add($target);
}
}
return $passed ? $supported : false;
}
private function alias($targets)
{
$aliased = new ArrayCollection;
foreach ($targets as $target) {
$found = false;
foreach ($this->getAliases() as $alias => $instance) {
// Target can match multiple aliases, or an alias that has no specific instance
if (null === $instance || $target instanceof $instance) {
$found = true;
$aliased[] = array($alias => $target);
}
}
if (! $found) {
throw new \InvalidArgumentException(sprintf('No alias found for %s', is_object($target) ? get_class($target) : $target));
}
}
return $aliased;
}
}