1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/form-comparator |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\FormComparator\Comparator\CollectionDiffService; |
7
|
|
|
|
8
|
|
|
use Zend\Form\Element\Hidden; |
9
|
|
|
use Zend\Form\ElementInterface; |
10
|
|
|
use Zend\Form\FieldsetInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class HashElementBuilder |
14
|
|
|
* |
15
|
|
|
* @package Nnx\FormComparator\Comparator\CollectionDiffService |
16
|
|
|
*/ |
17
|
|
|
class HashElementBuilder |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Набор данных из которых строится хеш |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $hashParts = []; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param ElementInterface $element |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
* @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
33
|
|
|
*/ |
34
|
|
|
public function hash(ElementInterface $element) |
35
|
|
|
{ |
36
|
|
|
$this->hashParts = []; |
37
|
|
|
|
38
|
|
|
$this->buildHash($element); |
39
|
|
|
|
40
|
|
|
ksort($this->hashParts, SORT_STRING); |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
$hashStack = []; |
44
|
|
|
foreach ($this->hashParts as $key => $value) { |
45
|
|
|
$hashStack[] = $key . '=' . $value; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return implode('|', $hashStack); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ElementInterface $element |
54
|
|
|
* @param null|string $namePrefix |
55
|
|
|
* |
56
|
|
|
* @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
57
|
|
|
*/ |
58
|
|
|
protected function buildHash(ElementInterface $element, $namePrefix = null) |
59
|
|
|
{ |
60
|
|
|
if ($element instanceof Hidden) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
if (null === $namePrefix) { |
64
|
|
|
$elementName = $element->getName(); |
65
|
|
|
} else { |
66
|
|
|
$elementName = $namePrefix . '.' . $element->getName(); |
67
|
|
|
} |
68
|
|
|
if ($element instanceof FieldsetInterface) { |
69
|
|
|
foreach ($element->getIterator() as $childElementOrFieldset) { |
70
|
|
|
$this->buildHash($childElementOrFieldset, $elementName); |
71
|
|
|
} |
72
|
|
|
} else { |
73
|
|
|
$this->hashParts[$elementName] = $this->normalizeValue($element->getValue()); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Представлет значение элемента формы в виде строки |
79
|
|
|
* |
80
|
|
|
* @param $rawValue |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
* @throws \Nnx\FormComparator\Comparator\CollectionDiffService\Exception\RuntimeException |
84
|
|
|
*/ |
85
|
|
|
protected function normalizeValue($rawValue) |
86
|
|
|
{ |
87
|
|
|
|
88
|
|
|
if ($rawValue instanceof \DateTimeInterface) { |
89
|
|
|
$value = (string)$rawValue->getTimestamp(); |
90
|
|
|
} else { |
91
|
|
|
$value = $rawValue; |
92
|
|
|
$resultConvert = @settype($value, 'string'); |
93
|
|
|
if (false === $resultConvert) { |
94
|
|
|
throw new Exception\RuntimeException('Invalid type convert'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $value; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|