4 * Contains \Drupal\bootstrap\Utility\ArrayObject.
7 namespace Drupal\bootstrap\Utility;
9 use Drupal\Component\Utility\NestedArray;
10 use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
11 use Drupal\Core\Render\AttachmentsInterface;
12 use Drupal\Core\Render\BubbleableMetadata;
15 * Custom ArrayObject implementation.
17 * The native ArrayObject is unnecessarily complicated.
21 class ArrayObject implements \IteratorAggregate, \ArrayAccess, \Serializable, \Countable, AttachmentsInterface, RefinableCacheableDependencyInterface {
31 * Array object constructor.
36 public function __construct(array $array = []) {
37 $this->array = $array;
41 * Returns whether the requested key exists.
49 public function __isset($key) {
50 return $this->offsetExists($key);
54 * Sets the value at the specified key to value.
61 public function __set($key, $value) {
62 $this->offsetSet($key, $value);
66 * Unsets the value at the specified key.
71 public function __unset($key) {
72 $this->offsetUnset($key);
76 * Returns the value at the specified key by reference.
84 public function &__get($key) {
85 $ret =& $this->offsetGet($key);
92 public function addAttachments(array $attachments) {
93 BubbleableMetadata::createFromRenderArray($this->array)->addAttachments($attachments)->applyTo($this->array);
100 public function addCacheContexts(array $cache_contexts) {
101 BubbleableMetadata::createFromRenderArray($this->array)->addCacheContexts($cache_contexts)->applyTo($this->array);
108 public function addCacheTags(array $cache_tags) {
109 BubbleableMetadata::createFromRenderArray($this->array)->addCacheTags($cache_tags)->applyTo($this->array);
116 public function addCacheableDependency($other_object) {
117 BubbleableMetadata::createFromRenderArray($this->array)->addCacheableDependency($other_object)->applyTo($this->array);
124 * @param mixed $value
127 public function append($value) {
128 $this->array[] = $value;
132 * Sort the entries by value.
134 public function asort() {
139 * Merges an object's cacheable metadata into the variables array.
141 * @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $object
142 * The object whose cacheability metadata to retrieve. If it implements
143 * CacheableDependencyInterface, its cacheability metadata will be used,
144 * otherwise, the passed in object must be assumed to be uncacheable, so
149 public function bubbleObject($object) {
150 BubbleableMetadata::createFromRenderArray($this->array)->merge(BubbleableMetadata::createFromObject($object))->applyTo($this->array);
155 * Merges a render array's cacheable metadata into the variables array.
157 * @param array $build
162 public function bubbleRenderArray(array $build) {
163 BubbleableMetadata::createFromRenderArray($this->array)->merge(BubbleableMetadata::createFromRenderArray($build))->applyTo($this->array);
168 * Get the number of public properties in the ArrayObject.
173 public function count() {
174 return count($this->array);
178 * Exchange the array for another one.
180 * @param array|ArrayObject $data
186 * @throws \InvalidArgumentException
187 * When the passed data is not an array or an instance of ArrayObject.
189 public function exchangeArray($data) {
190 if (!is_array($data) && is_object($data) && !($data instanceof ArrayObject)) {
191 throw new \InvalidArgumentException('Passed variable is not an array or an instance of \Drupal\bootstrap\Utility\ArrayObject.');
193 if (is_object($data) && $data instanceof ArrayObject) {
194 $data = $data->getArrayCopy();
197 $this->array = $data;
202 * Creates a copy of the ArrayObject.
205 * A copy of the array.
207 public function getArrayCopy() {
214 public function getAttachments() {
215 return BubbleableMetadata::createFromRenderArray($this->array)->getAttachments();
221 public function getCacheContexts() {
222 return BubbleableMetadata::createFromRenderArray($this->array)->getCacheContexts();
228 public function getCacheTags() {
229 return BubbleableMetadata::createFromRenderArray($this->array)->getCacheTags();
235 public function getCacheMaxAge() {
236 return BubbleableMetadata::createFromRenderArray($this->array)->getCacheMaxAge();
240 * Creates a new iterator from an ArrayObject instance.
242 * @return \ArrayIterator
245 public function getIterator() {
246 return new \ArrayIterator($this->array);
250 * Sort the entries by key.
252 public function ksort() {
257 * Merges multiple values into the array.
259 * @param array $values
260 * An associative key/value array.
261 * @param bool $recursive
262 * Flag determining whether or not to recursively merge key/value pairs.
264 public function merge(array $values, $recursive = TRUE) {
266 $this->array = NestedArray::mergeDeepArray([$this->array, $values], TRUE);
269 $this->array += $values;
276 public function mergeCacheMaxAge($max_age) {
277 BubbleableMetadata::createFromRenderArray($this->array)->mergeCacheMaxAge($max_age)->applyTo($this->array);
282 * Sort an array using a case insensitive "natural order" algorithm.
284 public function natcasesort() {
285 natcasesort($this->array);
289 * Sort entries using a "natural order" algorithm.
291 public function natsort() {
292 natsort($this->array);
296 * Returns whether the requested key exists.
304 public function offsetExists($key) {
305 return isset($this->array[$key]);
309 * Returns the value at the specified key.
313 * @param mixed $default
314 * The default value to set if $key does not exist.
319 public function &offsetGet($key, $default = NULL) {
320 if (!$this->offsetExists($key)) {
321 $this->array[$key] = $default;
323 $ret = &$this->array[$key];
328 * Sets the value at the specified key to value.
332 * @param mixed $value
335 public function offsetSet($key, $value) {
336 $this->array[$key] = $value;
340 * Unsets the value at the specified key.
345 public function offsetUnset($key) {
346 if ($this->offsetExists($key)) {
347 unset($this->array[$key]);
352 * Serialize an ArrayObject.
355 * The serialized value.
357 public function serialize() {
358 return serialize(get_object_vars($this));
364 public function setAttachments(array $attachments) {
365 BubbleableMetadata::createFromRenderArray($this->array)->setAttachments($attachments)->applyTo($this->array);
370 * Sort entries with a user-defined function and maintain key association.
372 * @param mixed $function
373 * A callable function.
375 public function uasort($function) {
376 if (is_callable($function)) {
377 uasort($this->array, $function);
382 * Sort the entries by keys using a user-defined comparison function.
384 * @param mixed $function
385 * A callable function.
387 public function uksort($function) {
388 if (is_callable($function)) {
389 uksort($this->array, $function);
394 * Unserialize an ArrayObject.
396 * @param string $data
397 * The serialized data.
399 public function unserialize($data) {
400 $data = unserialize($data);
401 $this->exchangeArray($data['array']);