You are here

abstract class NodeElement in Extensible BBCode 8.3

Same name and namespace in other branches
  1. 4.0.x src/Parser/Tree/NodeElement.php \Drupal\xbbcode\Parser\Tree\NodeElement

A node element contains other elements.

Hierarchy

Expanded class hierarchy of NodeElement

File

src/Parser/Tree/NodeElement.php, line 10

Namespace

Drupal\xbbcode\Parser\Tree
View source
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();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ElementInterface::render public function Render this element to a string. 4
NodeElement::$children protected property The children of this node.
NodeElement::$output protected property The rendered children of this node.
NodeElement::append public function Append an element to the children of this element. Overrides NodeElementInterface::append
NodeElement::getChildren public function Get all children of the element. Overrides NodeElementInterface::getChildren
NodeElement::getContent public function Retrieve the rendered content of the element. Overrides NodeElementInterface::getContent
NodeElement::getDescendants public function Retrieve the descendants of the node. Overrides NodeElementInterface::getDescendants
NodeElement::getRenderedChildren public function Retrieve the rendered output of each child. Overrides NodeElementInterface::getRenderedChildren