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 ( f846a8...d6a28a )
by Sebastian
02:09 queued 11s
created

Attributes   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 114
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setAttributes() 0 19 4
A setAttribute() 0 12 2
A addClass() 0 12 2
A isEmpty() 0 4 2
A toArray() 0 8 2
A toString() 0 19 4
A __toString() 0 4 1
1
<?php
2
3
namespace Spatie\HtmlElement;
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
    public function isEmpty() : bool
81
    {
82
        return empty($this->attributes) && empty($this->classes);
83
    }
84
85
    public function toArray() : array
86
    {
87
        if (empty($this->classes)) {
88
            return $this->attributes;
89
        }
90
91
        return array_merge($this->attributes, ['class' => implode(' ', $this->classes)]);
92
    }
93
94
    public function toString() : string
95
    {
96
        if ($this->isEmpty()) {
97
            return '';
98
        }
99
100
        $attributeStrings = [];
101
102
        foreach ($this->toArray() as $attribute => $value) {
103
            if (empty($value)) {
104
                $attributeStrings[] = $attribute;
105
                continue;
106
            }
107
108
            $attributeStrings[] = "{$attribute}=\"{$value}\"";
109
        }
110
111
        return implode(' ', $attributeStrings);
112
    }
113
114
    public function __toString() : string
115
    {
116
        return $this->toString();
117
    }
118
}
119