1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Highlighter |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2016, Some right reserved. |
6
|
|
|
* |
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Contact with author: |
10
|
|
|
* Xmpp: [email protected] |
11
|
|
|
* E-mail: [email protected] |
12
|
|
|
* |
13
|
|
|
* From Kadet with love. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Kadet\Highlighter\Parser; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Kadet\Highlighter\Language\Language; |
20
|
|
|
use Kadet\Highlighter\Parser\Validator\Validator; |
21
|
|
|
|
22
|
|
|
class Rules extends \ArrayObject |
23
|
|
|
{ |
24
|
|
|
private $_language; |
25
|
|
|
public $validator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Rules constructor. |
29
|
|
|
* |
30
|
|
|
* @param Language $language |
31
|
|
|
*/ |
32
|
22 |
|
public function __construct($language) |
33
|
|
|
{ |
34
|
22 |
|
$this->_language = $language; |
35
|
22 |
|
$this->validator = new Validator(); |
36
|
22 |
|
} |
37
|
|
|
|
38
|
20 |
|
public function addMany(array $rules, $prefix = null) |
39
|
|
|
{ |
40
|
20 |
|
foreach ($rules as $name => $rule) { |
41
|
16 |
|
$name = $this->_getName($name, $prefix); |
42
|
|
|
|
43
|
16 |
|
if ($rule instanceof Rule) { |
44
|
15 |
|
$this->add($name, $rule); |
45
|
16 |
|
} elseif (is_array($rule)) { |
46
|
5 |
|
$this->addMany($rule, $name); |
47
|
5 |
|
} else { |
48
|
1 |
|
throw new \LogicException(); // todo: exception, message |
49
|
|
|
} |
50
|
19 |
|
} |
51
|
19 |
|
} |
52
|
|
|
|
53
|
16 |
|
private function _getName($name, $prefix) |
54
|
|
|
{ |
55
|
16 |
|
if (is_int($name)) { |
56
|
5 |
|
return $prefix; |
57
|
|
|
} else { |
58
|
16 |
|
return $prefix ? "$prefix.$name" : $name; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
16 |
|
public function add($name, Rule $rule) |
63
|
|
|
{ |
64
|
16 |
|
if (!isset($this[$name])) { |
65
|
16 |
|
$this[$name] = []; |
66
|
16 |
|
} |
67
|
|
|
|
68
|
16 |
|
if ($rule->language === false) { |
69
|
15 |
|
$rule->language = $this->_language; |
70
|
15 |
|
} |
71
|
|
|
|
72
|
16 |
|
if ($rule->validator === false) { |
73
|
15 |
|
$rule->validator = $this->validator; |
74
|
15 |
|
} |
75
|
|
|
|
76
|
16 |
|
$rule->factory->setBase($name); |
77
|
16 |
|
$this[$name][] = $rule; |
78
|
16 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $name |
82
|
|
|
* @param int $index |
83
|
|
|
* |
84
|
|
|
* @return \Kadet\Highlighter\Parser\Rule |
85
|
|
|
*/ |
86
|
1 |
|
public function &rule($name, $index = 0) |
87
|
|
|
{ |
88
|
1 |
|
return $this[$name][$index]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $name |
93
|
|
|
* |
94
|
|
|
* @return \Kadet\Highlighter\Parser\Rule[] |
95
|
|
|
*/ |
96
|
2 |
|
public function rules($name) |
97
|
|
|
{ |
98
|
2 |
|
if (!isset($this[$name])) { |
99
|
1 |
|
throw new \InvalidArgumentException(); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return $this[$name]; |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
public function remove($name, $index = null) |
106
|
|
|
{ |
107
|
1 |
|
if ($index === null) { |
108
|
1 |
|
unset($this[$name]); |
109
|
1 |
|
} else { |
110
|
1 |
|
unset($this[$name][$index]); |
111
|
|
|
} |
112
|
1 |
|
} |
113
|
|
|
|
114
|
13 |
|
public function all() |
115
|
|
|
{ |
116
|
13 |
|
$items = $this->getArrayCopy(); |
117
|
13 |
|
if(empty($items)) return []; |
118
|
|
|
|
119
|
13 |
|
return call_user_func_array('array_merge', $items); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return Language |
124
|
|
|
*/ |
125
|
|
|
public function getLanguage() |
126
|
|
|
{ |
127
|
|
|
return $this->_language; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param Language $language |
132
|
|
|
*/ |
133
|
|
|
public function setLanguage(Language $language = null) |
134
|
|
|
{ |
135
|
|
|
$this->_language = $language; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|