devel_wizard-2.x-dev/src/Spell/SpellDefinition.php

src/Spell/SpellDefinition.php
<?php

declare(strict_types=1);

namespace Drupal\devel_wizard\Spell;

use Drupal\Component\Plugin\Definition\PluginDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;

class SpellDefinition extends PluginDefinition implements \JsonSerializable {

  /**
   * The human-readable category.
   */
  protected string|TranslatableMarkup $category;

  /**
   * Gets the human-readable category of the Spell definition.
   */
  public function getCategory(): string|TranslatableMarkup {
    return $this->category;
  }

  /**
   * Sets the human-readable category of the Spell definition.
   */
  public function setCategory(string|TranslatableMarkup $category): static {
    $this->category = $category;

    return $this;
  }

  /**
   * The human-readable name.
   */
  protected string|TranslatableMarkup $label;

  /**
   * Gets the human-readable name of the Spell definition.
   */
  public function getLabel(): string|TranslatableMarkup {
    return $this->label;
  }

  /**
   * Sets the human-readable name of the Spell definition.
   */
  public function setLabel(string|TranslatableMarkup $label): static {
    $this->label = $label;

    return $this;
  }

  /**
   * An optional description for advanced Spells.
   */
  protected string|TranslatableMarkup $description;

  /**
   * Gets the description of the Spell definition.
   */
  public function getDescription(): string|TranslatableMarkup {
    return $this->description;
  }

  /**
   * Sets the description of the Spell definition.
   */
  public function setDescription(string|TranslatableMarkup $description): static {
    $this->description = $description;

    return $this;
  }

  /**
   * @var array<string, \Drupal\Core\StringTranslation\TranslatableMarkup>
   */
  protected array $tags = [];

  /**
   * @return array<string, \Drupal\Core\StringTranslation\TranslatableMarkup>
   */
  public function getTags(): array {
    return $this->tags;
  }

  /**
   * @param array<string, \Drupal\Core\StringTranslation\TranslatableMarkup> $tags
   */
  public function setTags(array $tags): static {
    $this->tags = $tags;

    return $this;
  }

  protected array $additional = [];

  public function __construct(array $definition) {
    if (array_key_exists('category', $definition)) {
      $this->setCategory($definition['category']);
    }

    if (array_key_exists('label', $definition)) {
      $this->setLabel($definition['label']);
    }

    if (array_key_exists('description', $definition)) {
      $this->setDescription($definition['description']);
    }

    if (array_key_exists('tags', $definition)) {
      $this->setTags($definition['tags']);
    }

    unset(
      $definition['category'],
      $definition['label'],
      $definition['description'],
      $definition['tags'],
    );

    foreach ($definition as $property => $value) {
      $this->set($property, $value);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function jsonSerialize(): array {
    $state = [
      'provider' => $this->getProvider(),
      'id' => $this->id(),
      'class' => $this->getClass(),
      'category' => $this->getCategory()->getUntranslatedString(),
      'label' => $this->getLabel()->getUntranslatedString(),
      'description' => $this->getDescription()->getUntranslatedString(),
      'tags' => [],
    ];
    $state += $this->additional;

    foreach ($this->getTags() as $tagKey => $tagLabel) {
      $state['tags'][$tagKey] = $tagLabel->getUntranslatedString();
    }

    return $state;
  }

  /**
   * Gets any arbitrary property.
   */
  public function get(string $property): mixed {
    if (property_exists($this, $property)) {
      return $this->{$property};
    }

    return $this->additional[$property] ?? NULL;
  }

  /**
   * Sets a value to an arbitrary property.
   */
  public function set(string $property, mixed $value): static {
    // @todo Do the following keys need protection: provider, id, class?
    if (property_exists($this, $property)) {
      $this->{$property} = $value;

      return $this;
    }

    $this->additional[$property] = $value;

    return $this;
  }

  public function getAbracadabraUrl(): Url {
    return Url::fromRoute(
      'devel_wizard.spell_form.abracadabra',
      [
        'spell' => $this->id(),
      ],
    );
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc