X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Flib%2FDrupal%2FCore%2FTypedData%2FComputedItemListTrait.php;fp=web%2Fcore%2Flib%2FDrupal%2FCore%2FTypedData%2FComputedItemListTrait.php;h=7a823a474252108c2f10ccc6b3e22fb4d93d0174;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/lib/Drupal/Core/TypedData/ComputedItemListTrait.php b/web/core/lib/Drupal/Core/TypedData/ComputedItemListTrait.php new file mode 100644 index 000000000..7a823a474 --- /dev/null +++ b/web/core/lib/Drupal/Core/TypedData/ComputedItemListTrait.php @@ -0,0 +1,155 @@ +valueComputed === FALSE) { + $this->computeValue(); + $this->valueComputed = TRUE; + } + } + + /** + * {@inheritdoc} + */ + public function getValue() { + $this->ensureComputedValue(); + return parent::getValue(); + } + + /** + * {@inheritdoc} + */ + public function setValue($values, $notify = TRUE) { + parent::setValue($values, $notify); + + // Make sure that subsequent getter calls do not try to compute the values + // again. + $this->valueComputed = TRUE; + } + + /** + * {@inheritdoc} + */ + public function getString() { + $this->ensureComputedValue(); + return parent::getString(); + } + + /** + * {@inheritdoc} + */ + public function get($index) { + if (!is_numeric($index)) { + throw new \InvalidArgumentException('Unable to get a value with a non-numeric delta in a list.'); + } + + // Unlike the base implementation of + // \Drupal\Core\TypedData\ListInterface::get(), we do not add an empty item + // automatically because computed item lists need to behave like + // non-computed ones. For example, calling isEmpty() on a computed item list + // should return TRUE when the values were computed and the item list is + // truly empty. + // @see \Drupal\Core\TypedData\Plugin\DataType\ItemList::get(). + $this->ensureComputedValue(); + + return isset($this->list[$index]) ? $this->list[$index] : NULL; + } + + /** + * {@inheritdoc} + */ + public function set($index, $value) { + $this->ensureComputedValue(); + return parent::set($index, $value); + } + + /** + * {@inheritdoc} + */ + public function appendItem($value = NULL) { + $this->ensureComputedValue(); + return parent::appendItem($value); + } + + /** + * {@inheritdoc} + */ + public function removeItem($index) { + $this->ensureComputedValue(); + return parent::removeItem($index); + } + + /** + * {@inheritdoc} + */ + public function isEmpty() { + $this->ensureComputedValue(); + return parent::isEmpty(); + } + + /** + * {@inheritdoc} + */ + public function offsetExists($offset) { + $this->ensureComputedValue(); + return parent::offsetExists($offset); + } + + /** + * {@inheritdoc} + */ + public function getIterator() { + $this->ensureComputedValue(); + return parent::getIterator(); + } + + /** + * {@inheritdoc} + */ + public function count() { + $this->ensureComputedValue(); + return parent::count(); + } + + /** + * {@inheritdoc} + */ + public function applyDefaultValue($notify = TRUE) { + // Default values do not make sense for computed item lists. However, this + // method can be overridden if needed. + return $this; + } + +}