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

/**
 * @file
5
 * Contains \Drupal\Core\TypedData\ListDataDefinition.
6 7 8 9 10
 */

namespace Drupal\Core\TypedData;

/**
11
 * A typed data definition class for defining lists.
12
 */
13
class ListDataDefinition extends DataDefinition implements ListDataDefinitionInterface {
14 15 16 17 18 19 20 21 22 23 24 25 26 27

  /**
   * 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'.
   *
28
   * @return \Drupal\Core\TypedData\ListDataDefinition
29 30 31
   *   A new List Data Definition object.
   */
  public static function create($item_type) {
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    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) {
    return new static(array(), \Drupal::typedDataManager()->createDataDefinition($item_type));
50 51 52 53 54
  }

  /**
   * {@inheritdoc}
   */
55 56 57
  public function __construct(array $values = array(), DataDefinitionInterface $item_definition = NULL) {
    $this->definition = $values;
    $this->itemDefinition = $item_definition;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  }

  /**
   * {@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() {
    $class = isset($this->definition['class']) ? $this->definition['class'] : NULL;
    if (!empty($class)) {
      return $class;
    }
    else {
      // If a list definition is used but no class has been specified, derive
      // the default list class from the item type.
87
      $item_type_definition = \Drupal::typedDataManager()
88
        ->getDefinition($this->getItemDefinition()->getDataType());
89 90 91
      if (!$item_type_definition) {
        throw new \LogicException(format_string('An invalid data type @plugin_id has been specified for list items.', array('@plugin_id' => $this->getItemDefinition()->getDataType())));
      }
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
      return $item_type_definition['list_class'];
    }
  }

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

  /**
   * Sets the item definition.
   *
   * @param \Drupal\Core\TypedData\DataDefinition $definition
   *   A list item's data definition.
   *
   * @return self
   *   The object itself for chaining.
   */
  public function setItemDefinition(DataDefinitionInterface $definition) {
    $this->itemDefinition = $definition;
    return $this;
  }
}