|
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
|
|
|
|