Classlist   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 78
ccs 0
cts 29
cp 0
rs 10
c 1
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A tokenize() 0 3 1
A add() 0 9 2
A contains() 0 3 1
A set() 0 9 3
A __construct() 0 3 1
A parse() 0 3 1
A remove() 0 7 2
A toggle() 0 9 2
1
<?php
2
3
namespace WebTheory\Html\Attributes;
4
5
use WebTheory\Html\Contracts\HtmlAttributeInterface;
6
7
class Classlist extends AbstractHtmlAttribute implements HtmlAttributeInterface
8
{
9
    protected string $name = 'class';
10
11
    protected array $value = [];
12
13
    public function __construct($value = [])
14
    {
15
        $this->set($value);
16
    }
17
18
    /**
19
     * @return $this
20
     */
21
    public function set($classlist): Classlist
22
    {
23
        if (is_string($classlist)) {
24
            $this->value = $this->tokenize($classlist);
25
        } elseif (is_array($classlist)) {
26
            $this->value = $classlist;
27
        }
28
29
        return $this;
30
    }
31
32
    /**
33
     * @return $this
34
     */
35
    public function add(string $list): Classlist
36
    {
37
        $list = $this->tokenize($list);
38
39
        foreach ((array) $list as $class) {
40
            $this->value[] = $class;
41
        }
42
43
        return $this;
44
    }
45
46
    /**
47
     * @return $this
48
     */
49
    public function remove($class): Classlist
50
    {
51
        while ($this->contains($class)) {
52
            unset($this->value[array_search($class, $this->value)]);
53
        }
54
55
        return $this;
56
    }
57
58
    /**
59
     * @return $this
60
     */
61
    public function toggle($class): Classlist
62
    {
63
        if ($this->contains($class)) {
64
            $this->remove($class);
65
        } else {
66
            $this->add($class);
67
        }
68
69
        return $this;
70
    }
71
72
    public function contains($class): bool
73
    {
74
        return in_array($class, $this->value, false);
75
    }
76
77
    public function parse(): string
78
    {
79
        return implode(' ', (array) $this->value);
80
    }
81
82
    public function tokenize(string $classlist): array
83
    {
84
        return explode(' ', $classlist);
85
    }
86
}
87