1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the feed-io package. |
4
|
|
|
* |
5
|
|
|
* (c) Alexandre Debril <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace FeedIo; |
12
|
|
|
|
13
|
|
|
use FeedIo\Rule\OptionalField; |
14
|
|
|
|
15
|
|
|
class RuleSet |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \ArrayIterator |
19
|
|
|
*/ |
20
|
|
|
protected $rules; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $aliases = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var RuleAbstract |
29
|
|
|
*/ |
30
|
|
|
protected $default; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param RuleAbstract $default default rule |
34
|
|
|
*/ |
35
|
48 |
|
public function __construct(RuleAbstract $default = null) |
36
|
|
|
{ |
37
|
48 |
|
$this->rules = new \ArrayIterator(array()); |
38
|
48 |
|
$this->default = is_null($default) ? new OptionalField() : $default; |
39
|
48 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return RuleAbstract |
43
|
|
|
*/ |
44
|
6 |
|
public function getDefault() |
45
|
|
|
{ |
46
|
6 |
|
return $this->default; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
8 |
|
public function getRules() |
53
|
|
|
{ |
54
|
8 |
|
return $this->rules->getArrayCopy(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param RuleAbstract $rule |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
31 |
|
public function add(RuleAbstract $rule, array $aliases = array()) |
62
|
|
|
{ |
63
|
31 |
|
$this->rules->offsetSet(strtolower($rule->getNodeName()), $rule); |
64
|
31 |
|
$this->addAliases($rule->getNodeName(), $aliases); |
65
|
|
|
|
66
|
31 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $name |
71
|
|
|
* @param array $aliases |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
32 |
|
public function addAliases($name, array $aliases) |
75
|
|
|
{ |
76
|
32 |
|
foreach ($aliases as $alias) { |
77
|
15 |
|
$this->aliases[strtolower($alias)] = strtolower($name); |
78
|
32 |
|
} |
79
|
|
|
|
80
|
32 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $name |
85
|
|
|
* @return RuleAbstract |
86
|
|
|
* @throws NotFoundException |
87
|
|
|
*/ |
88
|
12 |
|
public function get($name) |
89
|
|
|
{ |
90
|
12 |
|
$name = $this->getNameForAlias(strtolower($name)); |
91
|
12 |
|
if ($this->rules->offsetExists($name)) { |
92
|
10 |
|
return $this->rules->offsetGet($name); |
93
|
|
|
} |
94
|
|
|
|
95
|
7 |
|
return $this->default; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $alias |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
13 |
|
public function getNameForAlias($alias) |
103
|
|
|
{ |
104
|
13 |
|
if (array_key_exists($alias, $this->aliases)) { |
105
|
6 |
|
return $this->aliases[$alias]; |
106
|
|
|
} |
107
|
|
|
|
108
|
11 |
|
return $alias; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|