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