123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609 |
- <?php
- /**
- * Dot - PHP dot notation access to arrays
- *
- * @author Riku Särkinen <riku@adbar.io>
- * @link https://github.com/adbario/php-dot-notation
- * @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License)
- */
- namespace Adbar;
- use Countable;
- use ArrayAccess;
- use ArrayIterator;
- use JsonSerializable;
- use IteratorAggregate;
- /**
- * Dot
- *
- * This class provides a dot notation access and helper functions for
- * working with arrays of data. Inspired by Laravel Collection.
- */
- class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
- {
- /**
- * The stored items
- *
- * @var array
- */
- protected $items = [];
- /**
- * Create a new Dot instance
- *
- * @param mixed $items
- */
- public function __construct($items = [])
- {
- $this->items = $this->getArrayItems($items);
- }
- /**
- * Set a given key / value pair or pairs
- * if the key doesn't exist already
- *
- * @param array|int|string $keys
- * @param mixed $value
- */
- public function add($keys, $value = null)
- {
- if (is_array($keys)) {
- foreach ($keys as $key => $value) {
- $this->add($key, $value);
- }
- } elseif (is_null($this->get($keys))) {
- $this->set($keys, $value);
- }
- }
- /**
- * Return all the stored items
- *
- * @return array
- */
- public function all()
- {
- return $this->items;
- }
- /**
- * Delete the contents of a given key or keys
- *
- * @param array|int|string|null $keys
- */
- public function clear($keys = null)
- {
- if (is_null($keys)) {
- $this->items = [];
- return;
- }
- $keys = (array) $keys;
- foreach ($keys as $key) {
- $this->set($key, []);
- }
- }
- /**
- * Delete the given key or keys
- *
- * @param array|int|string $keys
- */
- public function delete($keys)
- {
- $keys = (array) $keys;
- foreach ($keys as $key) {
- if ($this->exists($this->items, $key)) {
- unset($this->items[$key]);
- continue;
- }
- $items = &$this->items;
- $segments = explode('.', $key);
- $lastSegment = array_pop($segments);
- foreach ($segments as $segment) {
- if (!isset($items[$segment]) || !is_array($items[$segment])) {
- continue 2;
- }
- $items = &$items[$segment];
- }
- unset($items[$lastSegment]);
- }
- }
- /**
- * Checks if the given key exists in the provided array.
- *
- * @param array $array Array to validate
- * @param int|string $key The key to look for
- *
- * @return bool
- */
- protected function exists($array, $key)
- {
- return array_key_exists($key, $array);
- }
- /**
- * Flatten an array with the given character as a key delimiter
- *
- * @param string $delimiter
- * @param array|null $items
- * @param string $prepend
- * @return array
- */
- public function flatten($delimiter = '.', $items = null, $prepend = '')
- {
- $flatten = [];
- if (is_null($items)) {
- $items = $this->items;
- }
- foreach ($items as $key => $value) {
- if (is_array($value) && !empty($value)) {
- $flatten = array_merge(
- $flatten,
- $this->flatten($delimiter, $value, $prepend.$key.$delimiter)
- );
- } else {
- $flatten[$prepend.$key] = $value;
- }
- }
- return $flatten;
- }
- /**
- * Return the value of a given key
- *
- * @param int|string|null $key
- * @param mixed $default
- * @return mixed
- */
- public function get($key = null, $default = null)
- {
- if (is_null($key)) {
- return $this->items;
- }
- if ($this->exists($this->items, $key)) {
- return $this->items[$key];
- }
- if (strpos($key, '.') === false) {
- return $default;
- }
- $items = $this->items;
- foreach (explode('.', $key) as $segment) {
- if (!is_array($items) || !$this->exists($items, $segment)) {
- return $default;
- }
- $items = &$items[$segment];
- }
- return $items;
- }
- /**
- * Return the given items as an array
- *
- * @param mixed $items
- * @return array
- */
- protected function getArrayItems($items)
- {
- if (is_array($items)) {
- return $items;
- } elseif ($items instanceof self) {
- return $items->all();
- }
- return (array) $items;
- }
- /**
- * Check if a given key or keys exists
- *
- * @param array|int|string $keys
- * @return bool
- */
- public function has($keys)
- {
- $keys = (array) $keys;
- if (!$this->items || $keys === []) {
- return false;
- }
- foreach ($keys as $key) {
- $items = $this->items;
- if ($this->exists($items, $key)) {
- continue;
- }
- foreach (explode('.', $key) as $segment) {
- if (!is_array($items) || !$this->exists($items, $segment)) {
- return false;
- }
- $items = $items[$segment];
- }
- }
- return true;
- }
- /**
- * Check if a given key or keys are empty
- *
- * @param array|int|string|null $keys
- * @return bool
- */
- public function isEmpty($keys = null)
- {
- if (is_null($keys)) {
- return empty($this->items);
- }
- $keys = (array) $keys;
- foreach ($keys as $key) {
- if (!empty($this->get($key))) {
- return false;
- }
- }
- return true;
- }
- /**
- * Merge a given array or a Dot object with the given key
- * or with the whole Dot object
- *
- * @param array|string|self $key
- * @param array|self $value
- */
- public function merge($key, $value = [])
- {
- if (is_array($key)) {
- $this->items = array_merge($this->items, $key);
- } elseif (is_string($key)) {
- $items = (array) $this->get($key);
- $value = array_merge($items, $this->getArrayItems($value));
- $this->set($key, $value);
- } elseif ($key instanceof self) {
- $this->items = array_merge($this->items, $key->all());
- }
- }
- /**
- * Recursively merge a given array or a Dot object with the given key
- * or with the whole Dot object.
- *
- * Duplicate keys are converted to arrays.
- *
- * @param array|string|self $key
- * @param array|self $value
- */
- public function mergeRecursive($key, $value = [])
- {
- if (is_array($key)) {
- $this->items = array_merge_recursive($this->items, $key);
- } elseif (is_string($key)) {
- $items = (array) $this->get($key);
- $value = array_merge_recursive($items, $this->getArrayItems($value));
- $this->set($key, $value);
- } elseif ($key instanceof self) {
- $this->items = array_merge_recursive($this->items, $key->all());
- }
- }
- /**
- * Recursively merge a given array or a Dot object with the given key
- * or with the whole Dot object.
- *
- * Instead of converting duplicate keys to arrays, the value from
- * given array will replace the value in Dot object.
- *
- * @param array|string|self $key
- * @param array|self $value
- */
- 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());
- }
- }
- /**
- * 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 $array1 Initial array to merge
- * @param array $array2 Array to recursively merge
- * @return array
- */
- protected function arrayMergeRecursiveDistinct(array $array1, array $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);
- } else {
- $merged[$key] = $value;
- }
- }
- return $merged;
- }
- /**
- * Return the value of a given key and
- * delete the key
- *
- * @param int|string|null $key
- * @param mixed $default
- * @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
- * @param mixed $value
- */
- public function push($key, $value = null)
- {
- if (is_null($value)) {
- $this->items[] = $key;
- return;
- }
- $items = $this->get($key);
- if (is_array($items) || is_null($items)) {
- $items[] = $value;
- $this->set($key, $items);
- }
- }
- /**
- * Replace all values or values within the given key
- * with an array or Dot object
- *
- * @param array|string|self $key
- * @param array|self $value
- */
- 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());
- }
- }
- /**
- * Set a given key / value pair or pairs
- *
- * @param array|int|string $keys
- * @param mixed $value
- */
- public function set($keys, $value = null)
- {
- if (is_array($keys)) {
- foreach ($keys as $key => $value) {
- $this->set($key, $value);
- }
- return;
- }
- $items = &$this->items;
- foreach (explode('.', $keys) as $key) {
- if (!isset($items[$key]) || !is_array($items[$key])) {
- $items[$key] = [];
- }
- $items = &$items[$key];
- }
- $items = $value;
- }
- /**
- * Replace all items with a given array
- *
- * @param mixed $items
- */
- public function setArray($items)
- {
- $this->items = $this->getArrayItems($items);
- }
- /**
- * Replace all items with a given array as a reference
- *
- * @param array $items
- */
- public function setReference(array &$items)
- {
- $this->items = &$items;
- }
- /**
- * Return the value of a given key or all the values as JSON
- *
- * @param mixed $key
- * @param int $options
- * @return string
- */
- 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);
- }
- /*
- * --------------------------------------------------------------
- * ArrayAccess interface
- * --------------------------------------------------------------
- */
- /**
- * Check if a given key exists
- *
- * @param int|string $key
- * @return bool
- */
- #[\ReturnTypeWillChange]
- public function offsetExists($key)
- {
- 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
- */
- #[\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
- */
- #[\ReturnTypeWillChange]
- public function offsetUnset($key)
- {
- $this->delete($key);
- }
- /*
- * --------------------------------------------------------------
- * Countable interface
- * --------------------------------------------------------------
- */
- /**
- * Return the number of items in a given key
- *
- * @param int|string|null $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;
- }
- }
|