1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/form-comparator |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\FormComparator\Comparator; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use Webmozart\Assert\Assert; |
10
|
|
|
use Zend\Form\ElementInterface; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class DiffCollectionElementBuilder |
15
|
|
|
* |
16
|
|
|
* @package Nnx\FormComparator\Comparator |
17
|
|
|
*/ |
18
|
|
|
class DiffCollectionElementBuilder |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Элемент был создан |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
const INSERT_ELEMENT_MODE = 'insertElement'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Элемент был удален |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
const DELETE_ELEMENT_MODE = 'deleteElement'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Элемент был изменен |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
const UPDATE_ELEMENT_MODE = 'updateElement'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Порядковый номер элмента коллекции |
43
|
|
|
* |
44
|
|
|
* @var integer |
45
|
|
|
*/ |
46
|
|
|
private $rowIndex; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Уникальный идендфификатор строки коллекци |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $uniqueRowId; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Хеш значения элемента коллекции который сравнивают |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
private $sourceHash; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Хеш значения элемента коллекции с которым сравнивают |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
private $targetHash; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Элемент коллекции который сравнивают |
71
|
|
|
* |
72
|
|
|
* @var ElementInterface |
73
|
|
|
*/ |
74
|
|
|
private $sourceCollectionElement; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Элемент коллекции с которым сравнивают |
78
|
|
|
* |
79
|
|
|
* @var ElementInterface |
80
|
|
|
*/ |
81
|
|
|
private $targetCollectionElement; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Определяет какой объект создавать |
85
|
|
|
* |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
private $mode; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Путь до элемента |
92
|
|
|
* |
93
|
|
|
* @var string |
94
|
|
|
*/ |
95
|
|
|
private $pathToElement; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Допустимые режимы |
99
|
|
|
* |
100
|
|
|
* @var array |
101
|
|
|
*/ |
102
|
|
|
private $accessMode = [ |
103
|
|
|
self::DELETE_ELEMENT_MODE => self::DELETE_ELEMENT_MODE, |
104
|
|
|
self::UPDATE_ELEMENT_MODE => self::UPDATE_ELEMENT_MODE, |
105
|
|
|
self::INSERT_ELEMENT_MODE => self::INSERT_ELEMENT_MODE, |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* DiffBuilder constructor. |
110
|
|
|
* |
111
|
|
|
* @param string $mode |
112
|
|
|
*/ |
113
|
|
|
public function __construct($mode) |
114
|
|
|
{ |
115
|
|
|
Assert::keyExists($this->accessMode, $mode); |
116
|
|
|
$this->mode = $mode; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Создает объект описывающий различие между двумя элементами формы |
121
|
|
|
* |
122
|
|
|
* @return AbstractDiff |
123
|
|
|
* @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
124
|
|
|
* @throws \Nnx\FormComparator\Comparator\Exception\DomainException |
125
|
|
|
*/ |
126
|
|
|
public function build() |
127
|
|
|
{ |
128
|
|
|
switch ($this->mode) { |
129
|
|
|
case self::DELETE_ELEMENT_MODE: |
130
|
|
|
$diff = new Diff\DeletedCollectionElement($this); |
131
|
|
|
break; |
132
|
|
|
case self::INSERT_ELEMENT_MODE: |
133
|
|
|
$diff = new Diff\InsertCollectionElement($this); |
134
|
|
|
break; |
135
|
|
|
case self::UPDATE_ELEMENT_MODE: |
136
|
|
|
$diff = new Diff\UpdateCollectionElement($this); |
137
|
|
|
break; |
138
|
|
|
default: |
139
|
|
|
throw new Exception\DomainException(sprintf('Unknown mode %s', $this->mode)); |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $diff; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Определяет какой объект создавать |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function getMode() |
152
|
|
|
{ |
153
|
|
|
Assert::string($this->mode); |
154
|
|
|
Assert::notEmpty($this->mode); |
155
|
|
|
return $this->mode; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Порядковый номер элмента коллекции |
161
|
|
|
* |
162
|
|
|
* @return int |
163
|
|
|
*/ |
164
|
|
|
public function getRowIndex() |
165
|
|
|
{ |
166
|
|
|
Assert::integer($this->rowIndex); |
167
|
|
|
return $this->rowIndex; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Уникальный идендфификатор строки коллекци |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public function getUniqueRowId() |
176
|
|
|
{ |
177
|
|
|
Assert::numeric($this->uniqueRowId); |
178
|
|
|
return $this->uniqueRowId; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Хеш значения элемента коллекции который сравнивают |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function getSourceHash() |
187
|
|
|
{ |
188
|
|
|
Assert::string($this->sourceHash); |
189
|
|
|
Assert::notEmpty($this->sourceHash); |
190
|
|
|
return $this->sourceHash; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Элемент коллекции который сравнивают |
195
|
|
|
* |
196
|
|
|
* @return ElementInterface |
197
|
|
|
*/ |
198
|
|
|
public function getSourceCollectionElement() |
199
|
|
|
{ |
200
|
|
|
Assert::isInstanceOf($this->sourceCollectionElement, ElementInterface::class); |
201
|
|
|
return $this->sourceCollectionElement; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Путь до элемента |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public function getPathToElement() |
211
|
|
|
{ |
212
|
|
|
Assert::string($this->pathToElement); |
213
|
|
|
Assert::notEmpty($this->pathToElement); |
214
|
|
|
return $this->pathToElement; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Уникальный идендфификатор строки коллекци |
219
|
|
|
* |
220
|
|
|
* @param string $uniqueRowId |
221
|
|
|
* |
222
|
|
|
* @return $this |
223
|
|
|
*/ |
224
|
|
|
public function setUniqueRowId($uniqueRowId) |
225
|
|
|
{ |
226
|
|
|
$this->uniqueRowId = $uniqueRowId; |
227
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Хеш значения элемента коллекции который сравнивают |
233
|
|
|
* |
234
|
|
|
* @param string $sourceHash |
235
|
|
|
* |
236
|
|
|
* @return $this |
237
|
|
|
*/ |
238
|
|
|
public function setSourceHash($sourceHash) |
239
|
|
|
{ |
240
|
|
|
$this->sourceHash = $sourceHash; |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Устанавливает элемент коллекции который сравнивают |
247
|
|
|
* |
248
|
|
|
* @param ElementInterface $sourceCollectionElement |
249
|
|
|
* |
250
|
|
|
* @return $this |
251
|
|
|
*/ |
252
|
|
|
public function setSourceCollectionElement(ElementInterface $sourceCollectionElement) |
253
|
|
|
{ |
254
|
|
|
$this->sourceCollectionElement = $sourceCollectionElement; |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Путь до элемента |
261
|
|
|
* |
262
|
|
|
* @param string $pathToElement |
263
|
|
|
* |
264
|
|
|
* @return $this |
265
|
|
|
*/ |
266
|
|
|
public function setPathToElement($pathToElement) |
267
|
|
|
{ |
268
|
|
|
$this->pathToElement = $pathToElement; |
269
|
|
|
|
270
|
|
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Порядковый номер элмента коллекции |
275
|
|
|
* |
276
|
|
|
* @param int $rowIndex |
277
|
|
|
* |
278
|
|
|
* @return $this |
279
|
|
|
*/ |
280
|
|
|
public function setRowIndex($rowIndex) |
281
|
|
|
{ |
282
|
|
|
$this->rowIndex = $rowIndex; |
283
|
|
|
|
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Хеш значения элемента коллекции с которым сравнивают |
289
|
|
|
* |
290
|
|
|
* @return string |
291
|
|
|
*/ |
292
|
|
|
public function getTargetHash() |
293
|
|
|
{ |
294
|
|
|
return $this->targetHash; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Хеш значения элемента коллекции с которым сравнивают |
299
|
|
|
* |
300
|
|
|
* @param string $targetHash |
301
|
|
|
* |
302
|
|
|
* @return $this |
303
|
|
|
*/ |
304
|
|
|
public function setTargetHash($targetHash) |
305
|
|
|
{ |
306
|
|
|
$this->targetHash = $targetHash; |
307
|
|
|
|
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Элемент коллекции с которым сравнивают |
313
|
|
|
* |
314
|
|
|
* @return ElementInterface |
315
|
|
|
*/ |
316
|
|
|
public function getTargetCollectionElement() |
317
|
|
|
{ |
318
|
|
|
return $this->targetCollectionElement; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Устанавливает элемент коллекции с которым сравнивают |
323
|
|
|
* |
324
|
|
|
* @param ElementInterface $targetCollectionElement |
325
|
|
|
* |
326
|
|
|
* @return $this |
327
|
|
|
*/ |
328
|
|
|
public function setTargetCollectionElement(ElementInterface $targetCollectionElement) |
329
|
|
|
{ |
330
|
|
|
$this->targetCollectionElement = $targetCollectionElement; |
331
|
|
|
|
332
|
|
|
return $this; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
|
336
|
|
|
|
337
|
|
|
|
338
|
|
|
} |
339
|
|
|
|