|
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
|
1 |
|
public function __construct(RuleAbstract $default = null) |
|
36
|
|
|
{ |
|
37
|
1 |
|
$this->rules = new \ArrayIterator(array()); |
|
38
|
1 |
|
$this->default = is_null($default) ? new OptionalField() : $default; |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return RuleAbstract |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function getDefault() |
|
45
|
|
|
{ |
|
46
|
1 |
|
return $this->default; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getRules() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->rules->getArrayCopy(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param RuleAbstract $rule |
|
59
|
|
|
* @return $this |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function add(RuleAbstract $rule, array $aliases = array()) |
|
62
|
|
|
{ |
|
63
|
1 |
|
$this->rules->offsetSet(strtolower($rule->getNodeName()), $rule); |
|
64
|
1 |
|
$this->addAliases($rule->getNodeName(), $aliases); |
|
65
|
|
|
|
|
66
|
1 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $name |
|
71
|
|
|
* @param array $aliases |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function addAliases($name, array $aliases) |
|
75
|
|
|
{ |
|
76
|
1 |
|
foreach ($aliases as $alias) { |
|
77
|
1 |
|
$this->aliases[strtolower($alias)] = strtolower($name); |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $name |
|
85
|
|
|
* @return RuleAbstract |
|
86
|
|
|
* @throws NotFoundException |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function get($name) |
|
89
|
|
|
{ |
|
90
|
1 |
|
$name = $this->getNameForAlias(strtolower($name)); |
|
91
|
1 |
|
if ($this->rules->offsetExists($name)) { |
|
92
|
1 |
|
return $this->rules->offsetGet($name); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this->default; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param $alias |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function getNameForAlias($alias) |
|
103
|
|
|
{ |
|
104
|
1 |
|
if (array_key_exists($alias, $this->aliases)) { |
|
105
|
|
|
return $this->aliases[$alias]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
return $alias; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|