Attributes::setAttributes()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Html;
4
5
use Spatie\Html\Helpers\Arr;
6
7
class Attributes
8
{
9
    /** @var array */
10
    protected $attributes = [];
11
12
    /** @var array */
13
    protected $classes = [];
14
15
    /**
16
     * @param iterable $attributes
17
     *
18
     * @return $this
19
     */
20
    public function setAttributes($attributes)
21
    {
22
        foreach ($attributes as $attribute => $value) {
23
            if ($attribute === 'class') {
24
                $this->addClass($value);
25
                continue;
26
            }
27
28
            if (is_int($attribute)) {
29
                $attribute = $value;
30
                $value = '';
31
            }
32
33
            $this->setAttribute($attribute, (string) $value);
34
        }
35
36
        return $this;
37
    }
38
39
    /**
40
     * @param string $attribute
41
     * @param string|null $value
42
     *
43
     * @return $this
44
     */
45
    public function setAttribute($attribute, $value = null)
46
    {
47
        if ($attribute === 'class') {
48
            $this->addClass($value);
49
50
            return $this;
51
        }
52
53
        $this->attributes[$attribute] = $value;
54
55
        return $this;
56
    }
57
58
    /**
59
     * @param string $attribute
60
     *
61
     * @return $this
62
     */
63
    public function forgetAttribute($attribute)
64
    {
65
        if ($attribute === 'class') {
66
            $this->classes = [];
67
68
            return $this;
69
        }
70
71
        if (isset($this->attributes[$attribute])) {
72
            unset($this->attributes[$attribute]);
73
        }
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param string $attribute
80
     * @param mixed $fallback
81
     *
82
     * @return mixed
83
     */
84
    public function getAttribute($attribute, $fallback = null)
85
    {
86
        if ($attribute === 'class') {
87
            return implode(' ', $this->classes);
88
        }
89
90
        return $this->attributes[$attribute] ?? $fallback;
91
    }
92
93
    public function hasAttribute(string $attribute): bool
94
    {
95
        return array_key_exists($attribute, $this->attributes);
96
    }
97
98
    /**
99
     * @param string|iterable $class
100
     */
101
    public function addClass($class)
102
    {
103
        if (is_string($class)) {
104
            $class = explode(' ', $class);
105
        }
106
107
        $class = Arr::getToggledValues($class);
108
109
        $this->classes = array_unique(
110
            array_merge($this->classes, $class)
111
        );
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    public function isEmpty()
118
    {
119
        return empty($this->attributes) && empty($this->classes);
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    public function toArray()
126
    {
127
        if (empty($this->classes)) {
128
            return $this->attributes;
129
        }
130
131
        return array_merge(['class' => implode(' ', $this->classes)], $this->attributes);
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function render()
138
    {
139
        if ($this->isEmpty()) {
140
            return '';
141
        }
142
143
        $attributeStrings = [];
144
145
        foreach ($this->toArray() as $attribute => $value) {
146
            if ($value === '') {
147
                $attributeStrings[] = $attribute;
148
                continue;
149
            }
150
151
            $value = htmlentities($value, ENT_QUOTES, 'UTF-8', false);
152
153
            $attributeStrings[] = "{$attribute}=\"{$value}\"";
154
        }
155
156
        return implode(' ', $attributeStrings);
157
    }
158
}
159