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
|
|
|
* @noinspection PhpParamsInspection |
13
|
|
|
* @noinspection PhpIncompatibleReturnTypeInspection |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace chillerlan\PrototypeDOM\Node; |
17
|
|
|
|
18
|
|
|
use function array_key_exists, call_user_func_array, is_array; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @implements \chillerlan\PrototypeDOM\Node\PrototypeElement |
22
|
|
|
*/ |
23
|
|
|
trait PrototypeElementTrait{ |
24
|
|
|
use PrototypeTraversalTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
|
|
public function wrap(PrototypeElement $wrapper):PrototypeElement{ |
30
|
|
|
return $wrapper->insert($this->replace($wrapper)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritDoc |
35
|
|
|
*/ |
36
|
|
|
public function update($content):PrototypeElement{ |
37
|
|
|
return $this->purge()->insert($content); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritDoc |
42
|
|
|
*/ |
43
|
|
|
public function insert($content):PrototypeElement{ |
44
|
|
|
|
45
|
|
|
if(!is_array($content)){ |
46
|
|
|
|
47
|
|
|
foreach($this->ownerDocument->toNodeList($content) as $node){ |
48
|
|
|
$this->insert_bottom($node); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
foreach(['before', 'after', 'top', 'bottom'] as $pos){ |
55
|
|
|
|
56
|
|
|
if(!array_key_exists($pos, $content)){ |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$nodes = $this->ownerDocument->toNodeList($content[$pos]); |
61
|
|
|
|
62
|
|
|
if($pos === 'top' && $this->hasChildNodes() || $pos === 'after' && $this->nextSibling){ |
|
|
|
|
63
|
|
|
$nodes->reverse(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
foreach($nodes as $node){ |
67
|
|
|
call_user_func_array([$this, 'insert_'.$pos], [$node]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritDoc |
77
|
|
|
*/ |
78
|
|
|
public function insert_before(PrototypeElement $node, PrototypeElement $refNode = null):PrototypeElement{ |
79
|
|
|
|
80
|
|
|
if($this->parentNode){ |
81
|
|
|
$this->parentNode->insertBefore($this->importNode($node), $refNode ?? $this); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritDoc |
89
|
|
|
*/ |
90
|
|
|
public function insert_after(PrototypeElement $node):PrototypeElement{ |
91
|
|
|
|
92
|
|
|
if(!$this->nextSibling && $this->parentNode){ |
93
|
|
|
return $this->parentNode->insert_bottom($node); // @codeCoverageIgnore |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->nextSibling->insert_before($node); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritDoc |
101
|
|
|
*/ |
102
|
|
|
public function insert_top(PrototypeElement $node):PrototypeElement{ |
103
|
|
|
|
104
|
|
|
if($this->hasChildNodes()){ |
105
|
|
|
return $this->firstChild->insert_before($node, $this->firstChild); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->insert_bottom($node); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritDoc |
113
|
|
|
*/ |
114
|
|
|
public function insert_bottom(PrototypeElement $node):PrototypeElement{ |
115
|
|
|
$this->appendChild($this->importNode($node)); |
|
|
|
|
116
|
|
|
|
117
|
|
|
return $this; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|