3 namespace Drupal\bootstrap\Utility;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
7 use Drupal\Core\Render\AttachmentsInterface;
8 use Drupal\Core\Render\BubbleableMetadata;
11 * Custom ArrayObject implementation.
13 * The native ArrayObject is unnecessarily complicated.
17 class ArrayObject implements \IteratorAggregate, \ArrayAccess, \Serializable, \Countable, AttachmentsInterface, RefinableCacheableDependencyInterface {
27 * Array object constructor.
32 public function __construct(array $array = []) {
33 $this->array = $array;
37 * Returns whether the requested key exists.
45 public function __isset($key) {
46 return $this->offsetExists($key);
50 * Sets the value at the specified key to value.
57 public function __set($key, $value) {
58 $this->offsetSet($key, $value);
62 * Unsets the value at the specified key.
67 public function __unset($key) {
68 $this->offsetUnset($key);
72 * Returns the value at the specified key by reference.
80 public function &__get($key) {
81 $ret =& $this->offsetGet($key);
88 public function addAttachments(array $attachments) {
89 BubbleableMetadata::createFromRenderArray($this->array)->addAttachments($attachments)->applyTo($this->array);
96 public function addCacheContexts(array $cache_contexts) {
97 BubbleableMetadata::createFromRenderArray($this->array)->addCacheContexts($cache_contexts)->applyTo($this->array);
104 public function addCacheTags(array $cache_tags) {
105 BubbleableMetadata::createFromRenderArray($this->array)->addCacheTags($cache_tags)->applyTo($this->array);
112 public function addCacheableDependency($other_object) {
113 BubbleableMetadata::createFromRenderArray($this->array)->addCacheableDependency($other_object)->applyTo($this->array);
120 * @param mixed $value
123 public function append($value) {
124 $this->array[] = $value;
128 * Sort the entries by value.
130 public function asort() {
135 * Merges an object's cacheable metadata into the variables array.
137 * @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $object
138 * The object whose cacheability metadata to retrieve. If it implements
139 * CacheableDependencyInterface, its cacheability metadata will be used,
140 * otherwise, the passed in object must be assumed to be uncacheable, so
145 public function bubbleObject($object) {
146 BubbleableMetadata::createFromRenderArray($this->array)->merge(BubbleableMetadata::createFromObject($object))->applyTo($this->array);
151 * Merges a render array's cacheable metadata into the variables array.
153 * @param array $build
158 public function bubbleRenderArray(array $build) {
159 BubbleableMetadata::createFromRenderArray($this->array)->merge(BubbleableMetadata::createFromRenderArray($build))->applyTo($this->array);
164 * Get the number of public properties in the ArrayObject.
169 public function count() {
170 return count($this->array);
174 * Exchange the array for another one.
176 * @param array|ArrayObject $data
182 * @throws \InvalidArgumentException
183 * When the passed data is not an array or an instance of ArrayObject.
185 public function exchangeArray($data) {
186 if (!is_array($data) && is_object($data) && !($data instanceof ArrayObject)) {
187 throw new \InvalidArgumentException('Passed variable is not an array or an instance of \Drupal\bootstrap\Utility\ArrayObject.');
189 if (is_object($data) && $data instanceof ArrayObject) {
190 $data = $data->getArrayCopy();
193 $this->array = $data;
198 * Creates a copy of the ArrayObject.
201 * A copy of the array.
203 public function getArrayCopy() {
210 public function getAttachments() {
211 return BubbleableMetadata::createFromRenderArray($this->array)->getAttachments();
217 public function getCacheContexts() {
218 return BubbleableMetadata::createFromRenderArray($this->array)->getCacheContexts();
224 public function getCacheTags() {
225 return BubbleableMetadata::createFromRenderArray($this->array)->getCacheTags();
231 public function getCacheMaxAge() {
232 return BubbleableMetadata::createFromRenderArray($this->array)->getCacheMaxAge();
236 * Creates a new iterator from an ArrayObject instance.
238 * @return \ArrayIterator
241 public function getIterator() {
242 return new \ArrayIterator($this->array);
246 * Sort the entries by key.
248 public function ksort() {
253 * Merges multiple values into the array.
255 * @param array $values
256 * An associative key/value array.
257 * @param bool $recursive
258 * Flag determining whether or not to recursively merge key/value pairs.
260 public function merge(array $values, $recursive = TRUE) {
262 $this->array = NestedArray::mergeDeepArray([$this->array, $values], TRUE);
265 $this->array += $values;
272 public function mergeCacheMaxAge($max_age) {
273 BubbleableMetadata::createFromRenderArray($this->array)->mergeCacheMaxAge($max_age)->applyTo($this->array);
278 * Sort an array using a case insensitive "natural order" algorithm.
280 public function natcasesort() {
281 natcasesort($this->array);
285 * Sort entries using a "natural order" algorithm.
287 public function natsort() {
288 natsort($this->array);
292 * Returns whether the requested key exists.
300 public function offsetExists($key) {
301 return isset($this->array[$key]);
305 * Returns the value at the specified key.
309 * @param mixed $default
310 * The default value to set if $key does not exist.
315 public function &offsetGet($key, $default = NULL) {
316 if (!$this->offsetExists($key)) {
317 $this->array[$key] = $default;
319 $ret = &$this->array[$key];
324 * Sets the value at the specified key to value.
328 * @param mixed $value
331 public function offsetSet($key, $value) {
332 $this->array[$key] = $value;
336 * Unsets the value at the specified key.
341 public function offsetUnset($key) {
342 if ($this->offsetExists($key)) {
343 unset($this->array[$key]);
348 * Serialize an ArrayObject.
351 * The serialized value.
353 public function serialize() {
354 return serialize(get_object_vars($this));
360 public function setAttachments(array $attachments) {
361 BubbleableMetadata::createFromRenderArray($this->array)->setAttachments($attachments)->applyTo($this->array);
366 * Sort entries with a user-defined function and maintain key association.
368 * @param mixed $function
369 * A callable function.
371 public function uasort($function) {
372 if (is_callable($function)) {
373 uasort($this->array, $function);
378 * Sort the entries by keys using a user-defined comparison function.
380 * @param mixed $function
381 * A callable function.
383 public function uksort($function) {
384 if (is_callable($function)) {
385 uksort($this->array, $function);
390 * Unserialize an ArrayObject.
392 * @param string $data
393 * The serialized data.
395 public function unserialize($data) {
396 $data = unserialize($data);
397 $this->exchangeArray($data['array']);