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

HashElementBuilder::hash()   A

Complexity

Conditions 2
Paths 2

Size

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