1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Trait ElementTrait |
4
|
|
|
* |
5
|
|
|
* @filesource ElementTrait.php |
6
|
|
|
* @created 08.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\PrototypeDOM\NodeList; |
16
|
|
|
|
17
|
|
|
trait ElementTrait{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @link http://api.prototypejs.org/dom/Element/wrap/ |
21
|
|
|
* |
22
|
|
|
* @param \chillerlan\PrototypeDOM\Node\PrototypeElement $wrapper |
23
|
|
|
* |
24
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeElement |
25
|
|
|
*/ |
26
|
|
|
public function wrap(PrototypeElement $wrapper):PrototypeElement{ |
27
|
|
|
return $wrapper->insert($this->replace($wrapper)); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @link http://api.prototypejs.org/dom/Element/update/ |
32
|
|
|
* |
33
|
|
|
* @param string|\DOMNode|\DOMNodeList $content |
34
|
|
|
* |
35
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeElement |
36
|
|
|
*/ |
37
|
|
|
public function update($content):PrototypeElement{ |
38
|
|
|
return $this->purge()->insert($content); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @link http://api.prototypejs.org/dom/Element/insert/ |
43
|
|
|
* |
44
|
|
|
* Accepted insertion points are: |
45
|
|
|
* - before (as element's previous sibling) |
46
|
|
|
* - after (as element's next sibling) |
47
|
|
|
* - top (as element's first child) |
48
|
|
|
* - bottom (as element's last child) |
49
|
|
|
* |
50
|
|
|
* @param string|array|\DOMNode|\DOMNodeList $content |
51
|
|
|
* |
52
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeElement |
53
|
|
|
*/ |
54
|
|
|
public function insert($content):PrototypeElement{ |
55
|
|
|
|
56
|
|
|
if(!\is_array($content)){ |
57
|
|
|
|
58
|
|
|
foreach($this->ownerDocument->toNodeList($content) as $node){ |
|
|
|
|
59
|
|
|
$this->insert_bottom($node); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
foreach(['before', 'after', 'top', 'bottom'] as $pos){ |
66
|
|
|
|
67
|
|
|
if(!\array_key_exists($pos, $content)){ |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$nodes = $this->ownerDocument->toNodeList($content[$pos]); |
72
|
|
|
|
73
|
|
|
if($pos === 'top' && $this->hasChildNodes() || $pos === 'after' && $this->nextSibling){ |
|
|
|
|
74
|
|
|
$nodes->reverse(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
foreach($nodes as $node){ |
78
|
|
|
\call_user_func_array([$this, 'insert_'.$pos], [$node]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param \chillerlan\PrototypeDOM\Node\PrototypeElement $node |
88
|
|
|
* @param \chillerlan\PrototypeDOM\Node\PrototypeElement|null $refNode |
89
|
|
|
* |
90
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeElement |
91
|
|
|
*/ |
92
|
|
|
public function insert_before(PrototypeElement $node, PrototypeElement $refNode = null):PrototypeElement{ |
93
|
|
|
|
94
|
|
|
if($this->parentNode){ |
95
|
|
|
$this->parentNode->insertBefore($this->importNode($node), $refNode ?? $this); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param \chillerlan\PrototypeDOM\Node\PrototypeElement $node |
103
|
|
|
* |
104
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeElement |
105
|
|
|
*/ |
106
|
|
|
public function insert_after(PrototypeElement $node):PrototypeElement{ |
107
|
|
|
|
108
|
|
|
if(!$this->nextSibling && $this->parentNode){ |
109
|
|
|
return $this->parentNode->insert_bottom($node); // @codeCoverageIgnore |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->nextSibling->insert_before($node); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param \chillerlan\PrototypeDOM\Node\PrototypeElement $node |
117
|
|
|
* |
118
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeElement |
119
|
|
|
*/ |
120
|
|
|
public function insert_top(PrototypeElement $node):PrototypeElement{ |
121
|
|
|
|
122
|
|
|
if($this->hasChildNodes()){ |
|
|
|
|
123
|
|
|
return $this->firstChild->insert_before($node, $this->firstChild); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this->insert_bottom($node); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param \chillerlan\PrototypeDOM\Node\PrototypeElement $node |
131
|
|
|
* |
132
|
|
|
* @return \chillerlan\PrototypeDOM\Node\PrototypeElement |
133
|
|
|
*/ |
134
|
|
|
public function insert_bottom(PrototypeElement $node):PrototypeElement{ |
135
|
|
|
$this->appendChild($this->importNode($node)); |
|
|
|
|
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
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.