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