ays, the value from * given array will replace the value in Dot object. * * @param array|self|string $key The Key. * @param array|self $value The Value. * @return $this */ public function mergeRecursiveDistinct( $key, $value = [] ) { if ( \is_array( $key ) ) { $this->items = $this->arrayMergeRecursiveDistinct( $this->items, $key ); } elseif ( is_string( $key ) ) { $items = (array) $this->get( $key ); $value = $this->arrayMergeRecursiveDistinct( $items, $this->getArrayItems( $value ) ); $this->set( $key, $value ); } elseif ( $key instanceof self ) { $this->items = $this->arrayMergeRecursiveDistinct( $this->items, $key->all() ); } return $this; } /** * Merges two arrays recursively. In contrast to array_merge_recursive, * duplicate keys are not converted to arrays but rather overwrite the * value in the first array with the duplicate value in the second array. * * @param array|array> $array1 Initial array to merge. * @param array|array> $array2 Array to recursively merge. * @return array|array> */ protected function arrayMergeRecursiveDistinct( $array1, $array2 ) { $merged = &$array1; foreach ( $array2 as $key => $value ) { if ( \is_array( $value ) && isset( $merged[ $key ] ) && \is_array( $merged[ $key ] ) ) { $merged[ $key ] = $this->arrayMergeRecursiveDistinct( $merged[ $key ], $value ); return; } $merged [ $key ] = $value; } return $merged; } /** * Return the value of a given key and * delete the key * * @param int|string|null $key The Key. * @param mixed $default The default value. * @return mixed */ public function pull( $key = null, $default = null ) { if ( \is_null( $key ) ) { $value = $this->all(); $this->clear(); return $value; } $value = $this->get( $key, $default ); $this->delete( $key ); return $value; } /** * Push a given value to the end of the array * in a given key * * @param mixed $key The Key. * @param mixed $value The Value. * @return $this */ public function push( $key, $value = null ) { if ( \is_null( $value ) ) { $this->items[] = $key; return $this; } $items = $this->get( $key ); if ( is_array( $items ) || \is_null( $items ) ) { $items[] = $value; $this->set( $key, $items ); } return $this; } /** * Replace all values or values within the given key * with an array or Dot object * * @param array|self|string $key The key. * @param array|self $value The Value. * @return $this */ public function replace( $key, $value = [] ) { if ( \is_array( $key ) ) { $this->items = array_replace( $this->items, $key ); } elseif ( \is_string( $key ) ) { $items = (array) $this->get( $key ); $value = \array_replace( $items, $this->getArrayItems( $value ) ); $this->set( $key, $value ); } elseif ( $key instanceof self ) { $this->items = \array_replace( $this->items, $key->all() ); } return $this; } /** * Set a given key / value pair or pairs * * @param array|int|string $keys The Keys. * @param mixed $value The Value. * @return $this */ public function set( $keys, $value = null ) { if ( \is_array( $keys ) ) { foreach ( $keys as $key => $value ) { $this->set( $key, $value ); } return $this; } $items = &$this->items; if ( \is_string( $keys ) ) { foreach ( \explode( $this->delimiter, $keys ) as $key ) { if ( ! isset( $items[ $key ] ) || ! is_array( $items[ $key ] ) ) { $items[ $key ] = []; } $items = &$items[ $key ]; } } $items = $value; return $this; } /** * Replace all items with a given array * * @param mixed $items The items. * @return $this */ public function setArray( $items ) { $this->items = $this->getArrayItems( $items ); return $this; } /** * Replace all items with a given array as a reference * * @param array $items The items. * @return $this */ public function setReference( &$items ) { $this->items = &$items; return $this; } /** * Return the value of a given key or all the values as JSON * * @param mixed $key The key. * @param int $options The options. * @return string|false */ public function toJson( $key = null, $options = 0 ) { if ( \is_string( $key ) ) { return \wp_json_encode( $this->get( $key ), $options ); } $options = \is_null( $key ) ? 0 : $key; return \wp_json_encode( $this->items, $options ); } /** * Output or return a parsable string representation of the * given array when exported by var_export() * * @param array $items The items. * @return object */ public static function __set_state( $items ) { return (object) $items; } /* * -------------------------------------------------------------- * ArrayAccess interface * -------------------------------------------------------------- */ /** * Check if a given key exists * * @param int|string $key The key. * @return bool */ #[\ReturnTypeWillChange] public function offsetExists( $key ) { return $this->has( $key ); } /** * Return the value of a given key * * @param int|string $key The key. * @return mixed */ #[\ReturnTypeWillChange] public function offsetGet( $key ) { return $this->get( $key ); } /** * Set a given value to the given key * * @param int|string|null $key The key. * @param mixed $value The value. */ #[\ReturnTypeWillChange] public function offsetSet( $key, $value ) { if ( \is_null( $key ) ) { $this->items[] = $value; return; } $this->set( $key, $value ); } /** * Delete the given key * * @param int|string $key The Key. * @return void */ #[\ReturnTypeWillChange] public function offsetUnset( $key ) { $this->delete( $key ); } /* * -------------------------------------------------------------- * Countable interface * -------------------------------------------------------------- */ /** * Return the number of items in a given key * * @param int|string|null $key The Key. * @return int */ #[\ReturnTypeWillChange] public function count( $key = null ) { return count( $this->get( $key ) ); } /* * -------------------------------------------------------------- * IteratorAggregate interface * -------------------------------------------------------------- */ /** * Get an iterator for the stored items * * @return \ArrayIterator */ #[\ReturnTypeWillChange] public function getIterator() { return new ArrayIterator( $this->items ); } /* * -------------------------------------------------------------- * JsonSerializable interface * -------------------------------------------------------------- */ /** * Return items for JSON serialization * * @return array */ #[\ReturnTypeWillChange] public function jsonSerialize() { return $this->items; } }