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