1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Trait PrototypeHTMLElementTrait |
4
|
|
|
* |
5
|
|
|
* @filesource PrototypeHTMLElementTrait.php |
6
|
|
|
* @created 11.05.2017 |
7
|
|
|
* @package chillerlan\PrototypeDOM\Node |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
* |
12
|
|
|
* @noinspection PhpIncompatibleReturnTypeInspection |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace chillerlan\PrototypeDOM\Node; |
16
|
|
|
|
17
|
|
|
use function array_key_exists, array_merge, explode, implode, in_array, strtolower, trim; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
trait PrototypeHTMLElementTrait{ |
23
|
|
|
use PrototypeElementTrait; |
24
|
|
|
|
25
|
|
|
public function getID():string{ |
26
|
|
|
return trim($this->getAttribute('id')); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function setID(string $id):PrototypeHTMLElement{ |
30
|
|
|
$this->setAttribute('id', $id); |
|
|
|
|
31
|
|
|
|
32
|
|
|
return $this; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getClassName():string{ |
36
|
|
|
return trim($this->getAttribute('class')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setClassName(string $class):PrototypeHTMLElement{ |
40
|
|
|
$this->setAttribute('class', $class); |
41
|
|
|
|
42
|
|
|
return $this; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getHref():string{ |
46
|
|
|
return trim($this->getAttribute('href')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setHref(string $href):PrototypeHTMLElement{ |
50
|
|
|
$this->setAttribute('href', $href); |
51
|
|
|
|
52
|
|
|
return $this; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getSrc():string{ |
56
|
|
|
return trim($this->getAttribute('src')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setSrc(string $src):PrototypeHTMLElement{ |
60
|
|
|
$this->setAttribute('src', $src); |
61
|
|
|
|
62
|
|
|
return $this; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
public function identify(string $newID = null):string{ |
69
|
|
|
$oldID = $this->getAttribute('id'); |
70
|
|
|
|
71
|
|
|
if($newID !== null){ |
72
|
|
|
$this->setAttribute('id', $newID); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $oldID; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function classNames():array{ |
82
|
|
|
|
83
|
|
|
if(!$this->hasAttributes()){ |
|
|
|
|
84
|
|
|
return []; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$classnames = explode(' ', $this->getClassName()); |
88
|
|
|
$currentClassnames = []; |
89
|
|
|
|
90
|
|
|
foreach($classnames as $classname){ |
91
|
|
|
$classname = trim($classname); |
92
|
|
|
|
93
|
|
|
if(!empty($classname)){ |
94
|
|
|
$currentClassnames[] = $classname; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $currentClassnames; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritDoc |
104
|
|
|
*/ |
105
|
|
|
public function hasClassName(string $classname):bool{ |
106
|
|
|
return in_array($classname, $this->classNames(), true); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritDoc |
111
|
|
|
*/ |
112
|
|
|
public function addClassName(string $classname):PrototypeHTMLElement{ |
113
|
|
|
return $this->addClassNames([$classname]); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritDoc |
118
|
|
|
*/ |
119
|
|
|
public function removeClassName(string $classname):PrototypeHTMLElement{ |
120
|
|
|
return $this->removeClassNames([$classname]); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritDoc |
125
|
|
|
*/ |
126
|
|
|
public function toggleClassName(string $classname):PrototypeHTMLElement{ |
127
|
|
|
|
128
|
|
|
if($this->hasClassName($classname)){ |
129
|
|
|
return $this->removeClassName($classname); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->addClassName($classname); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritDoc |
137
|
|
|
*/ |
138
|
|
|
public function getStyle(string $property):?string{ |
139
|
|
|
$currentStyle = $this->getStyles(); |
|
|
|
|
140
|
|
|
|
141
|
|
|
if(array_key_exists(strtolower($property), $currentStyle)){ |
142
|
|
|
return $currentStyle[$property]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @inheritDoc |
150
|
|
|
*/ |
151
|
|
|
public function setStyle(array $style, bool $replace = null):PrototypeHTMLElement{ |
152
|
|
|
$currentStyle = $this->getStyles(); |
153
|
|
|
|
154
|
|
|
if($replace !== true){ |
155
|
|
|
$style = array_merge($currentStyle, $style); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
foreach($style as $property => $value){ |
159
|
|
|
$style[$property] = $property.': '.$value.';'; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$this->setAttribute('style', implode(' ', $style)); |
163
|
|
|
|
164
|
|
|
return $this; |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|