001c58b28759548fdaccec663e12e838d1998931
[yaffs-website] / web / core / lib / Drupal / Core / TypedData / ListDataDefinition.php
1 <?php
2
3 namespace Drupal\Core\TypedData;
4
5 /**
6  * A typed data definition class for defining lists.
7  */
8 class ListDataDefinition extends DataDefinition implements ListDataDefinitionInterface {
9
10   /**
11    * The data definition of a list item.
12    *
13    * @var \Drupal\Core\TypedData\DataDefinitionInterface
14    */
15   protected $itemDefinition;
16
17   /**
18    * Creates a new list definition.
19    *
20    * @param string $item_type
21    *   The data type of the list items; e.g., 'string', 'integer' or 'any'.
22    *
23    * @return \Drupal\Core\TypedData\ListDataDefinition
24    *   A new List Data Definition object.
25    */
26   public static function create($item_type) {
27     return static::createFromItemType($item_type);
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   public static function createFromDataType($type) {
34     $definition = parent::createFromDataType($type);
35     // If nothing else given, default to a list of 'any' items.
36     $definition->itemDefinition = DataDefinition::create('any');
37     return $definition;
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public static function createFromItemType($item_type) {
44     return new static([], \Drupal::typedDataManager()->createDataDefinition($item_type));
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function __construct(array $values = [], DataDefinitionInterface $item_definition = NULL) {
51     $this->definition = $values;
52     $this->itemDefinition = $item_definition;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function getDataType() {
59     return 'list';
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function setDataType($type) {
66     if ($type != 'list') {
67       throw new \LogicException('Lists must always be of data type "list".');
68     }
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getClass() {
75     if (!empty($this->definition['class'])) {
76       return $this->definition['class'];
77     }
78
79     // If a list definition is used but no class has been specified, derive the
80     // default list class from the item type.
81     $item_type_definition = \Drupal::typedDataManager()
82       ->getDefinition($this->getItemDefinition()->getDataType());
83     if (!$item_type_definition) {
84       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    * {@inheritdoc}
91    */
92   public function getItemDefinition() {
93     return $this->itemDefinition;
94   }
95
96   /**
97    * Sets the item definition.
98    *
99    * @param \Drupal\Core\TypedData\DataDefinition $definition
100    *   A list item's data definition.
101    *
102    * @return $this
103    */
104   public function setItemDefinition(DataDefinitionInterface $definition) {
105     $this->itemDefinition = $definition;
106     return $this;
107   }
108
109   /**
110    * Magic method: Implements a deep clone.
111    */
112   public function __clone() {
113     // Ensure the itemDefinition property is actually cloned by overwriting the
114     // original reference.
115     $this->itemDefinition = clone $this->itemDefinition;
116   }
117
118 }