TagElement.php in Extensible BBCode 8.3
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;
use InvalidArgumentException;
class TagElement extends NodeElement implements TagElementInterface {
private $processor;
private $argument;
private $source;
private $name;
private $attributes = [];
private $option;
private $parent;
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);
}
}
public function getName() : string {
return $this->name;
}
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->name}{$this->argument}]{$content}[/{$this->name}]";
}
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;
}
}