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
|
|
|
|
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 PrototypeHTMLElementTrait{ |
25
|
|
|
use PrototypeElementTrait; |
26
|
|
|
|
27
|
|
|
protected function magic_get_id():string{ |
28
|
|
|
return \trim($this->getAttribute('id')); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function magic_set_id(string $id){ |
32
|
|
|
return $this->setAttribute('id', $id); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function magic_get_class():string{ |
36
|
|
|
return \trim($this->getAttribute('class')); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function magic_set_class(string $class){ |
40
|
|
|
return $this->setAttribute('class', $class); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function magic_get_href():string{ |
44
|
|
|
return \trim($this->getAttribute('href')); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function magic_set_href(string $href){ |
48
|
|
|
return $this->setAttribute('href', $href); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function magic_get_src():string{ |
52
|
|
|
return \trim($this->getAttribute('src')); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function magic_set_src(string $src){ |
56
|
|
|
return $this->setAttribute('src', $src); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function magic_get_innerHTML():string{ |
60
|
|
|
return $this->inspect(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* http://api.prototypejs.org/dom/Element/identify/ |
65
|
|
|
* |
66
|
|
|
* @param string|null $newID |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function identify(string $newID = null):string{ |
71
|
|
|
$oldID = $this->id; |
72
|
|
|
|
73
|
|
|
if($newID !== null){ |
74
|
|
|
$this->id = $newID; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $oldID; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @link http://api.prototypejs.org/dom/Element/classNames/ |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function classNames():array{ |
86
|
|
|
|
87
|
|
|
if(!$this->hasAttributes()){ |
|
|
|
|
88
|
|
|
return []; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$classnames = \explode(' ', $this->class); |
92
|
|
|
$currentClassnames = []; |
93
|
|
|
|
94
|
|
|
foreach($classnames as $classname){ |
95
|
|
|
|
96
|
|
|
if(empty($classname)){ |
97
|
|
|
continue; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$currentClassnames[] = $classname; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $currentClassnames; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @link http://api.prototypejs.org/dom/Element/hasClassName/ |
108
|
|
|
* |
109
|
|
|
* @param string $classname |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function hasClassName(string $classname):bool{ |
114
|
|
|
return \in_array($classname, $this->classNames(), true); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @link http://api.prototypejs.org/dom/Element/addClassName/ |
119
|
|
|
* |
120
|
|
|
* @param string $classname |
121
|
|
|
* |
122
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement |
123
|
|
|
*/ |
124
|
|
|
public function addClassName(string $classname):PrototypeHTMLElement{ |
125
|
|
|
return $this->addClassNames([$classname]); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @link http://api.prototypejs.org/dom/Element/removeClassName/ |
130
|
|
|
* |
131
|
|
|
* @param string $classname |
132
|
|
|
* |
133
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement |
134
|
|
|
*/ |
135
|
|
|
public function removeClassName(string $classname):PrototypeHTMLElement{ |
136
|
|
|
return $this->removeClassNames([$classname]); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @link http://api.prototypejs.org/dom/Element/toggleClassName/ |
141
|
|
|
* |
142
|
|
|
* @param string $classname |
143
|
|
|
* |
144
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement |
145
|
|
|
*/ |
146
|
|
|
public function toggleClassName(string $classname):PrototypeHTMLElement{ |
147
|
|
|
|
148
|
|
|
if($this->hasClassName($classname)){ |
149
|
|
|
return $this->removeClassName($classname); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->addClassName($classname); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @link http://api.prototypejs.org/dom/Element/getStyle/ |
157
|
|
|
* |
158
|
|
|
* @param string $property |
159
|
|
|
* |
160
|
|
|
* @return null|string |
161
|
|
|
*/ |
162
|
|
|
public function getStyle(string $property):?string{ |
163
|
|
|
$currentStyle = $this->getStyles(); |
|
|
|
|
164
|
|
|
|
165
|
|
|
if(\array_key_exists(\strtolower($property), $currentStyle)){ |
166
|
|
|
return $currentStyle[$property]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return null; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @link http://api.prototypejs.org/dom/Element/setStyle/ |
174
|
|
|
* |
175
|
|
|
* @param array $style |
176
|
|
|
* @param bool $replace |
177
|
|
|
* |
178
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement |
179
|
|
|
*/ |
180
|
|
|
public function setStyle(array $style, bool $replace = null):PrototypeHTMLElement{ |
181
|
|
|
$currentStyle = $this->getStyles(); |
|
|
|
|
182
|
|
|
|
183
|
|
|
if($replace !== true){ |
184
|
|
|
$style = \array_merge($currentStyle, $style); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
foreach($style as $property => $value){ |
188
|
|
|
$style[$property] = $property.': '.$value.';'; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->setAttribute('style', \implode(' ', $style)); |
|
|
|
|
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
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.