1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Trait HTMLElementTrait |
4
|
|
|
* |
5
|
|
|
* @filesource HTMLElementTrait.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
|
|
|
|
13
|
|
|
namespace chillerlan\PrototypeDOM\Node; |
14
|
|
|
|
15
|
|
|
use chillerlan\Traits\Magic; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @property string $id |
19
|
|
|
* @property string $class |
20
|
|
|
* @property string $href |
21
|
|
|
* @property string $src |
22
|
|
|
* @property string $innerHTML |
23
|
|
|
*/ |
24
|
|
|
trait HTMLElementTrait{ |
25
|
|
|
|
26
|
|
|
protected function magic_get_id():string{ |
27
|
|
|
return \trim($this->getAttribute('id')); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function magic_set_id($id){ |
31
|
|
|
return $this->setAttribute('id', $id); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function magic_get_class():string{ |
35
|
|
|
return \trim($this->getAttribute('class')); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function magic_set_class($class){ |
39
|
|
|
return $this->setAttribute('class', $class); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function magic_get_href():string{ |
43
|
|
|
return \trim($this->getAttribute('href')); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function magic_set_href($class){ |
47
|
|
|
return $this->setAttribute('href', $class); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function magic_get_src():string{ |
51
|
|
|
return \trim($this->getAttribute('src')); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function magic_set_src($class){ |
55
|
|
|
return $this->setAttribute('src', $class); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function magic_get_innerHTML():string{ |
59
|
|
|
return $this->inspect(); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* http://api.prototypejs.org/dom/Element/identify/ |
64
|
|
|
* |
65
|
|
|
* @param string|null $newID |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function identify(string $newID = null):string{ |
70
|
|
|
$oldID = $this->id; |
71
|
|
|
|
72
|
|
|
if($newID !== null){ |
73
|
|
|
$this->id = $newID; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $oldID; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @link http://api.prototypejs.org/dom/Element/classNames/ |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function classNames():array{ |
85
|
|
|
|
86
|
|
|
if(!$this->hasAttributes()){ |
|
|
|
|
87
|
|
|
return []; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$classnames = \explode(' ', $this->class); |
91
|
|
|
$currentClassnames = []; |
92
|
|
|
|
93
|
|
|
foreach($classnames as $classname){ |
94
|
|
|
|
95
|
|
|
if(empty($classname)){ |
96
|
|
|
continue; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$currentClassnames[] = $classname; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $currentClassnames; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @link http://api.prototypejs.org/dom/Element/hasClassName/ |
107
|
|
|
* |
108
|
|
|
* @param string $classname |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function hasClassName(string $classname):bool{ |
113
|
|
|
return \in_array($classname, $this->classNames(), true); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @link http://api.prototypejs.org/dom/Element/addClassName/ |
118
|
|
|
* |
119
|
|
|
* @param string $classname |
120
|
|
|
* |
121
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement |
122
|
|
|
*/ |
123
|
|
|
public function addClassName(string $classname):PrototypeHTMLElement{ |
124
|
|
|
return $this->addClassNames([$classname]); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @link http://api.prototypejs.org/dom/Element/removeClassName/ |
129
|
|
|
* |
130
|
|
|
* @param string $classname |
131
|
|
|
* |
132
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement |
133
|
|
|
*/ |
134
|
|
|
public function removeClassName(string $classname):PrototypeHTMLElement{ |
135
|
|
|
return $this->removeClassNames([$classname]); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @link http://api.prototypejs.org/dom/Element/toggleClassName/ |
140
|
|
|
* |
141
|
|
|
* @param string $classname |
142
|
|
|
* |
143
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement |
144
|
|
|
*/ |
145
|
|
|
public function toggleClassName(string $classname):PrototypeHTMLElement{ |
146
|
|
|
|
147
|
|
|
if($this->hasClassName($classname)){ |
148
|
|
|
return $this->removeClassName($classname); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->addClassName($classname); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @link http://api.prototypejs.org/dom/Element/getStyle/ |
156
|
|
|
* |
157
|
|
|
* @param string $property |
158
|
|
|
* |
159
|
|
|
* @return null|string |
160
|
|
|
*/ |
161
|
|
|
public function getStyle(string $property):?string{ |
162
|
|
|
$currentStyle = $this->getStyles(); |
|
|
|
|
163
|
|
|
|
164
|
|
|
if(\array_key_exists(\strtolower($property), $currentStyle)){ |
165
|
|
|
return $currentStyle[$property]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return null; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @link http://api.prototypejs.org/dom/Element/setStyle/ |
173
|
|
|
* |
174
|
|
|
* @param array $style |
175
|
|
|
* @param bool $replace |
176
|
|
|
* |
177
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement |
178
|
|
|
*/ |
179
|
|
|
public function setStyle(array $style, bool $replace = null):PrototypeHTMLElement{ |
180
|
|
|
$currentStyle = $this->getStyles(); |
|
|
|
|
181
|
|
|
|
182
|
|
|
if($replace !== true){ |
183
|
|
|
$style = \array_merge($currentStyle, $style); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
foreach($style as $property => $value){ |
187
|
|
|
$style[$property] = $property.': '.$value.';'; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$this->setAttribute('style', \implode(' ', $style)); |
|
|
|
|
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.