You are here

final class Token in Hook Event Dispatcher 8

Token ValueObject.

Convenience object to handle the integrity and assembly of tokens.

Hierarchy

  • class \Drupal\hook_event_dispatcher\Value\Token

Expanded class hierarchy of Token

4 files declare their use of Token
ExampleTokenEventSubscriber.php in src/Example/ExampleTokenEventSubscriber.php
TokenEventTest.php in tests/src/Unit/Token/TokenEventTest.php
TokensInfoEvent.php in src/Event/Token/TokensInfoEvent.php
TokenTest.php in tests/src/Unit/Token/TokenTest.php

File

src/Value/Token.php, line 14

Namespace

Drupal\hook_event_dispatcher\Value
View source
final class Token {

  /**
   * Type.
   *
   * @var string
   */
  private $type;

  /**
   * Token.
   *
   * @var string
   */
  private $token;

  /**
   * Description.
   *
   * @var string
   */
  private $description;

  /**
   * Name.
   *
   * @var string|\Drupal\Component\Render\MarkupInterface
   */
  private $name;

  /**
   * Is a dynamic field.
   *
   * @var bool
   */
  private $dynamic = FALSE;

  /**
   * Use create function instead.
   */
  private function __construct() {
  }

  /**
   * Token factory function.
   *
   * @param string $type
   *   The group name, like 'node'.
   * @param string $token
   *   The token, like 'url' or 'id'.
   * @param string|\Drupal\Component\Render\MarkupInterface $name
   *   The print-able name of the type.
   *
   * @return \Drupal\hook_event_dispatcher\Value\Token
   *   Creates a new token.
   *
   * @throws \UnexpectedValueException
   */
  public static function create($type, $token, $name) {
    $instance = new self();
    if (!is_string($type)) {
      throw new UnexpectedValueException('Type should be a string');
    }
    if (!is_string($token)) {
      throw new UnexpectedValueException('Token should be a string');
    }
    if (!is_string($name) && !$name instanceof MarkupInterface) {
      throw new UnexpectedValueException('Name should be a string or an instance of MarkupInterface');
    }
    $instance->type = $type;
    $instance->token = $token;
    $instance->name = $name;
    return $instance;
  }

  /**
   * Set description and return a new instance.
   *
   * @param string $description
   *   The description of the token type.
   *
   * @return \Drupal\hook_event_dispatcher\Value\Token
   *   New instance with the given description.
   *
   * @throws \UnexpectedValueException
   */
  public function setDescription($description) {
    if (!is_string($description) && !$description instanceof MarkupInterface) {
      throw new UnexpectedValueException('Description should be a string or an instance of MarkupInterface');
    }
    $clone = clone $this;
    $clone->description = $description;
    return $clone;
  }

  /**
   * Set whether or not the token is dynamic.
   *
   * @param bool $dynamic
   *   TRUE if the token is dynamic.
   *
   * @return \Drupal\hook_event_dispatcher\Value\Token
   *   New instance with the given dynamic.
   */
  public function setDynamic($dynamic) {
    $clone = clone $this;
    $clone->dynamic = $dynamic;
    return $clone;
  }

  /**
   * Getter.
   *
   * @return string|MarkupInterface|null
   *   The description.
   */
  public function getDescription() {
    return $this->description;
  }

  /**
   * Getter.
   *
   * @return string
   *   The type like 'node'.
   */
  public function getType() {
    return $this->type;
  }

  /**
   * Getter.
   *
   * @return string|MarkupInterface
   *   The label of the token.
   */
  public function getName() {
    return $this->name;
  }

  /**
   * Getter.
   *
   * @return string
   *   The token name like 'url'.
   */
  public function getToken() {
    return $this->token;
  }

  /**
   * Getter.
   *
   * @return bool
   *   Whether or not the token is dynamic.
   */
  public function isDynamic() {
    return $this->dynamic;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Token::$description private property Description.
Token::$dynamic private property Is a dynamic field.
Token::$name private property Name.
Token::$token private property Token.
Token::$type private property Type.
Token::create public static function Token factory function.
Token::getDescription public function Getter.
Token::getName public function Getter.
Token::getToken public function Getter.
Token::getType public function Getter.
Token::isDynamic public function Getter.
Token::setDescription public function Set description and return a new instance.
Token::setDynamic public function Set whether or not the token is dynamic.
Token::__construct private function Use create function instead.