} /** * Set a given key / value pair or pairs * * @param array|int|string $keys * @param mixed $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 * @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 * @return $this */ public function setReference(array &$items) { $this->items = &$items; return $this; } /** * Return the value of a given key or all the values as JSON * * @param mixed $key * @param int $options * @return string|false */ public function toJson($key = null, $options = 0) { if (is_string($key)) { return json_encode($this->get($key), $options); } $options = $key === null ? 0 : $key; return json_encode($this->items, $options); } /** * Output or return a parsable string representation of the * given array when exported by var_export() * * @param array $items * @return object */ public static function __set_state(array $items): object { return (object) $items; } /* * -------------------------------------------------------------- * ArrayAccess interface * -------------------------------------------------------------- */ /** * Check if a given key exists * * @param int|string $key * @return bool */ public function offsetExists($key): bool { return $this->has($key); } /** * Return the value of a given key * * @param int|string $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 * @param mixed $value */ public function offsetSet($key, $value): void { if ($key === null) { $this->items[] = $value; return; } $this->set($key, $value); } /** * Delete the given key * * @param int|string $key * @return void */ public function offsetUnset($key): void { $this->delete($key); } /* * -------------------------------------------------------------- * Countable interface * -------------------------------------------------------------- */ /** * Return the number of items in a given key * * @param int|string|null $key * @return int */ public function count($key = null): int { return count($this->get($key)); } /* * -------------------------------------------------------------- * IteratorAggregate interface * -------------------------------------------------------------- */ /** * Get an iterator for the stored items * * @return \ArrayIterator */ public function getIterator(): Traversable { return new ArrayIterator($this->items); } /* * -------------------------------------------------------------- * JsonSerializable interface * -------------------------------------------------------------- */ /** * Return items for JSON serialization * * @return array */ public function jsonSerialize(): array { return $this->items; } }