Security update for Core, with self-updated composer
[yaffs-website] / web / core / lib / Drupal / Core / TypedData / ComputedItemListTrait.php
1 <?php
2
3 namespace Drupal\Core\TypedData;
4
5 /**
6  * Provides common functionality for computed item lists.
7  *
8  * @see \Drupal\Core\TypedData\ListInterface
9  * @see \Drupal\Core\TypedData\Plugin\DataType\ItemList
10  * @see \Drupal\Core\Field\FieldItemListInterface
11  * @see \Drupal\Core\Field\FieldItemList
12  *
13  * @ingroup typed_data
14  *
15  * @internal
16  *   This trait has been added in Drupal 8.4.3 as an internal helper and should
17  *   not be used outside of core.
18  */
19 trait ComputedItemListTrait {
20
21   /**
22    * Whether the values have already been computed or not.
23    *
24    * @var bool
25    */
26   protected $valueComputed = FALSE;
27
28   /**
29    * Computes the values for an item list.
30    */
31   abstract protected function computeValue();
32
33   /**
34    * Ensures that values are only computed once.
35    */
36   protected function ensureComputedValue() {
37     if ($this->valueComputed === FALSE) {
38       $this->computeValue();
39       $this->valueComputed = TRUE;
40     }
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function getValue() {
47     $this->ensureComputedValue();
48     return parent::getValue();
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function setValue($values, $notify = TRUE) {
55     parent::setValue($values, $notify);
56
57     // Make sure that subsequent getter calls do not try to compute the values
58     // again.
59     $this->valueComputed = TRUE;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function getString() {
66     $this->ensureComputedValue();
67     return parent::getString();
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function get($index) {
74     if (!is_numeric($index)) {
75       throw new \InvalidArgumentException('Unable to get a value with a non-numeric delta in a list.');
76     }
77
78     // Unlike the base implementation of
79     // \Drupal\Core\TypedData\ListInterface::get(), we do not add an empty item
80     // automatically because computed item lists need to behave like
81     // non-computed ones. For example, calling isEmpty() on a computed item list
82     // should return TRUE when the values were computed and the item list is
83     // truly empty.
84     // @see \Drupal\Core\TypedData\Plugin\DataType\ItemList::get().
85     $this->ensureComputedValue();
86
87     return isset($this->list[$index]) ? $this->list[$index] : NULL;
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function set($index, $value) {
94     $this->ensureComputedValue();
95     return parent::set($index, $value);
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function appendItem($value = NULL) {
102     $this->ensureComputedValue();
103     return parent::appendItem($value);
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function removeItem($index) {
110     $this->ensureComputedValue();
111     return parent::removeItem($index);
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   public function isEmpty() {
118     $this->ensureComputedValue();
119     return parent::isEmpty();
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   public function offsetExists($offset) {
126     $this->ensureComputedValue();
127     return parent::offsetExists($offset);
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function getIterator() {
134     $this->ensureComputedValue();
135     return parent::getIterator();
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   public function count() {
142     $this->ensureComputedValue();
143     return parent::count();
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function applyDefaultValue($notify = TRUE) {
150     // Default values do not make sense for computed item lists. However, this
151     // method can be overridden if needed.
152     return $this;
153   }
154
155 }