GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 782770...02cc1c )
by Sebastian
03:06
created

Attributes::addClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Spatie\Menu\Html;
4
5
class Attributes
6
{
7
    /** @var array */
8
    protected $attributes = [];
9
10
    /** @var array */
11
    protected $classes = [];
12
13
    public function __construct(array $attributes = [])
14
    {
15
        $this->setAttributes($attributes);
16
    }
17
18
    /**
19
     * @param array $attributes
20
     *
21
     * @return $this
22
     */
23
    public function setAttributes(array $attributes)
24
    {
25
        foreach ($attributes as $attribute => $value) {
26
27
            if ($attribute === 'class') {
28
                $this->addClass($value);
29
                continue;
30
            }
31
32
            if (is_int($attribute)) {
33
                $attribute = $value;
34
                $value = '';
35
            }
36
37
            $this->setAttribute($attribute, $value);
38
        }
39
40
        return $this;
41
    }
42
43
    /**
44
     * @param string $attribute
45
     * @param string $value
46
     *
47
     * @return $this
48
     */
49
    public function setAttribute(string $attribute, string $value = '')
50
    {
51
        if ($attribute === 'class') {
52
            $this->addClass($value);
53
54
            return $this;
55
        }
56
57
        $this->attributes[$attribute] = $value;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @param string|array $class
64
     *
65
     * @return $this
66
     */
67
    public function addClass($class)
68
    {
69
        if (!is_array($class)) {
70
            $class = [$class];
71
        }
72
73
        $this->classes = array_unique(
74
            array_merge($this->classes, $class)
75
        );
76
77
        return $this;
78
    }
79
80
    /**
81
     * @param \Spatie\Menu\Html\Attributes $attributes
82
     *
83
     * @return $this
84
     */
85
    public function mergeWith(Attributes $attributes)
86
    {
87
        $this->attributes = array_merge($this->attributes, $attributes->attributes);
88
        $this->classes = array_merge($this->classes, $attributes->classes);
89
90
        return $this;
91
    }
92
93
    public function isEmpty(): bool
94
    {
95
        return empty($this->attributes) && empty($this->classes);
96
    }
97
98
    public function toArray(): array
99
    {
100
        if (empty($this->classes)) {
101
            return $this->attributes;
102
        }
103
104
        return array_merge($this->attributes, ['class' => implode(' ', $this->classes)]);
105
    }
106
107
    public function toString(): string
108
    {
109
        if ($this->isEmpty()) {
110
            return '';
111
        }
112
113
        $attributeStrings = [];
114
115
        foreach ($this->toArray() as $attribute => $value) {
116
            if (is_null($value) || $value === '') {
117
                $attributeStrings[] = $attribute;
118
                continue;
119
            }
120
121
            $attributeStrings[] = "{$attribute}=\"{$value}\"";
122
        }
123
124
        return implode(' ', $attributeStrings);
125
    }
126
127
    public function __toString() : string
128
    {
129
        return $this->toString();
130
    }
131
}
132