Total Complexity | 13 |
Total Lines | 93 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | class Classlist extends AbstractHtmlAttribute implements HtmlAttributeInterface |
||
8 | { |
||
9 | /** |
||
10 | * |
||
11 | */ |
||
12 | protected $value = []; |
||
13 | |||
14 | protected const ATTRIBUTE = 'class'; |
||
15 | |||
16 | /** |
||
17 | * |
||
18 | */ |
||
19 | public function __construct($value = []) |
||
20 | { |
||
21 | $this->set($value); |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * |
||
26 | */ |
||
27 | public function set($classlist) |
||
28 | { |
||
29 | if (is_string($classlist)) { |
||
30 | $this->value = $this->tokenize($classlist); |
||
31 | } elseif (is_array($classlist)) { |
||
32 | $this->value = $classlist; |
||
33 | } |
||
34 | |||
35 | return $this; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * |
||
40 | */ |
||
41 | public function add(string $list) |
||
42 | { |
||
43 | $list = $this->tokenize($list); |
||
44 | |||
45 | foreach ((array) $list as $class) { |
||
46 | $this->value[] = $class; |
||
47 | } |
||
48 | |||
49 | return $this; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * |
||
54 | */ |
||
55 | public function remove($class) |
||
56 | { |
||
57 | while ($this->contains($class)) { |
||
58 | unset($this->value[array_search($class, $this->value)]); |
||
59 | } |
||
60 | |||
61 | return $this; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * |
||
66 | */ |
||
67 | public function toggle($class) |
||
68 | { |
||
69 | if ($this->contains($class)) { |
||
70 | $this->remove($class); |
||
71 | } else { |
||
72 | $this->add($class); |
||
73 | } |
||
74 | |||
75 | return $this; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * |
||
80 | */ |
||
81 | public function contains($class) |
||
82 | { |
||
83 | return in_array($class, $this->value, false); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * |
||
88 | */ |
||
89 | public function parse(): string |
||
90 | { |
||
91 | return implode(' ', (array) $this->value); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * |
||
96 | */ |
||
97 | public function tokenize(string $classlist): array |
||
100 | } |
||
101 | } |
||
102 |