|
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
|
|
|
|
|
21
|
|
|
class Rules extends \ArrayObject |
|
22
|
|
|
{ |
|
23
|
|
|
private $_language; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Rules constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param Language $language |
|
29
|
|
|
*/ |
|
30
|
22 |
|
public function __construct($language) |
|
31
|
|
|
{ |
|
32
|
22 |
|
$this->_language = $language; |
|
33
|
22 |
|
} |
|
34
|
|
|
|
|
35
|
20 |
|
public function addMany(array $rules, $prefix = null) |
|
36
|
|
|
{ |
|
37
|
20 |
|
foreach ($rules as $name => $rule) { |
|
38
|
16 |
|
$name = $this->_getName($name, $prefix); |
|
39
|
|
|
|
|
40
|
16 |
|
if ($rule instanceof Rule) { |
|
41
|
15 |
|
$this->add($name, $rule); |
|
42
|
16 |
|
} elseif (is_array($rule)) { |
|
43
|
5 |
|
$this->addMany($rule, $name); |
|
44
|
5 |
|
} else { |
|
45
|
1 |
|
throw new \LogicException(); // todo: exception, message |
|
46
|
|
|
} |
|
47
|
19 |
|
} |
|
48
|
19 |
|
} |
|
49
|
|
|
|
|
50
|
16 |
|
private function _getName($name, $prefix) |
|
51
|
|
|
{ |
|
52
|
16 |
|
if (is_int($name)) { |
|
53
|
5 |
|
return $prefix; |
|
54
|
|
|
} else { |
|
55
|
16 |
|
return $prefix ? "$prefix.$name" : $name; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
16 |
|
public function add($name, Rule $rule) |
|
60
|
|
|
{ |
|
61
|
16 |
|
if (!isset($this[$name])) { |
|
62
|
16 |
|
$this[$name] = []; |
|
63
|
16 |
|
} |
|
64
|
|
|
|
|
65
|
16 |
|
if ($rule->language === false) { |
|
66
|
15 |
|
$rule->language = $this->_language; |
|
67
|
15 |
|
} |
|
68
|
|
|
|
|
69
|
16 |
|
$rule->factory->setBase($name); |
|
70
|
16 |
|
$this[$name][] = $rule; |
|
71
|
16 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $name |
|
75
|
|
|
* @param int $index |
|
76
|
|
|
* |
|
77
|
|
|
* @return \Kadet\Highlighter\Parser\Rule |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function &rule($name, $index = 0) |
|
80
|
|
|
{ |
|
81
|
1 |
|
return $this[$name][$index]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param $name |
|
86
|
|
|
* |
|
87
|
|
|
* @return \Kadet\Highlighter\Parser\Rule[] |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public function rules($name) |
|
90
|
|
|
{ |
|
91
|
2 |
|
if (!isset($this[$name])) { |
|
92
|
1 |
|
throw new \InvalidArgumentException(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
return $this[$name]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
public function remove($name, $index = null) |
|
99
|
|
|
{ |
|
100
|
1 |
|
if ($index === null) { |
|
101
|
1 |
|
unset($this[$name]); |
|
102
|
1 |
|
} else { |
|
103
|
1 |
|
unset($this[$name][$index]); |
|
104
|
|
|
} |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
13 |
|
public function all() |
|
108
|
|
|
{ |
|
109
|
13 |
|
$items = $this->getArrayCopy(); |
|
110
|
13 |
|
if(empty($items)) return []; |
|
111
|
|
|
|
|
112
|
13 |
|
return call_user_func_array('array_merge', $items); |
|
113
|
|
|
} |
|
114
|
|
|
} |