summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal/Core/TypedData/ListDataDefinition.php
blob: 25258a48e6d0a10add514ccdcf0d11a9c5caedc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
87
88
89
90
91
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
117
118
119
120
<?php

namespace Drupal\Core\TypedData;

/**
 * A typed data definition class for defining lists.
 */
class ListDataDefinition extends DataDefinition implements ListDataDefinitionInterface {

  /**
   * 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'.
   *
   * @return \Drupal\Core\TypedData\ListDataDefinition
   *   A new List Data Definition object.
   */
  public static function create($item_type) {
    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([], \Drupal::typedDataManager()->createDataDefinition($item_type));
  }

  /**
   * {@inheritdoc}
   */
  public function __construct(array $values = [], DataDefinitionInterface $item_definition = NULL) {
    $this->definition = $values;
    $this->itemDefinition = $item_definition;
  }

  /**
   * {@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.
      $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");
      }
      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 $this
   */
  public function setItemDefinition(DataDefinitionInterface $definition) {
    $this->itemDefinition = $definition;
    return $this;
  }

  /**
   * 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;
  }

}