|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author: Viskov Sergey |
|
4
|
|
|
* @date : 19.03.16 |
|
5
|
|
|
* @time : 1:54 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace LTDBeget\sphinx\configurator\deserializers; |
|
9
|
|
|
|
|
10
|
|
|
use LTDBeget\sphinx\configurator\Configuration; |
|
11
|
|
|
use LTDBeget\sphinx\configurator\configurationEntities\base\Section; |
|
12
|
|
|
use LTDBeget\sphinx\configurator\exceptions\DeserializeException; |
|
13
|
|
|
use LTDBeget\sphinx\enums\base\eOption; |
|
14
|
|
|
use LTDBeget\sphinx\enums\eSection; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class ArrayDeserializer |
|
18
|
|
|
* Serialize correct array to Configuration object |
|
19
|
|
|
* |
|
20
|
|
|
* @package LTDBeget\sphinx\configurator\deserializers |
|
21
|
|
|
*/ |
|
22
|
|
|
final class ArrayDeserializer |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Make Configuration object from array serialized content of sphinx configuration file |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $arrayConfiguration |
|
28
|
|
|
* @param Configuration $objectConfiguration |
|
29
|
|
|
* |
|
30
|
|
|
* @return Configuration |
|
31
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\SectionException |
|
32
|
|
|
* @throws \InvalidArgumentException |
|
33
|
|
|
* @throws \LogicException |
|
34
|
|
|
* @throws \BadMethodCallException |
|
35
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\ConfigurationException |
|
36
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\DeserializeException |
|
37
|
|
|
*/ |
|
38
|
8 |
|
public static function deserialize(array $arrayConfiguration, Configuration $objectConfiguration) : Configuration |
|
39
|
|
|
{ |
|
40
|
8 |
|
$serializer = new self(); |
|
41
|
8 |
|
$serializer->arrayConfiguration = $arrayConfiguration; |
|
42
|
8 |
|
$serializer->objectConfiguration = $objectConfiguration; |
|
43
|
|
|
|
|
44
|
8 |
|
return $serializer->deserializeInternal(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @internal |
|
49
|
|
|
* ArrayDeserializer constructor. |
|
50
|
|
|
*/ |
|
51
|
8 |
|
private function __construct() |
|
52
|
|
|
{ |
|
53
|
8 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @internal |
|
57
|
|
|
* @return Configuration |
|
58
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\SectionException |
|
59
|
|
|
* @throws \LogicException |
|
60
|
|
|
* @throws \InvalidArgumentException |
|
61
|
|
|
* @throws \BadMethodCallException |
|
62
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\ConfigurationException |
|
63
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\DeserializeException |
|
64
|
|
|
*/ |
|
65
|
8 |
|
private function deserializeInternal() : Configuration |
|
66
|
|
|
{ |
|
67
|
8 |
|
foreach ($this->arrayConfiguration as $section) { |
|
68
|
8 |
|
$sectionObject = $this->deserializeSection($section); |
|
69
|
|
|
|
|
70
|
8 |
|
if (!array_key_exists('options', $section)) { |
|
71
|
2 |
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
6 |
|
$this->deserializeOptions($section['options'], $sectionObject); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
3 |
|
return $this->objectConfiguration; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @internal |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $section_data |
|
84
|
|
|
* |
|
85
|
|
|
* @return Section |
|
86
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\SectionException |
|
87
|
|
|
* @throws \LogicException |
|
88
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\DeserializeException |
|
89
|
|
|
* @throws \InvalidArgumentException |
|
90
|
|
|
* @throws \BadMethodCallException |
|
91
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\ConfigurationException |
|
92
|
|
|
*/ |
|
93
|
8 |
|
private function deserializeSection(array $section_data) : Section |
|
94
|
|
|
{ |
|
95
|
8 |
|
$type = $section_data['type'] ?? NULL; |
|
96
|
8 |
|
$name = $section_data['name'] ?? NULL; |
|
97
|
8 |
|
$inheritance = $section_data['inheritance'] ?? NULL; |
|
98
|
|
|
|
|
99
|
|
|
switch ($type) { |
|
100
|
8 |
|
case eSection::INDEXER: |
|
101
|
4 |
|
$section = $this->objectConfiguration->getIndexer(); |
|
102
|
4 |
|
break; |
|
103
|
8 |
|
case eSection::SEARCHD: |
|
104
|
4 |
|
$section = $this->objectConfiguration->getSearchd(); |
|
105
|
4 |
|
break; |
|
106
|
8 |
|
case eSection::COMMON: |
|
107
|
4 |
|
$section = $this->objectConfiguration->getCommon(); |
|
108
|
3 |
|
break; |
|
109
|
8 |
|
case eSection::SOURCE: |
|
110
|
6 |
|
$section = $this->objectConfiguration->addSource($name, $inheritance); |
|
111
|
6 |
|
break; |
|
112
|
6 |
|
case eSection::INDEX: |
|
113
|
6 |
|
$section = $this->objectConfiguration->addIndex($name, $inheritance); |
|
114
|
6 |
|
break; |
|
115
|
|
|
default: |
|
116
|
|
|
throw new DeserializeException('Unknown section type'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
8 |
|
return $section; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @internal |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $options |
|
126
|
|
|
* @param Section $section |
|
127
|
|
|
* |
|
128
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\DeserializeException |
|
129
|
|
|
* @throws \InvalidArgumentException |
|
130
|
|
|
* @throws \LogicException |
|
131
|
|
|
*/ |
|
132
|
6 |
|
private function deserializeOptions(array $options, Section $section) |
|
133
|
|
|
{ |
|
134
|
6 |
|
foreach ($options as $option) { |
|
135
|
|
|
|
|
136
|
6 |
|
if (!array_key_exists('name', $option)) { |
|
137
|
|
|
throw new DeserializeException('Wrong array format. All options must contain name.'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
6 |
|
if (!array_key_exists('value', $option)) { |
|
141
|
|
|
throw new DeserializeException('Wrong array format. All options must contain value.'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
6 |
|
$optionName = $option['name']; |
|
145
|
6 |
|
$optionValue = $option['value']; |
|
146
|
6 |
|
$this->deserializeOption($section, $optionName, $optionValue); |
|
147
|
|
|
} |
|
148
|
6 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param Section $section |
|
152
|
|
|
* @param $optionName |
|
153
|
|
|
* @param $optionValue |
|
154
|
|
|
* |
|
155
|
|
|
* @throws \InvalidArgumentException |
|
156
|
|
|
* @throws \LogicException |
|
157
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\DeserializeException |
|
158
|
|
|
*/ |
|
159
|
6 |
|
private function deserializeOption(Section $section, $optionName, $optionValue) |
|
160
|
|
|
{ |
|
161
|
6 |
|
$sectionType = $section->getType(); |
|
162
|
6 |
|
$informer = $section->getConfiguration()->getInformer(); |
|
163
|
6 |
|
$optionName = $this->getOptionName($section, $optionName); |
|
164
|
|
|
|
|
165
|
6 |
|
if ($informer->isKnownOption($sectionType, $optionName)) { |
|
166
|
6 |
|
$section->addOption($optionName, $optionValue); |
|
167
|
3 |
|
} elseif ($informer->isRemovedOption($sectionType, $optionName)) { |
|
168
|
3 |
|
return; |
|
169
|
|
|
} else { |
|
170
|
|
|
throw new DeserializeException("Unknown option name {$optionName} in section {$section->getType()}"); |
|
171
|
|
|
} |
|
172
|
6 |
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @internal |
|
176
|
|
|
* |
|
177
|
|
|
* @param Section $section |
|
178
|
|
|
* @param string $name |
|
179
|
|
|
* |
|
180
|
|
|
* @return eOption |
|
181
|
|
|
* @throws \LogicException |
|
182
|
|
|
* @throws \InvalidArgumentException |
|
183
|
|
|
* @throws \LTDBeget\sphinx\configurator\exceptions\DeserializeException |
|
184
|
|
|
*/ |
|
185
|
6 |
|
private function getOptionName(Section $section, string $name) : eOption |
|
186
|
|
|
{ |
|
187
|
|
|
try { |
|
188
|
|
|
/** |
|
189
|
|
|
* @var eOption $optionClass |
|
190
|
|
|
*/ |
|
191
|
6 |
|
$optionClass = $this->getOptionNameClass($section->getType()); |
|
192
|
6 |
|
$option = $optionClass::get($name); |
|
193
|
|
|
} catch (\InvalidArgumentException $e) { |
|
194
|
|
|
throw new DeserializeException("Unknown section type {$section->getType()}"); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
6 |
|
return $option; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param eSection $section |
|
202
|
|
|
* |
|
203
|
|
|
* @return string |
|
204
|
|
|
*/ |
|
205
|
6 |
|
private function getOptionNameClass(eSection $section) : string |
|
206
|
|
|
{ |
|
207
|
6 |
|
$sectionName = ucfirst((string) $section); |
|
208
|
|
|
|
|
209
|
6 |
|
return "LTDBeget\\sphinx\\enums\\options\\e{$sectionName}Option"; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @var array |
|
214
|
|
|
*/ |
|
215
|
|
|
private $arrayConfiguration = []; |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @var Configuration |
|
219
|
|
|
*/ |
|
220
|
|
|
private $objectConfiguration; |
|
221
|
|
|
} |