reverseTransform()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 8
c 1
b 1
f 0
nc 3
nop 1
dl 0
loc 15
rs 10
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Yves\FactFinderWebComponents\Form;
9
10
use Symfony\Component\Form\DataTransformerInterface;
11
12
class WebComponentConfigToTwigConfigTransformer implements DataTransformerInterface
13
{
14
    public const ITEMS_PARAMETER = 'items';
15
16
    /**
17
     * @inheritdoc
18
     *
19
     * @param mixed $value
20
     *
21
     * @return mixed|void
22
     */
23
    public function transform($value)
24
    {
25
        $result = [];
26
27
        if (is_array($value) === false) {
28
            return $result;
29
        }
30
31
        $result = $value;
32
        if (isset($value['properties'])) {
33
            $result['component'] = $this->transformFromArrayToHtmlAttributes($value['properties']);
34
            unset($result['properties']);
35
        }
36
37
        return $result;
38
    }
39
40
    /**
41
     * @param array $componentParameters
42
     *
43
     * @return string
44
     */
45
    protected function transformFromArrayToHtmlAttributes(array $componentParameters): string
46
    {
47
        $htmlAttributesString = '';
48
49
        foreach ($componentParameters as $itemKey => $itemValue) {
50
            if (is_bool($itemValue) === false) {
51
                $htmlAttributesString .= sprintf('%s="%s" ', $itemKey, $itemValue);
52
53
                continue;
54
            }
55
56
            if ($itemValue === true) {
57
                $htmlAttributesString .= sprintf('%s ', $itemKey);
58
            }
59
        }
60
61
        return trim($htmlAttributesString);
62
    }
63
64
    /**
65
     * @inheritdoc
66
     *
67
     * @param mixed $value
68
     *
69
     * @return mixed|void
70
     */
71
    public function reverseTransform($value)
72
    {
73
        $result = [];
74
75
        if (is_array($value) === false) {
76
            return $result;
77
        }
78
79
        $result = $value;
80
        if (isset($value['component']) && is_string($value['component'])) {
81
            $result['properties'] = $this->transformFropmHtmlAttributesToArray($result['component']);
82
            unset($result['component']);
83
        }
84
85
        return $result;
86
    }
87
88
    /**
89
     * @param string $attributes
90
     *
91
     * @return array
92
     */
93
    protected function transformFropmHtmlAttributesToArray(string $attributes): array
94
    {
95
        $properties = [];
96
97
        $attributeDataCollection = explode(' ', trim($attributes));
98
        foreach ($attributeDataCollection as $attributeDataString) {
99
            if (preg_match('/([^=]+)=([^ ]+)/', $attributeDataString, $varData)) {
100
                $properties[trim($varData[1], '"\'')] = trim($varData[2], '"\'');
101
            } elseif (preg_match('/([^=]+)/', $attributeDataString, $varData)) {
102
                $properties[trim($varData[1], '"\'')] = true;
103
            }
104
        }
105
106
        return $properties;
107
    }
108
}
109