You are here

class TagElement in Extensible BBCode 4.0.x

Same name and namespace in other branches
  1. 8.3 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 11

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;

  /**
   * Opening tag name.
   *
   * @var string
   */
  private $openingName;

  /**
   * Closing tag name.
   *
   * @var string
   */
  private $closingName;

  /**
   * TagElement constructor.
   *
   * @param string $opening
   *   The opening tag name.
   * @param string $argument
   *   The argument (everything past the tag name)
   * @param string $source
   *   The source of the content.
   */
  public function __construct(string $opening, string $argument, string $source) {
    $this->name = mb_strtolower($opening);
    $this->openingName = $opening;
    $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 getOpeningName() : string {
    return $this->openingName;
  }

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

  /**
   * {@inheritdoc}
   */
  public function setClosingName(string $closing) : TagElementInterface {
    $this->closingName = $closing;
    return $this;
  }

  /**
   * {@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->openingName}{$this->argument}]{$content}[/{$this->closingName}]";
    }
    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::$closingName private property Closing tag name.
TagElement::$name private property The tag name.
TagElement::$openingName private property Opening 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::getClosingName public function Get the original closing tag name. Overrides TagElementInterface::getClosingName
TagElement::getName public function Get the canonical (lower-case) tag name of this element. Overrides TagElementInterface::getName
TagElement::getOpeningName public function Get the original opening tag name. Overrides TagElementInterface::getOpeningName
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::setClosingName public function Get the original closing tag name. Overrides TagElementInterface::setClosingName
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.