24use BadMethodCallException;
25use InvalidArgumentException;
77 $this->store = $store;
78 $this->index = $index;
79 [
$this->name, $this->rawChildren ] = $this->store[$index];
90 public static function factory( array $store, $index ) {
91 if ( !isset( $store[$index] ) ) {
95 $descriptor = $store[$index];
96 if ( is_string( $descriptor ) ) {
97 $class = PPNode_Hash_Text::class;
98 } elseif ( is_array( $descriptor ) ) {
99 if ( $descriptor[self::NAME][0] ===
'@' ) {
100 $class = PPNode_Hash_Attr::class;
102 $class = self::class;
105 throw new InvalidArgumentException( __METHOD__ .
': invalid node descriptor' );
107 return new $class( $store, $index );
117 for ( $node = $this->
getFirstChild(); $node; $node = $node->getNextSibling() ) {
119 $attribs .=
' ' . $node->name .
120 '="' . htmlspecialchars( $node->value, ENT_COMPAT ) .
'"';
122 $inner .= $node->__toString();
125 if ( $inner ===
'' ) {
126 return "<{$this->name}$attribs/>";
128 return "<{$this->name}$attribs>$inner</{$this->name}>";
137 foreach ( $this->rawChildren as $i => $child ) {
151 if ( !isset( $this->rawChildren[0] ) ) {
177 foreach ( $this->rawChildren as $i => $child ) {
178 if ( is_array( $child ) && $child[self::NAME] ===
$name ) {
190 return $this->rawChildren;
234 foreach ( $children as $i => $child ) {
235 if ( !is_array( $child ) ) {
238 if ( $child[self::NAME] ===
'name' ) {
239 $bits[
'name'] =
new self( $children, $i );
240 if ( isset( $child[self::CHILDREN][0][self::NAME] )
245 } elseif ( $child[self::NAME] ===
'value' ) {
246 $bits[
'value'] =
new self( $children, $i );
250 if ( !isset( $bits[
'name'] ) ) {
251 throw new InvalidArgumentException(
'Invalid brace node passed to ' . __METHOD__ );
253 if ( !isset( $bits[
'index'] ) ) {
276 foreach ( $children as $i => $child ) {
277 if ( !is_array( $child ) ) {
280 switch ( $child[self::NAME] ) {
282 $bits[
'name'] =
new self( $children, $i );
285 $bits[
'attr'] =
new self( $children, $i );
288 $bits[
'inner'] =
new self( $children, $i );
291 $bits[
'close'] =
new self( $children, $i );
295 if ( !isset( $bits[
'name'] ) ) {
296 throw new InvalidArgumentException(
'Invalid ext node passed to ' . __METHOD__ );
307 if ( $this->name !==
'h' ) {
308 throw new BadMethodCallException(
'Invalid h node passed to ' . __METHOD__ );
320 foreach ( $children as $child ) {
321 if ( !is_array( $child ) ) {
324 if ( $child[self::NAME] ===
'@i' ) {
326 } elseif ( $child[self::NAME] ===
'@level' ) {
330 if ( !isset( $bits[
'i'] ) ) {
331 throw new InvalidArgumentException(
'Invalid h node passed to ' . __METHOD__ );
353 $bits = [
'lineStart' =>
'' ];
354 foreach ( $children as $i => $child ) {
355 if ( !is_array( $child ) ) {
358 switch ( $child[self::NAME] ) {
360 $bits[
'title'] =
new self( $children, $i );
363 $parts[] =
new self( $children, $i );
366 $bits[
'lineStart'] =
'1';
370 if ( !isset( $bits[
'title'] ) ) {
371 throw new InvalidArgumentException(
'Invalid node passed to ' . __METHOD__ );
379class_alias( PPNode_Hash_Tree::class,
'PPNode_Hash_Tree' );