TagElement.php in Extensible BBCode 4.0.x
File
src/Parser/Tree/TagElement.php
View source
<?php
namespace Drupal\xbbcode\Parser\Tree;
use Drupal\xbbcode\Parser\Processor\TagProcessorInterface;
use Drupal\xbbcode\Parser\XBBCodeParser;
class TagElement extends NodeElement implements TagElementInterface {
private $processor;
private $argument;
private $source;
private $name;
private $attributes = [];
private $option;
private $parent;
private $openingName;
private $closingName;
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);
}
}
public function getName() : string {
return $this->name;
}
public function getOpeningName() : string {
return $this->openingName;
}
public function getClosingName() : string {
return $this->closingName;
}
public function setClosingName(string $closing) : TagElementInterface {
$this->closingName = $closing;
return $this;
}
public function getArgument() : string {
return $this->argument;
}
public function getAttribute(string $name) : ?string {
return $this->attributes[$name] ?? NULL;
}
public function setAttribute(string $name, string $value = NULL) : void {
$this->attributes[$name] = $value;
if ($value === NULL) {
unset($this->attributes[$name]);
}
}
public function getAttributes() : array {
return $this->attributes;
}
public function setAttributes(array $attributes) : void {
$this->attributes = $attributes;
}
public function getOption() : string {
return $this->option ?? '';
}
public function setOption(string $value) : void {
$this->option = $value;
}
public function getSource() : string {
return $this->source;
}
public function setSource(string $source) : void {
$this->source = $source;
}
public function getOuterSource() : string {
if (!isset($this->outerSource)) {
$content = $this
->getContent();
$this->outerSource = "[{$this->openingName}{$this->argument}]{$content}[/{$this->closingName}]";
}
return $this->outerSource;
}
public function getParent() : NodeElementInterface {
return $this->parent;
}
public function setParent(NodeElementInterface $parent) : void {
$this->parent = $parent;
}
public function render() : OutputElementInterface {
if (!$this
->getProcessor()) {
throw new \InvalidArgumentException("Missing processor for tag [{$this->name}]");
}
return $this
->getProcessor()
->process($this);
}
public function getProcessor() : TagProcessorInterface {
return $this->processor;
}
public function setProcessor(TagProcessorInterface $processor) : void {
$this->processor = $processor;
}
}