10use BadMethodCallException;
11use InvalidArgumentException;
63 $this->store = $store;
64 $this->index = $index;
65 [
$this->name, $this->rawChildren ] = $this->store[$index];
76 public static function factory( array $store, $index ) {
77 if ( !isset( $store[$index] ) ) {
81 $descriptor = $store[$index];
82 if ( is_string( $descriptor ) ) {
83 $class = PPNode_Hash_Text::class;
84 } elseif ( is_array( $descriptor ) ) {
85 if ( $descriptor[self::NAME][0] ===
'@' ) {
86 $class = PPNode_Hash_Attr::class;
91 throw new InvalidArgumentException( __METHOD__ .
': invalid node descriptor' );
93 return new $class( $store, $index );
103 for ( $node = $this->
getFirstChild(); $node; $node = $node->getNextSibling() ) {
105 $attribs .=
' ' . $node->name .
106 '="' . htmlspecialchars( $node->value, ENT_COMPAT ) .
'"';
108 $inner .= $node->__toString();
111 if ( $inner ===
'' ) {
112 return "<{$this->name}$attribs/>";
114 return "<{$this->name}$attribs>$inner</{$this->name}>";
123 foreach ( $this->rawChildren as $i => $child ) {
137 if ( !isset( $this->rawChildren[0] ) ) {
163 foreach ( $this->rawChildren as $i => $child ) {
164 if ( is_array( $child ) && $child[self::NAME] ===
$name ) {
176 return $this->rawChildren;
220 foreach ( $children as $i => $child ) {
221 if ( !is_array( $child ) ) {
224 if ( $child[self::NAME] ===
'name' ) {
225 $bits[
'name'] =
new self( $children, $i );
226 if ( isset( $child[self::CHILDREN][0][self::NAME] )
231 } elseif ( $child[self::NAME] ===
'value' ) {
232 $bits[
'value'] =
new self( $children, $i );
236 if ( !isset( $bits[
'name'] ) ) {
237 throw new InvalidArgumentException(
'Invalid brace node passed to ' . __METHOD__ );
239 if ( !isset( $bits[
'index'] ) ) {
262 foreach ( $children as $i => $child ) {
263 if ( !is_array( $child ) ) {
266 switch ( $child[self::NAME] ) {
268 $bits[
'name'] =
new self( $children, $i );
271 $bits[
'attr'] =
new self( $children, $i );
274 $bits[
'inner'] =
new self( $children, $i );
277 $bits[
'close'] =
new self( $children, $i );
281 if ( !isset( $bits[
'name'] ) ) {
282 throw new InvalidArgumentException(
'Invalid ext node passed to ' . __METHOD__ );
293 if ( $this->name !==
'h' ) {
294 throw new BadMethodCallException(
'Invalid h node passed to ' . __METHOD__ );
306 foreach ( $children as $child ) {
307 if ( !is_array( $child ) ) {
310 if ( $child[self::NAME] ===
'@i' ) {
312 } elseif ( $child[self::NAME] ===
'@level' ) {
316 if ( !isset( $bits[
'i'] ) ) {
317 throw new InvalidArgumentException(
'Invalid h node passed to ' . __METHOD__ );
339 $bits = [
'lineStart' =>
'' ];
340 foreach ( $children as $i => $child ) {
341 if ( !is_array( $child ) ) {
344 switch ( $child[self::NAME] ) {
346 $bits[
'title'] =
new self( $children, $i );
349 $parts[] =
new self( $children, $i );
352 $bits[
'lineStart'] =
'1';
356 if ( !isset( $bits[
'title'] ) ) {
357 throw new InvalidArgumentException(
'Invalid node passed to ' . __METHOD__ );
365class_alias( PPNode_Hash_Tree::class,
'PPNode_Hash_Tree' );