1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author: Viskov Sergey |
4
|
|
|
* @date : 19.03.16 |
5
|
|
|
* @time : 0:04 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace LTDBeget\sphinx\configurator\configurationEntities\base; |
9
|
|
|
|
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use LogicException; |
12
|
|
|
use LTDBeget\sphinx\configurator\Configuration; |
13
|
|
|
use LTDBeget\sphinx\configurator\exceptions\SectionException; |
14
|
|
|
use LTDBeget\sphinx\enums\eSection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Definition |
18
|
|
|
* |
19
|
|
|
* @package LTDBeget\sphinx\configurator\configurationEntities\base |
20
|
|
|
*/ |
21
|
|
|
abstract class Definition extends Section |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $name; |
27
|
|
|
/** |
28
|
|
|
* @var Definition |
29
|
|
|
*/ |
30
|
|
|
private $inheritance; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Source constructor. |
34
|
|
|
* |
35
|
|
|
* @param Configuration $configuration |
36
|
|
|
* @param string $name |
37
|
|
|
* @param string|null $inheritance |
38
|
|
|
* |
39
|
|
|
* @throws InvalidArgumentException |
40
|
|
|
* @throws LogicException |
41
|
|
|
* @throws SectionException |
42
|
|
|
*/ |
43
|
13 |
|
public function __construct( |
44
|
|
|
Configuration $configuration, |
45
|
|
|
string $name, |
46
|
|
|
string $inheritance = NULL |
47
|
|
|
) |
48
|
|
|
{ |
49
|
13 |
|
parent::__construct($configuration); |
50
|
|
|
|
51
|
13 |
|
$this->defineName($this->sanitizeName($name)); |
52
|
|
|
|
53
|
12 |
|
if (!empty($inheritance)) { |
54
|
9 |
|
$this->setParent($this->sanitizeName($inheritance)); |
55
|
|
|
} |
56
|
11 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
1 |
|
public function __toString() : string |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
1 |
|
$string = "{$this->getType()} {$this->getName()}"; |
65
|
1 |
|
if ($this->isHasInheritance()) { |
66
|
1 |
|
$string .= " : {$this->getInheritance()->getName()}"; |
67
|
|
|
} |
68
|
|
|
} catch (\Exception $e) { |
69
|
|
|
$string = ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return $string; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
8 |
|
public function isHasInheritance() : bool |
79
|
|
|
{ |
80
|
8 |
|
return NULL !== $this->inheritance; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return Definition |
85
|
|
|
* @throws LogicException |
86
|
|
|
* @throws InvalidArgumentException |
87
|
|
|
* @throws SectionException |
88
|
|
|
*/ |
89
|
4 |
|
public function getInheritance() : Definition |
90
|
|
|
{ |
91
|
4 |
|
if (!$this->isHasInheritance()) { |
92
|
|
|
throw new SectionException("Trying to get inheritance for {$this->getType()} which doesn't' have it."); |
93
|
|
|
} |
94
|
|
|
|
95
|
4 |
|
return $this->inheritance; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
10 |
|
public function getName() : string |
102
|
|
|
{ |
103
|
10 |
|
return $this->name; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @throws LogicException |
108
|
|
|
* @throws SectionException |
109
|
|
|
* @throws InvalidArgumentException |
110
|
|
|
*/ |
111
|
2 |
|
public function delete() |
112
|
|
|
{ |
113
|
2 |
|
foreach ($this->getSelfTypeIterator() as $definition) { |
114
|
2 |
|
if ($definition->isHasInheritance() && $definition->getInheritance() === $this) { |
115
|
2 |
|
$definition->delete(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
parent::delete(); |
120
|
2 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $name |
124
|
|
|
* |
125
|
|
|
* @throws LogicException |
126
|
|
|
* @throws SectionException |
127
|
|
|
*/ |
128
|
|
|
private function defineName(string $name) |
129
|
13 |
|
{ |
130
|
|
|
foreach ($this->getSelfTypeIterator() as $definition) { |
131
|
13 |
|
if ($definition->getName() === $name) { |
132
|
|
|
throw new SectionException("Duplicate name {$name} found in {$this->getType()} section"); |
133
|
13 |
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->name = $name; |
137
|
13 |
|
} |
138
|
1 |
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $inheritance |
141
|
12 |
|
* |
142
|
10 |
|
* @throws SectionException |
143
|
10 |
|
* @throws LogicException |
144
|
|
|
*/ |
145
|
|
|
private function setParent(string $inheritance) |
146
|
|
|
{ |
147
|
12 |
|
foreach ($this->getSelfTypeIterator() as $definition) { |
148
|
12 |
|
if ($definition->getName() === $inheritance) { |
149
|
|
|
$this->inheritance = $definition; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (!$this->isHasInheritance()) { |
154
|
|
|
throw new SectionException("Inheritance with name {$inheritance} of section {$this->getType()} doesn't exists in configuration"); |
155
|
|
|
} |
156
|
|
|
} |
157
|
9 |
|
|
158
|
|
|
/** |
159
|
9 |
|
* @param string $name |
160
|
|
|
* |
161
|
9 |
|
* @return string |
162
|
1 |
|
* @throws \InvalidArgumentException |
163
|
|
|
* @throws SectionException |
164
|
|
|
*/ |
165
|
8 |
|
private function sanitizeName(string $name) : string |
166
|
8 |
|
{ |
167
|
8 |
|
$name = trim($name); |
168
|
|
|
|
169
|
|
|
if (empty($name)) { |
170
|
|
|
throw new SectionException("Name or inheritance of section {$this->getType()} can't be empty."); |
171
|
8 |
|
} |
172
|
2 |
|
|
173
|
|
|
if (!$this->isValidName($name)) { |
174
|
6 |
|
throw new SectionException("Name or inheritance of section {$this->getType()} must contains only A-Za-z and _ symbols"); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $name; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
13 |
|
* @param $name |
182
|
|
|
* |
183
|
13 |
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
private function isValidName($name) : bool |
186
|
|
|
{ |
187
|
|
|
return (bool)preg_match("/^[A-Za-z_\d]*$/", $name); |
188
|
|
|
} |
189
|
|
|
|
190
|
12 |
|
/** |
191
|
|
|
* @return Definition[] |
192
|
12 |
|
* @throws LogicException |
193
|
12 |
|
*/ |
194
|
7 |
|
private function getSelfTypeIterator() |
195
|
7 |
|
{ |
196
|
9 |
|
switch ($this->getType()) { |
197
|
9 |
|
case eSection::INDEX: |
198
|
9 |
|
$iterator = $this->getConfiguration()->iterateIndex(); |
199
|
|
|
break; |
200
|
|
|
case eSection::SOURCE: |
201
|
|
|
$iterator = $this->getConfiguration()->iterateSource(); |
202
|
|
|
break; |
203
|
12 |
|
default: |
204
|
|
|
throw new LogicException("Unknown type {$this->getType()}"); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $iterator; |
208
|
|
|
} |
209
|
|
|
} |