1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/jms-serializer-module |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\JmsSerializerModule\DataContainerBuilder\XmlBuilder; |
7
|
|
|
|
8
|
|
|
use Nnx\JmsSerializerModule\DataContainer\EntityInterface; |
9
|
|
|
use SimpleXMLElement; |
10
|
|
|
use Nnx\JmsSerializerModule\DataContainer\DataContainerInterface; |
11
|
|
|
use Nnx\JmsSerializerModule\DataContainer\Index; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ParserContext |
15
|
|
|
* |
16
|
|
|
* @package Nnx\JmsSerializerModule\DataContainerBuilder\XmlBuilder |
17
|
|
|
*/ |
18
|
|
|
class ParserContext |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var SimpleXMLElement[] |
22
|
|
|
*/ |
23
|
|
|
protected $itemNodes; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Устанавливает связь, с контейнером в котором содержаться данные для родительской сущности |
27
|
|
|
* |
28
|
|
|
* @var EntityInterface |
29
|
|
|
*/ |
30
|
|
|
protected $parentEntity; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Уровень вложенности сущности в xml дереве, на котором находится обрабатываемый узел |
34
|
|
|
* |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
protected $level = 0; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Имя связи, которая связывает контейнер с данные для сущности, с контейнером данных, родительской сущности |
41
|
|
|
* |
42
|
|
|
* @var string|null |
43
|
|
|
*/ |
44
|
|
|
protected $parentAssociation; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Контейнер с результирующими данными |
48
|
|
|
* |
49
|
|
|
* @var DataContainerInterface |
50
|
|
|
*/ |
51
|
|
|
protected $dataContainer; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Устанавливает индекс |
55
|
|
|
* |
56
|
|
|
* @var Index |
57
|
|
|
*/ |
58
|
|
|
protected $index; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Набор нод для парсинга |
62
|
|
|
* |
63
|
|
|
* @return SimpleXMLElement[] |
64
|
|
|
*/ |
65
|
|
|
public function getItemNodes() |
66
|
|
|
{ |
67
|
|
|
return $this->itemNodes; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Устанавливает набор нод для парсинга |
72
|
|
|
* |
73
|
|
|
* @param array $itemNodes |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function setItemNodes(array $itemNodes) |
78
|
|
|
{ |
79
|
|
|
$this->itemNodes = $itemNodes; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Возвращает связь, с контейнером в котором содержаться данные для родительской сущности |
86
|
|
|
* |
87
|
|
|
* @return EntityInterface |
88
|
|
|
*/ |
89
|
|
|
public function getParentEntity() |
90
|
|
|
{ |
91
|
|
|
return $this->parentEntity; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Устанавливает связь, с контейнером в котором содержаться данные для родительской сущности |
96
|
|
|
* |
97
|
|
|
* @param EntityInterface $parentEntity |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function setParentEntity(EntityInterface $parentEntity) |
102
|
|
|
{ |
103
|
|
|
$this->parentEntity = $parentEntity; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Возвращает уровень вложенности сущности в xml дереве, на котором находится обрабатываемый узел |
110
|
|
|
* |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
public function getLevel() |
114
|
|
|
{ |
115
|
|
|
return $this->level; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Устанавливает уровень вложенности сущности в xml дереве, на котором находится обрабатываемый узел |
120
|
|
|
* |
121
|
|
|
* @param int $level |
122
|
|
|
* |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
|
|
public function setLevel($level) |
126
|
|
|
{ |
127
|
|
|
$this->level = (integer)$level; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Возвращает имя связи, которая связывает контейнер с данные для сущности, с контейнером данных, родительской сущности |
134
|
|
|
* |
135
|
|
|
* @return null|string |
136
|
|
|
*/ |
137
|
|
|
public function getParentAssociation() |
138
|
|
|
{ |
139
|
|
|
return $this->parentAssociation; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Устанавливает имя связи, которая связывает контейнер с данные для сущности, с контейнером данных, родительской сущности |
144
|
|
|
* |
145
|
|
|
* @param null|string $parentAssociation |
146
|
|
|
* |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function setParentAssociation($parentAssociation) |
150
|
|
|
{ |
151
|
|
|
$this->parentAssociation = $parentAssociation; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Проверка корректного состояния контекста |
158
|
|
|
* |
159
|
|
|
* @throws \Nnx\JmsSerializerModule\DataContainerBuilder\XmlBuilder\Exception\InvalidParserContextException |
160
|
|
|
*/ |
161
|
|
|
public function validate() |
162
|
|
|
{ |
163
|
|
|
$parentAssociation = $this->getParentAssociation(); |
164
|
|
|
$parentEntity = $this->getParentEntity(); |
165
|
|
|
|
166
|
|
View Code Duplication |
if (null !== $parentAssociation && null === $parentEntity) { |
|
|
|
|
167
|
|
|
$errMsg = 'Parent entity not specified'; |
168
|
|
|
throw new Exception\InvalidParserContextException($errMsg); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
View Code Duplication |
if (null === $parentAssociation && null !== $parentEntity) { |
|
|
|
|
172
|
|
|
$errMsg = 'Parent association not specified'; |
173
|
|
|
throw new Exception\InvalidParserContextException($errMsg); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
if (null === $this->getDataContainer()) { |
178
|
|
|
$errMsg = 'Data container not specified'; |
179
|
|
|
throw new Exception\InvalidParserContextException($errMsg); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (null === $this->getIndex()) { |
183
|
|
|
$errMsg = 'Index storage not specified'; |
184
|
|
|
throw new Exception\InvalidParserContextException($errMsg); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Возвращает контейнер с результирующими данными |
190
|
|
|
* |
191
|
|
|
* @return DataContainerInterface |
192
|
|
|
*/ |
193
|
|
|
public function getDataContainer() |
194
|
|
|
{ |
195
|
|
|
return $this->dataContainer; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Устанавливает контейнер с результирующими данными |
200
|
|
|
* |
201
|
|
|
* @param DataContainerInterface $dataContainer |
202
|
|
|
* |
203
|
|
|
* @return $this |
204
|
|
|
*/ |
205
|
|
|
public function setDataContainer(DataContainerInterface $dataContainer) |
206
|
|
|
{ |
207
|
|
|
$this->dataContainer = $dataContainer; |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Возвращает хранилище индексов |
214
|
|
|
* |
215
|
|
|
* @return Index |
216
|
|
|
*/ |
217
|
|
|
public function getIndex() |
218
|
|
|
{ |
219
|
|
|
return $this->index; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Устанавливает хранилище индексов |
224
|
|
|
* |
225
|
|
|
* @param Index $index |
226
|
|
|
* |
227
|
|
|
* @return $this |
228
|
|
|
*/ |
229
|
|
|
public function setIndex(Index $index) |
230
|
|
|
{ |
231
|
|
|
$this->index = $index; |
232
|
|
|
|
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
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.