Completed
Pull Request — master (#14)
by Sebastian
03:03
created

Attributes::hasAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
    public function setAttributes(iterable $attributes)
19
    {
20
        foreach ($attributes as $attribute => $value) {
21
            if ($attribute === 'class') {
22
                $this->addClass($value);
23
                continue;
24
            }
25
26
            if (is_int($attribute)) {
27
                $attribute = $value;
28
                $value = '';
29
            }
30
31
            $this->setAttribute($attribute, (string) $value);
32
        }
33
    }
34
35
    /**
36
     * @param string $attribute
37
     * @param string $value
38
     */
39
    public function setAttribute(string $attribute, string $value = '')
40
    {
41
        if ($attribute === 'class') {
42
            $this->addClass($value);
43
44
            return $this;
45
        }
46
47
        $this->attributes[$attribute] = $value;
48
    }
49
50
    public function forgetAttribute(string $attribute)
51
    {
52
        if ($attribute === 'class') {
53
            $this->classes = [];
54
55
            return $this;
56
        }
57
58
        if (isset($this->attributes[$attribute])) {
59
            unset($this->attributes[$attribute]);
60
        }
61
62
        return $this;
63
    }
64
65
    public function getAttribute(string $attribute, string $fallback = null): ?string
66
    {
67
        if ($attribute === 'class') {
68
            return implode(' ', $this->classes);
69
        }
70
71
        return $this->attributes[$attribute] ?? $fallback;
72
    }
73
74
    public function hasAttribute(string $attribute): bool
75
    {
76
        return array_key_exists($attribute, $this->attributes);
77
    }
78
79
    /**
80
     * @param string|iterable $class
81
     */
82
    public function addClass($class)
83
    {
84
        $class = Arr::getToggledValues($class);
85
86
        $this->classes = array_unique(
87
            array_merge($this->classes, $class)
88
        );
89
    }
90
91
    public function isEmpty(): bool
92
    {
93
        return empty($this->attributes) && empty($this->classes);
94
    }
95
96
    public function toArray(): array
97
    {
98
        if (empty($this->classes)) {
99
            return $this->attributes;
100
        }
101
102
        return array_merge(['class' => implode(' ', $this->classes)], $this->attributes);
103
    }
104
105
    public function render(): string
106
    {
107
        if ($this->isEmpty()) {
108
            return '';
109
        }
110
111
        $attributeStrings = [];
112
113
        foreach ($this->toArray() as $attribute => $value) {
114
            if ($value === '') {
115
                $attributeStrings[] = $attribute;
116
                continue;
117
            }
118
119
            $value = htmlentities($value, ENT_QUOTES, 'UTF-8', false);
120
121
            $attributeStrings[] = "{$attribute}=\"{$value}\"";
122
        }
123
124
        return implode(' ', $attributeStrings);
125
    }
126
}
127