You are here

class TagElement in Extensible BBCode 8.3

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

A BBCode tag element.

Hierarchy

Expanded class hierarchy of TagElement

2 files declare their use of TagElement
TreeEncodeTrait.php in standard/src/TreeEncodeTrait.php
XBBCodeParser.php in src/Parser/XBBCodeParser.php

File

src/Parser/Tree/TagElement.php, line 12

Namespace

Drupal\xbbcode\Parser\Tree
View source
class TagElement extends NodeElement implements TagElementInterface {

  /**
   * The processor handling this element.
   *
   * @var \Drupal\xbbcode\Parser\Processor\TagProcessorInterface
   */
  private $processor;

  /**
   * The tag argument.
   *
   * @var string
   */
  private $argument;

  /**
   * The tag content source.
   *
   * @var string
   */
  private $source;

  /**
   * The tag name.
   *
   * @var string
   */
  private $name;

  /**
   * The tag attributes.
   *
   * @var string[]
   */
  private $attributes = [];

  /**
   * The tag option.
   *
   * @var string
   */
  private $option;

  /**
   * The tag's parent element.
   *
   * @var \Drupal\xbbcode\Parser\Tree\NodeElementInterface
   */
  private $parent;

  /**
   * TagElement constructor.
   *
   * @param string $name
   *   The tag name.
   * @param string $argument
   *   The argument (everything past the tag name)
   * @param string $source
   *   The source of the content.
   */
  public function __construct($name, $argument, $source) {
    $this->name = $name;
    $this->argument = $argument;
    $this->source = $source;
    if ($argument && $argument[0] === '=') {
      $this->option = XBBCodeParser::parseOption($argument);
    }
    else {
      $this->attributes = XBBCodeParser::parseAttributes($argument);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getName() : string {
    return $this->name;
  }

  /**
   * {@inheritdoc}
   */
  public function getArgument() : string {
    return $this->argument;
  }

  /**
   * {@inheritdoc}
   */
  public function getAttribute(string $name) : ?string {
    return $this->attributes[$name] ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function setAttribute(string $name, string $value = NULL) : void {
    $this->attributes[$name] = $value;
    if ($value === NULL) {
      unset($this->attributes[$name]);
    }
  }

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

  /**
   * {@inheritdoc}
   */
  public function setAttributes(array $attributes) : void {
    $this->attributes = $attributes;
  }

  /**
   * {@inheritdoc}
   */
  public function getOption() : string {
    return $this->option ?? '';
  }

  /**
   * {@inheritdoc}
   */
  public function setOption(string $value) : void {
    $this->option = $value;
  }

  /**
   * {@inheritdoc}
   */
  public function getSource() : string {
    return $this->source;
  }

  /**
   * {@inheritdoc}
   */
  public function setSource(string $source) : void {
    $this->source = $source;
  }

  /**
   * {@inheritdoc}
   */
  public function getOuterSource() : string {

    // Reconstruct the opening and closing tags, but render the content.
    if (!isset($this->outerSource)) {
      $content = $this
        ->getContent();
      $this->outerSource = "[{$this->name}{$this->argument}]{$content}[/{$this->name}]";
    }
    return $this->outerSource;
  }

  /**
   * {@inheritdoc}
   */
  public function getParent() : NodeElementInterface {
    return $this->parent;
  }

  /**
   * {@inheritdoc}
   */
  public function setParent(NodeElementInterface $parent) : void {
    $this->parent = $parent;
  }

  /**
   * {@inheritdoc}
   *
   * @throws \InvalidArgumentException
   *   If the tag does not have an assigned processor.
   */
  public function render() : OutputElementInterface {
    if (!$this
      ->getProcessor()) {
      throw new InvalidArgumentException("Missing processor for tag [{$this->name}]");
    }
    return $this
      ->getProcessor()
      ->process($this);
  }

  /**
   * {@inheritdoc}
   */
  public function getProcessor() : TagProcessorInterface {
    return $this->processor;
  }

  /**
   * {@inheritdoc}
   */
  public function setProcessor(TagProcessorInterface $processor) : void {
    $this->processor = $processor;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
TagElement::$argument private property The tag argument.
TagElement::$attributes private property The tag attributes.
TagElement::$name private property The tag name.
TagElement::$option private property The tag option.
TagElement::$parent private property The tag's parent element.
TagElement::$processor private property The processor handling this element.
TagElement::$source private property The tag content source.
TagElement::getArgument public function Retrieve the unparsed argument string. Overrides TagElementInterface::getArgument
TagElement::getAttribute public function Retrieve a particular attribute of the element. Overrides TagElementInterface::getAttribute
TagElement::getAttributes public function Return all attribute values. Overrides TagElementInterface::getAttributes
TagElement::getName public function Get the tag name of this element. Overrides TagElementInterface::getName
TagElement::getOption public function Retrieve the option-type attribute of the element. Overrides TagElementInterface::getOption
TagElement::getOuterSource public function Retrieve the content including the opening and closing tags. Overrides TagElementInterface::getOuterSource
TagElement::getParent public function Retrieve the parent of the current tag. Overrides TagElementInterface::getParent
TagElement::getProcessor public function Get the assigned processor. Overrides TagElementInterface::getProcessor
TagElement::getSource public function Retrieve the content source of the tag. Overrides TagElementInterface::getSource
TagElement::render public function Overrides ElementInterface::render
TagElement::setAttribute public function Set an attribute of the element. Overrides TagElementInterface::setAttribute
TagElement::setAttributes public function Set all attribute values. Overrides TagElementInterface::setAttributes
TagElement::setOption public function Set the option-style attribute of the element. Overrides TagElementInterface::setOption
TagElement::setParent public function Set the parent of the current tag. Overrides TagElementInterface::setParent
TagElement::setProcessor public function Assign a processor to this tag element. Overrides TagElementInterface::setProcessor
TagElement::setSource public function Set the content source of the tag. Overrides TagElementInterface::setSource
TagElement::__construct public function TagElement constructor.