ItemList.php in Zircon Profile 8.0
File
core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php
View source
<?php
namespace Drupal\Core\TypedData\Plugin\DataType;
use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\Core\TypedData\ListInterface;
use Drupal\Core\TypedData\TypedData;
use Drupal\Core\TypedData\TypedDataInterface;
class ItemList extends TypedData implements \IteratorAggregate, ListInterface {
protected $list = array();
public function getValue() {
$values = array();
foreach ($this->list as $delta => $item) {
$values[$delta] = $item
->getValue();
}
return $values;
}
public function setValue($values, $notify = TRUE) {
if (!isset($values) || $values === array()) {
$this->list = array();
}
else {
if (!is_array($values)) {
throw new \InvalidArgumentException('Cannot set a list with a non-array value.');
}
foreach (array_values($values) as $delta => $value) {
if (!isset($this->list[$delta])) {
$this->list[$delta] = $this
->createItem($delta, $value);
}
else {
$this->list[$delta]
->setValue($value, FALSE);
}
}
$this->list = array_slice($this->list, 0, count($values));
}
if ($notify && isset($this->parent)) {
$this->parent
->onChange($this->name);
}
}
public function getString() {
$strings = array();
foreach ($this->list as $item) {
$strings[] = $item
->getString();
}
return implode(', ', array_filter($strings, '\\Drupal\\Component\\Utility\\Unicode::strlen'));
}
public function get($index) {
if (!is_numeric($index)) {
throw new \InvalidArgumentException('Unable to get a value with a non-numeric delta in a list.');
}
if ($index == 0 && !isset($this->list[0]) && $this->definition
->isComputed()) {
$this->list[0] = $this
->createItem(0);
}
return isset($this->list[$index]) ? $this->list[$index] : NULL;
}
public function set($index, $value) {
if (!is_numeric($index)) {
throw new \InvalidArgumentException('Unable to set a value with a non-numeric delta in a list.');
}
if ($index < 0 || $index > count($this->list)) {
throw new \InvalidArgumentException('Unable to set a value to a non-subsequent delta in a list.');
}
if ($value instanceof TypedDataInterface) {
$value = $value
->getValue();
}
$item = isset($this->list[$index]) ? $this->list[$index] : $this
->appendItem();
$item
->setValue($value);
return $this;
}
public function removeItem($index) {
if (isset($this->list) && array_key_exists($index, $this->list)) {
unset($this->list[$index]);
$this
->rekey($index);
}
else {
throw new \InvalidArgumentException('Unable to remove item at non-existing index.');
}
return $this;
}
protected function rekey($from_index = 0) {
$this->list = array_values($this->list);
for ($i = $from_index; $i < count($this->list); $i++) {
$this->list[$i]
->setContext($i, $this);
}
}
public function first() {
return $this
->get(0);
}
public function offsetExists($offset) {
return isset($this->list[$offset]);
}
public function offsetUnset($offset) {
$this
->removeItem($offset);
}
public function offsetGet($offset) {
return $this
->get($offset);
}
public function offsetSet($offset, $value) {
if (!isset($offset)) {
$this
->appendItem($value);
}
else {
$this
->set($offset, $value);
}
}
public function appendItem($value = NULL) {
$offset = count($this->list);
$item = $this
->createItem($offset, $value);
$this->list[$offset] = $item;
return $item;
}
protected function createItem($offset = 0, $value = NULL) {
return $this
->getTypedDataManager()
->getPropertyInstance($this, $offset, $value);
}
public function getItemDefinition() {
return $this->definition
->getItemDefinition();
}
public function getIterator() {
return new \ArrayIterator($this->list);
}
public function count() {
return count($this->list);
}
public function isEmpty() {
foreach ($this->list as $item) {
if ($item instanceof ComplexDataInterface || $item instanceof ListInterface) {
if (!$item
->isEmpty()) {
return FALSE;
}
}
elseif ($item
->getValue() !== NULL) {
return FALSE;
}
}
return TRUE;
}
public function filter($callback) {
if (isset($this->list)) {
$removed = FALSE;
$this->list = array_filter($this->list, function ($item) use ($callback, &$removed) {
if (call_user_func($callback, $item)) {
return TRUE;
}
else {
$removed = TRUE;
}
});
if ($removed) {
$this
->rekey();
}
}
return $this;
}
public function onChange($delta) {
if (isset($this->parent)) {
$this->parent
->onChange($this->name);
}
}
public function __clone() {
foreach ($this->list as $delta => $item) {
$this->list[$delta] = clone $item;
$this->list[$delta]
->setContext($delta, $this);
}
}
}