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