Completed
Push — master ( fe8dc9...ddccce )
by Andrey
02:35
created

DiffBuilder::build()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 3
nop 0
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
use Webmozart\Assert\Assert;
8
use Zend\Form\ElementInterface;
9
use Zend\Form\FormInterface;
10
11
/**
12
 * Class DiffBuilder
13
 *
14
 * @package Nnx\FormComparator\Comparator
15
 */
16
class DiffBuilder
17
{
18
    /**
19
     * Элемент был создан
20
     *
21
     * @var string
22
     */
23
    const INSERT_ELEMENT_MODE = 'insertElement';
24
25
    /**
26
     * Элемент был удален
27
     *
28
     * @var string
29
     */
30
    const DELETE_ELEMENT_MODE = 'deleteElement';
31
32
    /**
33
     * Элемент был изменен
34
     *
35
     * @var string
36
     */
37
    const UPDATE_ELEMENT_MODE = 'updateElement';
38
39
    /**
40
     * Элемент был создан
41
     *
42
     * @var string
43
     */
44
    const INSERT_COLLECTION_MODE = 'insertCollection';
45
46
    /**
47
     * Элемент был удален
48
     *
49
     * @var string
50
     */
51
    const DELETE_COLLECTION_MODE = 'deleteCollection';
52
53
    /**
54
     * Элемент был изменен
55
     *
56
     * @var string
57
     */
58
    const UPDATE_COLLECTION_MODE = 'updateCollection';
59
60
    /**
61
     * Определяет какой объект создавать
62
     *
63
     * @var string
64
     */
65
    private $mode;
66
67
    /**
68
     * Путь до элемента
69
     *
70
     * @var string
71
     */
72
    private $pathToElement;
73
74
    /**
75
     * Заголовок элемента который сравнивают
76
     *
77
     * @var string
78
     */
79
    private $sourceLabel;
80
81
    /**
82
     * Значение элемента который сравнивают
83
     *
84
     * @var mixed
85
     */
86
    private $sourceValue;
87
88
    /**
89
     * Значение элемента с которым сравнивают
90
     *
91
     * @var mixed
92
     */
93
    private $targetValue;
94
95
    /**
96
     * Форма которую сравнивают
97
     *
98
     * @var FormInterface
99
     */
100
    private $sourceForm;
101
102
    /**
103
     * Форма с которой сравнивают
104
     *
105
     * @var FormInterface
106
     */
107
    private $targetForm;
108
109
    /**
110
     * Элемент который сравнивают
111
     *
112
     * @var ElementInterface
113
     */
114
    private $sourceElement;
115
116
    /**
117
     * Элемент с которым сравнивают
118
     *
119
     * @var ElementInterface
120
     */
121
    private $targetElement;
122
123
    /**
124
     * Допустимые режимы
125
     *
126
     * @var array
127
     */
128
    private $accessMode = [
129
        self::DELETE_ELEMENT_MODE => self::DELETE_ELEMENT_MODE,
130
        self::UPDATE_ELEMENT_MODE => self::UPDATE_ELEMENT_MODE,
131
        self::INSERT_ELEMENT_MODE => self::INSERT_ELEMENT_MODE,
132
    ];
133
134
    /**
135
     * DiffBuilder constructor.
136
     *
137
     * @param string $mode
138
     */
139
    public function __construct($mode)
140
    {
141
        Assert::keyExists($this->accessMode, $mode);
142
        $this->mode = $mode;
143
    }
144
145
    /**
146
     * Элемент который сравнивают
147
     *
148
     * @return ElementInterface
149
     */
150
    public function getSourceElement()
151
    {
152
        return $this->sourceElement;
153
    }
154
155
    /**
156
     * Элемент с которым сравнивают
157
     *
158
     * @return ElementInterface
159
     */
160
    public function getTargetElement()
161
    {
162
        return $this->targetElement;
163
    }
164
165
    /**
166
     *  Устанавливает элемент который сравнивают
167
     *
168
     * @param ElementInterface $sourceElement
169
     *
170
     * @return $this
171
     */
172
    public function setSourceElement($sourceElement)
173
    {
174
        $this->sourceElement = $sourceElement;
175
176
        return $this;
177
    }
178
179
    /**
180
     * Возвращает элемент который сравнивают
181
     *
182
     * @param ElementInterface $targetElement
183
     *
184
     * @return $this
185
     */
186
    public function setTargetElement($targetElement)
187
    {
188
        $this->targetElement = $targetElement;
189
190
        return $this;
191
    }
192
193
    /**
194
     * Возвращает форму которую сравнивают
195
     *
196
     * @return FormInterface
197
     */
198
    public function getSourceForm()
199
    {
200
        return $this->sourceForm;
201
    }
202
203
    /**
204
     * Возвращает форму с которой сравнивают
205
     *
206
     * @return FormInterface
207
     */
208
    public function getTargetForm()
209
    {
210
        return $this->targetForm;
211
    }
212
213
    /**
214
     * Устанавливает форма которую сравнивают
215
     *
216
     * @param FormInterface $sourceForm
217
     *
218
     * @return $this
219
     */
220
    public function setSourceForm(FormInterface $sourceForm)
221
    {
222
        $this->sourceForm = $sourceForm;
223
224
        return $this;
225
    }
226
227
    /**
228
     * Устанавливает форму с которой сравнивают
229
     *
230
     * @param FormInterface $targetForm
231
     *
232
     * @return $this
233
     */
234
    public function setTargetForm(FormInterface $targetForm)
235
    {
236
        $this->targetForm = $targetForm;
237
238
        return $this;
239
    }
240
241
242
243
    /**
244
     * Путь до элемента
245
     *
246
     * @return string
247
     */
248
    public function getPathToElement()
249
    {
250
        return $this->pathToElement;
251
    }
252
253
    /**
254
     * Устанавливает путь до элемента
255
     *
256
     * @param string $pathToElement
257
     *
258
     * @return $this
259
     */
260
    public function setPathToElement($pathToElement)
261
    {
262
        $this->pathToElement = $pathToElement;
263
264
        return $this;
265
    }
266
267
    /**
268
     * Заголовок элемента который сравнивают
269
     *
270
     * @return string
271
     */
272
    public function getSourceLabel()
273
    {
274
        return $this->sourceLabel;
275
    }
276
277
    /**
278
     * Устанавливают заголовок элемента который сравнивают
279
     *
280
     * @param string $sourceLabel
281
     *
282
     * @return $this
283
     */
284
    public function setSourceLabel($sourceLabel)
285
    {
286
        $this->sourceLabel = $sourceLabel;
287
288
        return $this;
289
    }
290
291
    /**
292
     * Значение элемента который сравнивают
293
     *
294
     * @return mixed
295
     */
296
    public function getSourceValue()
297
    {
298
        return $this->sourceValue;
299
    }
300
301
    /**
302
     * Устанавливает значение элемента который сравнивают
303
     *
304
     * @param mixed $sourceValue
305
     *
306
     * @return $this
307
     */
308
    public function setSourceValue($sourceValue)
309
    {
310
        $this->sourceValue = $sourceValue;
311
312
        return $this;
313
    }
314
315
    /**
316
     * Возвращают значение элемента с которым сравнивают
317
     *
318
     * @return mixed
319
     */
320
    public function getTargetValue()
321
    {
322
        return $this->targetValue;
323
    }
324
325
    /**
326
     * Устанавливают значение элемента с которым сравнивают
327
     *
328
     * @param mixed $targetValue
329
     *
330
     * @return $this
331
     */
332
    public function setTargetValue($targetValue)
333
    {
334
        $this->targetValue = $targetValue;
335
336
        return $this;
337
    }
338
339
    /**
340
     * Создает объект описывающий различие между двумя элементами формы
341
     *
342
     * @return AbstractDiff
343
     */
344
    public function build()
345
    {
346
        if (self::DELETE_ELEMENT_MODE === $this->mode) {
347
            $diff = new Diff\DeleteElement($this);
348
        } elseif (self::INSERT_ELEMENT_MODE === $this->mode) {
349
            $diff = new Diff\InsertElement($this);
350
        } else {
351
            $diff = new Diff\UpdateElement($this);
352
        }
353
354
        return $diff;
355
    }
356
357
    /**
358
     * Определяет какой объект создавать
359
     *
360
     * @return string
361
     */
362
    public function getMode()
363
    {
364
        return $this->mode;
365
    }
366
}
367