-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRulesEngine.php
106 lines (81 loc) · 2.01 KB
/
RulesEngine.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
<?php
namespace CodeMeme\RulesBundle;
use Doctrine\Common\Collections\ArrayCollection;
class RulesEngine
{
private $aliases;
private $rules;
private $targets;
public function __construct($rules = array())
{
$this->aliases = new ArrayCollection;
$this->rules = new ArrayCollection;
$this->targets = new ArrayCollection;
}
public function setAlias($alias, $value)
{
$this->getAliases()->set($alias, $value);
}
public function getAliases()
{
return $this->aliases;
}
public function setAliases($aliases)
{
$this->aliases = $aliases;
return $this;
}
public function addRule($rule)
{
// Import existing aliases into the new rule
foreach ($this->getAliases() as $alias => $value) {
if (! $rule->getAliases()->containsKey($alias)) {
$rule->getAliases()->set($alias, $value);
}
}
$this->getRules()->add($rule);
}
public function addRules($rules)
{
foreach ($rules as $rule) {
$this->addRule($rule);
}
}
public function getRules()
{
return $this->rules;
}
public function setRules($rules)
{
$this->rules = $rules;
return $this;
}
public function addTarget($target)
{
$this->getTargets()->add($target);
}
public function addTargets($targets)
{
foreach ($targets as $target) {
$this->addTarget($target);
}
}
public function getTargets()
{
return $this->targets;
}
public function setTargets($targets)
{
$this->targets = $targets;
return $this;
}
public function evaluate($targets = array())
{
foreach ($this->getTargets() as $target) {
$targets[] = $target;
}
foreach ($this->getRules() as $rule) {
$rule->evaluate($targets);
}
}
}