Passed
Push — attributes ( 364588...7c09f5 )
by Peter
04:03
created

Attributes::isEqual()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Html;
6
7
class Attributes
8
{
9
    /** @var array<string,Attribute> */
10
    protected array $items = [];
11
12
    /**
13
     * Attribute constructor.
14
     *
15
     * @param array<string,mixed>|null $attributes
16
     */
17
    public function __construct(array $attributes = null)
18
    {
19
        if ($attributes === null) {
20
            return;
21
        }
22
23
        foreach ($attributes as $key => $values) {
24
            if ($values instanceof Attribute) {
25
                $this->items[$values->getKey()] = $values;
26
            } else {
27
                $this->items[$key] = new Attribute($key, ...(array)$values);
28
            }
29
        }
30
    }
31
32
    /**
33
     * @param Attributes $attributes
34
     *
35
     * @return $this
36
     */
37
    public function merge(Attributes $attributes): self
38
    {
39
        return $this->mergeItems(...$attributes->getItems());
40
    }
41
42
    /**
43
     * @param Attribute ...$attributes
44
     *
45
     * @return $this
46
     */
47
    public function mergeItems(Attribute ...$attributes): self
48
    {
49
        foreach ($attributes as $attribute) {
50
            $key = $attribute->getKey();
51
            if (array_key_exists($key, $this->items)) {
52
                $this->items[$key]->append(...$attribute->getValues());
53
            } else {
54
                $this->items[$key] = $attribute;
55
            }
56
        }
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param Attributes $attributes
63
     *
64
     * @return $this
65
     */
66
    public function replace(Attributes $attributes): self
67
    {
68
        return $this->replaceItems(...$attributes->getItems());
69
    }
70
71
    /**
72
     * @param Attribute ...$attributes
73
     *
74
     * @return $this
75
     */
76
    public function replaceItems(Attribute ...$attributes): self
77
    {
78
        foreach ($attributes as $attribute) {
79
            $key = $attribute->getKey();
80
            assert($key === $attribute->getKey());
81
            $this->items[$key] = $attribute;
82
        }
83
84
        return $this;
85
    }
86
87
    /**
88
     * @param string ...$keys
89
     *
90
     * @return $this
91
     */
92
    public function remove(string ...$keys): self
93
    {
94
        foreach ($keys as $key) {
95
            if (array_key_exists($key, $this->items)) {
96
                unset($this->items[$key]);
97
            }
98
        }
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return array<string,Attribute>
105
     */
106
    public function getItems(): array
107
    {
108
        return array_values($this->items);
109
    }
110
111
    /**
112
     * @param string $key
113
     *
114
     * @return Attribute|null
115
     */
116
    public function getItem(string $key): ?Attribute
117
    {
118
        if (array_key_exists($key, $this->items)) {
119
            return $this->items[$key];
120
        }
121
122
        return null;
123
    }
124
125
    /**
126
     * @param string $key
127
     *
128
     * @return bool
129
     */
130
    public function hasItem(string $key): bool
131
    {
132
        return array_key_exists($key, $this->items);
133
    }
134
135
    /**
136
     * @param Attribute $attribute
137
     *
138
     * @return $this
139
     */
140
    public function mergeItem(Attribute $attribute): self
141
    {
142
        $key = $attribute->getKey();
143
        if (array_key_exists($key, $this->items) && !$this->items[$key]->isNull() && !$attribute->isNull()) {
144
            $this->items[$key]->append(...$attribute->getValues());
145
        } elseif (!$attribute->isNull() || !array_key_exists($key, $this->items)) {
146
            $this->items[$key] = $attribute;
147
        }
148
149
        return $this;
150
    }
151
152
    /**
153
     * @param Attribute $attribute
154
     *
155
     * @return $this
156
     */
157
    public function replaceItem(Attribute $attribute): self
158
    {
159
        $this->items[$attribute->getKey()] = $attribute;
160
161
        return $this;
162
    }
163
164
    /**
165
     * @param Attributes|null $attributes
166
     *
167
     * @return bool
168
     */
169
    public function isEqual(?Attributes $attributes): bool
170
    {
171
        if (null === $attributes) {
172
            return count($this->items) === 0;
173
        }
174
175
        return (string)$this == (string)$attributes;
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function __toString(): string
182
    {
183
        $return = '';
184
        foreach ($this->items as $item) {
185
            $return .= ' ' . (string)$item;
186
        }
187
188
        return $return;
189
    }
190
}
191