Completed
Pull Request — master (#5)
by Oleksandr
12:32 queued 05:09
created

WebComponentConfigToTwigConfigTransformer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A transformFromArrayToHtmlAttributes() 0 23 5
A transform() 0 15 3
A reverseTransform() 0 14 3
A transformFropmHtmlAttributesToArray() 0 15 4
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
        $properties = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $properties is dead and can be removed.
Loading history...
27
28
        if (is_array($value)) {
29
            $result = $value;
30
31
            if (isset($value['properties'])) {
32
                $result['component'] = $this->transformFromArrayToHtmlAttributes($result['properties'] ?? []);
33
                unset($result['properties']);
34
            }
35
        }
36
37
        return $result;
38
    }
39
40
    /**
41
     * @param array $collection
42
     *
43
     * @return string
44
     */
45
    protected function transformFromArrayToHtmlAttributes(array $collection): string
46
    {
47
        $htmlAttributesString = '';
48
49
        $iterationCount = 0;
50
        $maxIterationCount = count($collection);
51
        foreach ($collection as $itemKey => $itemValue) {
52
            if (is_bool($itemValue)) {
53
                if ($itemValue === true) {
54
                    $htmlAttributesString .= sprintf('%s', $itemKey);
55
                }
56
            } else {
57
                $htmlAttributesString .= sprintf('%s="%s"', $itemKey, $itemValue);
58
            }
59
60
            if ($iterationCount < $maxIterationCount) {
61
                $htmlAttributesString .= ' ';
62
            }
63
64
            ++$iterationCount;
65
        }
66
67
        return $htmlAttributesString;
68
    }
69
70
    /**
71
     * @inheritdoc
72
     *
73
     * @param mixed $value
74
     *
75
     * @return mixed|void
76
     */
77
    public function reverseTransform($value)
78
    {
79
        $result = [];
80
81
        if (is_array($value)) {
82
            $result = $value;
83
84
            if (isset($value['component'])) {
85
                $result['properties'] = $this->transformFropmHtmlAttributesToArray($result['component'] ?? []);
0 ignored issues
show
Bug introduced by
It seems like $result['component'] ?? array() can also be of type array; however, parameter $attributes of SprykerEco\Yves\FactFind...HtmlAttributesToArray() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
                $result['properties'] = $this->transformFropmHtmlAttributesToArray(/** @scrutinizer ignore-type */ $result['component'] ?? []);
Loading history...
86
                unset($result['component']);
87
            }
88
        }
89
90
        return $result;
91
    }
92
93
    /**
94
     * @param string $attributes
95
     *
96
     * @return array
97
     */
98
    protected function transformFropmHtmlAttributesToArray(string $attributes): array
99
    {
100
        $properties = [];
101
102
        if (is_string($attributes)) {
0 ignored issues
show
introduced by
The condition is_string($attributes) is always true.
Loading history...
103
            $attributeDataCollection = explode(' ', trim($attributes));
104
            foreach ($attributeDataCollection as $attributeDataString) {
105
                $varData = explode('=', $attributeDataString);
106
                if ($varData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $varData of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
107
                    $properties[$varData[0]] = trim($varData[1], '"\'');
108
                }
109
            }
110
        }
111
112
        return $properties;
113
    }
114
}
115