ListDataDefinition.php 2.91 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\Core\TypedData;

/**
6
 * A typed data definition class for defining lists.
7
 */
8
class ListDataDefinition extends DataDefinition implements ListDataDefinitionInterface {
9 10 11 12 13 14 15 16 17 18 19 20 21 22

  /**
   * The data definition of a list item.
   *
   * @var \Drupal\Core\TypedData\DataDefinitionInterface
   */
  protected $itemDefinition;

  /**
   * Creates a new list definition.
   *
   * @param string $item_type
   *   The data type of the list items; e.g., 'string', 'integer' or 'any'.
   *
23
   * @return \Drupal\Core\TypedData\ListDataDefinition
24 25 26
   *   A new List Data Definition object.
   */
  public static function create($item_type) {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    return static::createFromItemType($item_type);
  }

  /**
   * {@inheritdoc}
   */
  public static function createFromDataType($type) {
    $definition = parent::createFromDataType($type);
    // If nothing else given, default to a list of 'any' items.
    $definition->itemDefinition = DataDefinition::create('any');
    return $definition;
  }

  /**
   * {@inheritdoc}
   */
  public static function createFromItemType($item_type) {
44
    return new static([], \Drupal::typedDataManager()->createDataDefinition($item_type));
45 46 47 48 49
  }

  /**
   * {@inheritdoc}
   */
50
  public function __construct(array $values = [], DataDefinitionInterface $item_definition = NULL) {
51 52
    $this->definition = $values;
    $this->itemDefinition = $item_definition;
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  }

  /**
   * {@inheritdoc}
   */
  public function getDataType() {
    return 'list';
  }

  /**
   * {@inheritdoc}
   */
  public function setDataType($type) {
    if ($type != 'list') {
      throw new \LogicException('Lists must always be of data type "list".');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getClass() {
75 76
    if (!empty($this->definition['class'])) {
      return $this->definition['class'];
77
    }
78 79 80 81 82 83 84

    // If a list definition is used but no class has been specified, derive the
    // default list class from the item type.
    $item_type_definition = \Drupal::typedDataManager()
      ->getDefinition($this->getItemDefinition()->getDataType());
    if (!$item_type_definition) {
      throw new \LogicException("An invalid data type '{$this->getItemDefinition()->getDataType()}' has been specified for list items");
85
    }
86
    return $item_type_definition['list_class'];
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  }

  /**
   * {@inheritdoc}
   */
  public function getItemDefinition() {
    return $this->itemDefinition;
  }

  /**
   * Sets the item definition.
   *
   * @param \Drupal\Core\TypedData\DataDefinition $definition
   *   A list item's data definition.
   *
102
   * @return $this
103 104 105 106 107
   */
  public function setItemDefinition(DataDefinitionInterface $definition) {
    $this->itemDefinition = $definition;
    return $this;
  }
108

109 110 111 112 113 114 115 116 117
  /**
   * Magic method: Implements a deep clone.
   */
  public function __clone() {
    // Ensure the itemDefinition property is actually cloned by overwriting the
    // original reference.
    $this->itemDefinition = clone $this->itemDefinition;
  }

118
}