You are here

NodeElement.php in Extensible BBCode 8.3

Same filename and directory in other branches
  1. 4.0.x src/Parser/Tree/NodeElement.php

File

src/Parser/Tree/NodeElement.php
View source
<?php

namespace Drupal\xbbcode\Parser\Tree;

use Iterator;

/**
 * A node element contains other elements.
 */
abstract class NodeElement implements NodeElementInterface {

  /**
   * The children of this node.
   *
   * @var \Drupal\xbbcode\Parser\Tree\ElementInterface[]
   */
  protected $children = [];

  /**
   * The rendered children of this node.
   *
   * @var \Drupal\xbbcode\Parser\Tree\OutputElementInterface[]
   */
  protected $output;

  /**
   * {@inheritdoc}
   */
  public function append(ElementInterface $element) : void {
    $this->children[] = $element;
  }

  /**
   * {@inheritdoc}
   */
  public function getChildren() : array {
    return $this->children;
  }

  /**
   * {@inheritdoc}
   */
  public function getContent() : string {
    return implode('', $this
      ->getRenderedChildren());
  }

  /**
   * {@inheritdoc}
   */
  public function getRenderedChildren($force_render = TRUE) : array {
    if (!$force_render) {
      return $this->output ?? [];
    }
    if ($this->output === NULL) {
      $this->output = [];
      foreach ($this->children as $child) {
        $this->output[] = $child
          ->render();
      }
    }
    return $this->output;
  }

  /**
   * {@inheritdoc}
   */
  public function getDescendants() : Iterator {
    foreach ($this->children as $child) {
      (yield $child);
      if ($child instanceof NodeElementInterface) {
        yield from $child
          ->getDescendants();
      }
    }
  }

}

Classes

Namesort descending Description
NodeElement A node element contains other elements.