3109cd2115678dd37f4b520930e1c83b12715fd8
[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     $class = isset($this->definition['class']) ? $this->definition['class'] : NULL;
76     if (!empty($class)) {
77       return $class;
78     }
79     else {
80       // If a list definition is used but no class has been specified, derive
81       // the default list class from the item type.
82       $item_type_definition = \Drupal::typedDataManager()
83         ->getDefinition($this->getItemDefinition()->getDataType());
84       if (!$item_type_definition) {
85         throw new \LogicException("An invalid data type '{$this->getItemDefinition()->getDataType()}' has been specified for list items");
86       }
87       return $item_type_definition['list_class'];
88     }
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function getItemDefinition() {
95     return $this->itemDefinition;
96   }
97
98   /**
99    * Sets the item definition.
100    *
101    * @param \Drupal\Core\TypedData\DataDefinition $definition
102    *   A list item's data definition.
103    *
104    * @return $this
105    */
106   public function setItemDefinition(DataDefinitionInterface $definition) {
107     $this->itemDefinition = $definition;
108     return $this;
109   }
110
111 }