-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnclosingElement.php
116 lines (103 loc) · 2.03 KB
/
EnclosingElement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace wpscholar\Elements;
/**
* Class EnclosingElement
*
* @property string $content
* @property string $children
*/
class EnclosingElement extends ElementNode {
/**
* Storage for child nodes
*
* @var array
*/
protected $_children = array();
/**
* Prepend a node
*
* @param Node $node
*/
public function prepend( Node $node ) {
$node->parent = $this;
array_unshift( $this->_children, $node );
}
/**
* Append a node
*
* @param string|Node $node
*/
public function append( $node ) {
if ( is_string( $node ) ) {
$node = new TextNode( $node );
}
if ( ! is_object( $node ) || ! $node instanceof Node ) {
throw new \InvalidArgumentException( 'Invalid node' );
}
$node->parent = $this;
array_push( $this->_children, $node );
}
/**
* Remove a node by index
*
* @param int $index
*/
public function remove( $index ) {
unset( $this->_children[ $index ] );
}
/**
* Remove all nodes
*/
public function removeAll() {
$this->_children = array();
}
/**
* Get the element's content
*
* @return string
*/
final public function get_content() {
return join( PHP_EOL, $this->_children );
}
/**
* Set element content
*
* @param Node|string|array $content
*/
final public function set_content( $content ) {
$this->removeAll();
$nodes = is_array( $content ) ? $content : [ $content ];
foreach ( $nodes as $node ) {
if ( is_string( $node ) ) {
$node = new TextNode( $node );
} else if ( is_array( $node ) ) {
$node = ElementFactory::buildElement( $node );
}
$this->append( $node );
}
}
/**
* Get children
*
* @return array
*/
final public function get_children() {
return $this->_children;
}
/**
* Set children
*
* @param Node|string|array $children
*/
final public function set_children( $children ) {
$this->set_content( $children );
}
/**
* Return element as a string
*
* @return string
*/
final public function __toString() {
return "<{$this->tag} {$this->atts}>{$this->content}</{$this->tag}>";
}
}