|
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 Nnx\FormComparator\Comparator\CollectionDiffService\HashElementBuilder; |
|
10
|
|
|
use Webmozart\Assert\Assert; |
|
11
|
|
|
use Zend\Form\Element\Collection; |
|
12
|
|
|
use Zend\Form\ElementInterface; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class CollectionDiffService |
|
17
|
|
|
* |
|
18
|
|
|
* @package Nnx\FormComparator\Comparator |
|
19
|
|
|
*/ |
|
20
|
|
|
class CollectionDiffService |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Компоент отвечающий за подготовку хеша элемента |
|
24
|
|
|
* |
|
25
|
|
|
* @var HashElementBuilder |
|
26
|
|
|
*/ |
|
27
|
|
|
private $hashElementBuilder; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Возвращает компоент отвечающий за подготовку хеша элемента |
|
31
|
|
|
* |
|
32
|
|
|
* @return HashElementBuilder |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getHashElementBuilder() |
|
35
|
|
|
{ |
|
36
|
|
|
if (null === $this->hashElementBuilder) { |
|
37
|
|
|
$this->hashElementBuilder = new HashElementBuilder(); |
|
38
|
|
|
} |
|
39
|
|
|
return $this->hashElementBuilder; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Устанавливает компоент отвечающий за подготовку хеша элемента |
|
44
|
|
|
* |
|
45
|
|
|
* @param HashElementBuilder $hashElementBuilder |
|
46
|
|
|
* |
|
47
|
|
|
* @return $this |
|
48
|
|
|
*/ |
|
49
|
|
|
public function setHashElementBuilder(HashElementBuilder $hashElementBuilder) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->hashElementBuilder = $hashElementBuilder; |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Получение расхождения элементов меджу двумя коллекциями |
|
59
|
|
|
* |
|
60
|
|
|
* @param Collection $sourceCollection |
|
61
|
|
|
* @param Collection $targetCollection |
|
62
|
|
|
* |
|
63
|
|
|
* @param $prefixPath |
|
64
|
|
|
* |
|
65
|
|
|
* @return mixed |
|
66
|
|
|
* @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function buildDiff(Collection $sourceCollection, Collection $targetCollection, $prefixPath) |
|
69
|
|
|
{ |
|
70
|
|
|
Assert::string($prefixPath); |
|
71
|
|
|
Assert::notEmpty($prefixPath); |
|
72
|
|
|
|
|
73
|
|
|
$sourceCollectionElementsHash = $this->buildHashForCollectionElements($sourceCollection); |
|
|
|
|
|
|
74
|
|
|
$targetCollectionElementsHash = $this->buildHashForCollectionElements($targetCollection); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param Collection $collection |
|
82
|
|
|
* |
|
83
|
|
|
* |
|
84
|
|
|
* @return array |
|
85
|
|
|
* @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function buildHashForCollectionElements(Collection $collection) |
|
88
|
|
|
{ |
|
89
|
|
|
$order = 0; |
|
90
|
|
|
$hashForCollectionElements = []; |
|
91
|
|
|
$hashElementBuilder = $this->getHashElementBuilder(); |
|
92
|
|
|
|
|
93
|
|
|
foreach ($collection->getIterator() as $elementOrFieldset) { |
|
94
|
|
|
/** @var ElementInterface $elementOrFieldset */ |
|
95
|
|
|
Assert::isInstanceOf($elementOrFieldset, ElementInterface::class); |
|
96
|
|
|
$hashForCollectionElements[$order] = $hashElementBuilder->hash($elementOrFieldset); |
|
97
|
|
|
$order++; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $hashForCollectionElements; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.