Passed
Push — html ( 51e1ba...d230bc )
by Peter
02:59
created

Attributes::fromArray()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 6
nop 1
dl 0
loc 25
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Html\Helper;
6
7
use AbterPhp\Framework\Helper\Debug;
8
use AbterPhp\Framework\Html\Attribute;
9
10
class Attributes
11
{
12
    /**
13
     * @param array<string,string|string[]>|null $values
14
     *
15
     * @return array
16
     */
17
    public static function fromArray(?array $values): array
18
    {
19
        if ($values === null) {
20
            return [];
21
        }
22
23
        $attributes = [];
24
25
        foreach ($values as $key => $value) {
26
            if (null === $value) {
27
                $values = [];
28
            } elseif (!is_array($value)) {
29
                $values = [$value];
30
            } elseif (is_object($value) && $value instanceof Attribute) {
31
                throw new \InvalidArgumentException('AttributesHelper::fromArray does not handle Attributes');
32
            } else {
33
                $values = $value;
34
            }
35
36
            assert(Collection::allStrings($values), Debug::prettyPrint('not all strings: %s', $values));
37
38
            $attributes[$key] = new Attribute($key, ...$values);
39
        }
40
41
        return $attributes;
42
    }
43
44
    /**
45
     * @param array<string,Attribute>|null $attribA
46
     * @param array<string,Attribute>|null $attribB
47
     *
48
     * @return array<string,Attribute>
49
     */
50
    public static function merge(?array $attribA, ?array $attribB): array
51
    {
52
        $attribA ??= [];
53
        $attribB ??= [];
54
55
        if (count($attribB) === 0) {
56
            return $attribA;
57
        }
58
59
        foreach ($attribB as $key => $attrib2) {
60
            assert($attrib2 instanceof Attribute, Debug::prettyPrint('not an Attribute: "%s"', $attrib2));
61
            assert($key === $attrib2->getKey(), sprintf('key: %s <=> %s', $key, $attrib2->getKey()));
62
63
            $key = $attrib2->getKey();
64
            if (!array_key_exists($key, $attribA)) {
65
                $attribA[$key] = clone $attrib2;
66
                continue;
67
            }
68
69
            if (null !== $attrib2->getValues()) {
70
                $attribA[$key] = $attribA[$key]->append(...$attrib2->getValues());
71
            }
72
        }
73
74
        return $attribA;
75
    }
76
77
    /**
78
     * @param array<string,Attribute>|null $attribA
79
     * @param Attribute                    ...$attribB
80
     *
81
     * @return array<string,Attribute>
82
     */
83
    public static function mergeItem(?array $attribA, Attribute ...$attribB): array
84
    {
85
        $attribC = [];
86
        foreach ($attribB as $attribute) {
87
            $attribC[$attribute->getKey()] = $attribute;
88
        }
89
90
        return static::merge($attribA, $attribC);
91
    }
92
93
    /**
94
     * @param array<string,Attribute>|null $attribA
95
     * @param array<string,Attribute>|null $attribB
96
     *
97
     * @return array<string,Attribute>
98
     */
99
    public static function replace(?array $attribA, ?array $attribB): array
100
    {
101
        $attribA ??= [];
102
        $attribB ??= [];
103
104
        if (count($attribB) === 0) {
105
            return $attribA;
106
        }
107
108
        foreach ($attribB as $key => $attrib2) {
109
            assert($attrib2 instanceof Attribute, Debug::prettyPrint('not an Attribute: "%s"', $attrib2));
110
            assert($key === $attrib2->getKey(), sprintf('key: %s <=> %s', $key, $attrib2->getKey()));
111
112
            $key           = $attrib2->getKey();
113
            $attribA[$key] = clone $attrib2;
114
        }
115
116
        return $attribA;
117
    }
118
119
    /**
120
     * @param array<string,Attribute>|null $attribA
121
     * @param Attribute                    ...$attribB
122
     *
123
     * @return array<string,Attribute>
124
     */
125
    public static function replaceItem(?array $attribA, Attribute ...$attribB): array
126
    {
127
        $attribC = [];
128
        foreach ($attribB as $attribute) {
129
            $attribC[$attribute->getKey()] = $attribute;
130
        }
131
132
        return static::replace($attribA, $attribC);
133
    }
134
135
    /**
136
     * @param array<string,Attribute>|null $attributes
137
     * @param array<string,Attribute>|null $attributes2
138
     *
139
     * @return bool
140
     */
141
    public static function isEqual(?array $attributes, ?array $attributes2): bool
142
    {
143
        $attributes  ??= [];
144
        $attributes2 ??= [];
145
146
        if (count($attributes) != count($attributes2)) {
147
            return false;
148
        }
149
150
        foreach ($attributes as $key => $attribute) {
151
            if (!array_key_exists($key, $attributes2)) {
152
                return false;
153
            }
154
155
            if (!$attribute->isEqual($attributes2[$key])) {
156
                return false;
157
            }
158
        }
159
160
        return true;
161
    }
162
163
    /**
164
     * @param array<string,Attribute> $attributes
165
     *
166
     * @return string
167
     */
168
    public static function toString(array $attributes): string
169
    {
170
        $attr = [];
171
        foreach ($attributes as $attribute) {
172
            $attr[] = " " . $attribute;
173
        }
174
175
        return join('', $attr);
176
    }
177
}
178