Attributes::isEmpty()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
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
        if (isset($this->attributes[$attribute])) {
91
            return $this->attributes[$attribute];
92
        }
93
94
        return $fallback;
95
    }
96
97
    /**
98
     * @param string $attribute
99
     *
100
     * @return bool
101
     */
102
    public function hasAttribute($attribute)
103
    {
104
        return array_key_exists($attribute, $this->attributes);
105
    }
106
107
    /**
108
     * @param string|iterable $class
109
     */
110
    public function addClass($class)
111
    {
112
        if (is_string($class)) {
113
            $class = explode(' ', $class);
114
        }
115
116
        $class = Arr::getToggledValues($class);
117
118
        $this->classes = array_unique(
119
            array_merge($this->classes, $class)
120
        );
121
    }
122
123
    /**
124
     * @return bool
125
     */
126
    public function isEmpty()
127
    {
128
        return empty($this->attributes) && empty($this->classes);
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    public function toArray()
135
    {
136
        if (empty($this->classes)) {
137
            return $this->attributes;
138
        }
139
140
        return array_merge(['class' => implode(' ', $this->classes)], $this->attributes);
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function render()
147
    {
148
        if ($this->isEmpty()) {
149
            return '';
150
        }
151
152
        $attributeStrings = [];
153
154
        foreach ($this->toArray() as $attribute => $value) {
155
            if ($value === '') {
156
                $attributeStrings[] = $attribute;
157
                continue;
158
            }
159
160
            $value = htmlentities($value, ENT_QUOTES, 'UTF-8', false);
161
162
            $attributeStrings[] = "{$attribute}=\"{$value}\"";
163
        }
164
165
        return implode(' ', $attributeStrings);
166
    }
167
}
168