Passed
Push — attributes ( 57ed6a )
by Peter
04:12
created

Attributes::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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