Passed
Push — html ( 49f55d...51e1ba )
by Peter
03:18
created

Attributes::isEqual()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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