-
Notifications
You must be signed in to change notification settings - Fork 2
/
RuleFactory.php
64 lines (49 loc) · 1.58 KB
/
RuleFactory.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
<?php
namespace CodeMeme\RulesBundle;
use CodeMeme\RulesBundle\Rule\BehaviorFactory;
class RuleFactory
{
public function createRule($name, $conditions, $actions, $fallbacks = array())
{
$rule = new Rule;
$rule->setName($name);
$factory = new BehaviorFactory;
if (! $conditions) {
throw new \InvalidArgumentException('Rule conditions must be iterable');
}
foreach ($conditions as $condition => $values) {
$rule->getConditions()->add(
$factory->createBehavior($condition, $values)
);
}
if (! $actions) {
throw new \InvalidArgumentException('Rule actions must be iterable');
}
foreach ($actions as $action => $values) {
$rule->getActions()->add(
$factory->createBehavior($action, $values)
);
}
if ($fallbacks) {
foreach ($fallbacks as $fallback => $values) {
$rule->getFallbacks()->add(
$factory->createBehavior($fallback, $values)
);
}
}
return $rule;
}
public function createRules($configs)
{
$rules = array();
foreach ($configs as $name => $config) {
$rules[] = $this->createRule(
$name,
$config['if'],
$config['then'],
isset($config['else']) ? $config['else'] : array()
);
}
return $rules;
}
}