Dot.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. <?php
  2. /**
  3. * Dot - PHP dot notation access to arrays
  4. *
  5. * @author Riku Särkinen <riku@adbar.io>
  6. * @link https://github.com/adbario/php-dot-notation
  7. * @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License)
  8. */
  9. namespace Adbar;
  10. use Countable;
  11. use ArrayAccess;
  12. use ArrayIterator;
  13. use JsonSerializable;
  14. use IteratorAggregate;
  15. /**
  16. * Dot
  17. *
  18. * This class provides a dot notation access and helper functions for
  19. * working with arrays of data. Inspired by Laravel Collection.
  20. */
  21. class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
  22. {
  23. /**
  24. * The stored items
  25. *
  26. * @var array
  27. */
  28. protected $items = [];
  29. /**
  30. * The delimiter (alternative to a '.') to be used.
  31. *
  32. * @var string
  33. */
  34. protected $delimiter = '.';
  35. /**
  36. * Create a new Dot instance
  37. *
  38. * @param mixed $items
  39. * @param string $delimiter
  40. */
  41. public function __construct($items = [], $delimiter = '.')
  42. {
  43. $this->items = $this->getArrayItems($items);
  44. $this->delimiter = strlen($delimiter) ? $delimiter : '.';
  45. }
  46. /**
  47. * Set a given key / value pair or pairs
  48. * if the key doesn't exist already
  49. *
  50. * @param array|int|string $keys
  51. * @param mixed $value
  52. */
  53. public function add($keys, $value = null)
  54. {
  55. if (is_array($keys)) {
  56. foreach ($keys as $key => $value) {
  57. $this->add($key, $value);
  58. }
  59. } elseif (is_null($this->get($keys))) {
  60. $this->set($keys, $value);
  61. }
  62. }
  63. /**
  64. * Return all the stored items
  65. *
  66. * @return array
  67. */
  68. public function all()
  69. {
  70. return $this->items;
  71. }
  72. /**
  73. * Delete the contents of a given key or keys
  74. *
  75. * @param array|int|string|null $keys
  76. */
  77. public function clear($keys = null)
  78. {
  79. if (is_null($keys)) {
  80. $this->items = [];
  81. return;
  82. }
  83. $keys = (array) $keys;
  84. foreach ($keys as $key) {
  85. $this->set($key, []);
  86. }
  87. }
  88. /**
  89. * Delete the given key or keys
  90. *
  91. * @param array|int|string $keys
  92. */
  93. public function delete($keys)
  94. {
  95. $keys = (array) $keys;
  96. foreach ($keys as $key) {
  97. if ($this->exists($this->items, $key)) {
  98. unset($this->items[$key]);
  99. continue;
  100. }
  101. $items = &$this->items;
  102. $segments = explode($this->delimiter, $key);
  103. $lastSegment = array_pop($segments);
  104. foreach ($segments as $segment) {
  105. if (!isset($items[$segment]) || !is_array($items[$segment])) {
  106. continue 2;
  107. }
  108. $items = &$items[$segment];
  109. }
  110. unset($items[$lastSegment]);
  111. }
  112. }
  113. /**
  114. * Checks if the given key exists in the provided array.
  115. *
  116. * @param array $array Array to validate
  117. * @param int|string $key The key to look for
  118. *
  119. * @return bool
  120. */
  121. protected function exists($array, $key)
  122. {
  123. return array_key_exists($key, $array);
  124. }
  125. /**
  126. * Flatten an array with the given character as a key delimiter
  127. *
  128. * @param string $delimiter
  129. * @param array|null $items
  130. * @param string $prepend
  131. * @return array
  132. */
  133. public function flatten($delimiter = '.', $items = null, $prepend = '')
  134. {
  135. $flatten = [];
  136. if (is_null($items)) {
  137. $items = $this->items;
  138. }
  139. if (!func_num_args()) {
  140. $delimiter = $this->delimiter;
  141. }
  142. foreach ($items as $key => $value) {
  143. if (is_array($value) && !empty($value)) {
  144. $flatten = array_merge(
  145. $flatten,
  146. $this->flatten($delimiter, $value, $prepend.$key.$delimiter)
  147. );
  148. } else {
  149. $flatten[$prepend.$key] = $value;
  150. }
  151. }
  152. return $flatten;
  153. }
  154. /**
  155. * Return the value of a given key
  156. *
  157. * @param int|string|null $key
  158. * @param mixed $default
  159. * @return mixed
  160. */
  161. public function get($key = null, $default = null)
  162. {
  163. if (is_null($key)) {
  164. return $this->items;
  165. }
  166. if ($this->exists($this->items, $key)) {
  167. return $this->items[$key];
  168. }
  169. if (strpos($key, $this->delimiter) === false) {
  170. return $default;
  171. }
  172. $items = $this->items;
  173. foreach (explode($this->delimiter, $key) as $segment) {
  174. if (!is_array($items) || !$this->exists($items, $segment)) {
  175. return $default;
  176. }
  177. $items = &$items[$segment];
  178. }
  179. return $items;
  180. }
  181. /**
  182. * Return the given items as an array
  183. *
  184. * @param mixed $items
  185. * @return array
  186. */
  187. protected function getArrayItems($items)
  188. {
  189. if (is_array($items)) {
  190. return $items;
  191. } elseif ($items instanceof self) {
  192. return $items->all();
  193. }
  194. return (array) $items;
  195. }
  196. /**
  197. * Check if a given key or keys exists
  198. *
  199. * @param array|int|string $keys
  200. * @return bool
  201. */
  202. public function has($keys)
  203. {
  204. $keys = (array) $keys;
  205. if (!$this->items || $keys === []) {
  206. return false;
  207. }
  208. foreach ($keys as $key) {
  209. $items = $this->items;
  210. if ($this->exists($items, $key)) {
  211. continue;
  212. }
  213. foreach (explode($this->delimiter, $key) as $segment) {
  214. if (!is_array($items) || !$this->exists($items, $segment)) {
  215. return false;
  216. }
  217. $items = $items[$segment];
  218. }
  219. }
  220. return true;
  221. }
  222. /**
  223. * Check if a given key or keys are empty
  224. *
  225. * @param array|int|string|null $keys
  226. * @return bool
  227. */
  228. public function isEmpty($keys = null)
  229. {
  230. if (is_null($keys)) {
  231. return empty($this->items);
  232. }
  233. $keys = (array) $keys;
  234. foreach ($keys as $key) {
  235. if (!empty($this->get($key))) {
  236. return false;
  237. }
  238. }
  239. return true;
  240. }
  241. /**
  242. * Merge a given array or a Dot object with the given key
  243. * or with the whole Dot object
  244. *
  245. * @param array|string|self $key
  246. * @param array|self $value
  247. */
  248. public function merge($key, $value = [])
  249. {
  250. if (is_array($key)) {
  251. $this->items = array_merge($this->items, $key);
  252. } elseif (is_string($key)) {
  253. $items = (array) $this->get($key);
  254. $value = array_merge($items, $this->getArrayItems($value));
  255. $this->set($key, $value);
  256. } elseif ($key instanceof self) {
  257. $this->items = array_merge($this->items, $key->all());
  258. }
  259. }
  260. /**
  261. * Recursively merge a given array or a Dot object with the given key
  262. * or with the whole Dot object.
  263. *
  264. * Duplicate keys are converted to arrays.
  265. *
  266. * @param array|string|self $key
  267. * @param array|self $value
  268. */
  269. public function mergeRecursive($key, $value = [])
  270. {
  271. if (is_array($key)) {
  272. $this->items = array_merge_recursive($this->items, $key);
  273. } elseif (is_string($key)) {
  274. $items = (array) $this->get($key);
  275. $value = array_merge_recursive($items, $this->getArrayItems($value));
  276. $this->set($key, $value);
  277. } elseif ($key instanceof self) {
  278. $this->items = array_merge_recursive($this->items, $key->all());
  279. }
  280. }
  281. /**
  282. * Recursively merge a given array or a Dot object with the given key
  283. * or with the whole Dot object.
  284. *
  285. * Instead of converting duplicate keys to arrays, the value from
  286. * given array will replace the value in Dot object.
  287. *
  288. * @param array|string|self $key
  289. * @param array|self $value
  290. */
  291. public function mergeRecursiveDistinct($key, $value = [])
  292. {
  293. if (is_array($key)) {
  294. $this->items = $this->arrayMergeRecursiveDistinct($this->items, $key);
  295. } elseif (is_string($key)) {
  296. $items = (array) $this->get($key);
  297. $value = $this->arrayMergeRecursiveDistinct($items, $this->getArrayItems($value));
  298. $this->set($key, $value);
  299. } elseif ($key instanceof self) {
  300. $this->items = $this->arrayMergeRecursiveDistinct($this->items, $key->all());
  301. }
  302. }
  303. /**
  304. * Merges two arrays recursively. In contrast to array_merge_recursive,
  305. * duplicate keys are not converted to arrays but rather overwrite the
  306. * value in the first array with the duplicate value in the second array.
  307. *
  308. * @param array $array1 Initial array to merge
  309. * @param array $array2 Array to recursively merge
  310. * @return array
  311. */
  312. protected function arrayMergeRecursiveDistinct(array $array1, array $array2)
  313. {
  314. $merged = &$array1;
  315. foreach ($array2 as $key => $value) {
  316. if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
  317. $merged[$key] = $this->arrayMergeRecursiveDistinct($merged[$key], $value);
  318. } else {
  319. $merged[$key] = $value;
  320. }
  321. }
  322. return $merged;
  323. }
  324. /**
  325. * Return the value of a given key and
  326. * delete the key
  327. *
  328. * @param int|string|null $key
  329. * @param mixed $default
  330. * @return mixed
  331. */
  332. public function pull($key = null, $default = null)
  333. {
  334. if (is_null($key)) {
  335. $value = $this->all();
  336. $this->clear();
  337. return $value;
  338. }
  339. $value = $this->get($key, $default);
  340. $this->delete($key);
  341. return $value;
  342. }
  343. /**
  344. * Push a given value to the end of the array
  345. * in a given key
  346. *
  347. * @param mixed $key
  348. * @param mixed $value
  349. */
  350. public function push($key, $value = null)
  351. {
  352. if (is_null($value)) {
  353. $this->items[] = $key;
  354. return;
  355. }
  356. $items = $this->get($key);
  357. if (is_array($items) || is_null($items)) {
  358. $items[] = $value;
  359. $this->set($key, $items);
  360. }
  361. }
  362. /**
  363. * Replace all values or values within the given key
  364. * with an array or Dot object
  365. *
  366. * @param array|string|self $key
  367. * @param array|self $value
  368. */
  369. public function replace($key, $value = [])
  370. {
  371. if (is_array($key)) {
  372. $this->items = array_replace($this->items, $key);
  373. } elseif (is_string($key)) {
  374. $items = (array) $this->get($key);
  375. $value = array_replace($items, $this->getArrayItems($value));
  376. $this->set($key, $value);
  377. } elseif ($key instanceof self) {
  378. $this->items = array_replace($this->items, $key->all());
  379. }
  380. }
  381. /**
  382. * Set a given key / value pair or pairs
  383. *
  384. * @param array|int|string $keys
  385. * @param mixed $value
  386. */
  387. public function set($keys, $value = null)
  388. {
  389. if (is_array($keys)) {
  390. foreach ($keys as $key => $value) {
  391. $this->set($key, $value);
  392. }
  393. return;
  394. }
  395. $items = &$this->items;
  396. foreach (explode($this->delimiter, $keys) as $key) {
  397. if (!isset($items[$key]) || !is_array($items[$key])) {
  398. $items[$key] = [];
  399. }
  400. $items = &$items[$key];
  401. }
  402. $items = $value;
  403. }
  404. /**
  405. * Replace all items with a given array
  406. *
  407. * @param mixed $items
  408. */
  409. public function setArray($items)
  410. {
  411. $this->items = $this->getArrayItems($items);
  412. }
  413. /**
  414. * Replace all items with a given array as a reference
  415. *
  416. * @param array $items
  417. */
  418. public function setReference(array &$items)
  419. {
  420. $this->items = &$items;
  421. }
  422. /**
  423. * Return the value of a given key or all the values as JSON
  424. *
  425. * @param mixed $key
  426. * @param int $options
  427. * @return string
  428. */
  429. public function toJson($key = null, $options = 0)
  430. {
  431. if (is_string($key)) {
  432. return json_encode($this->get($key), $options);
  433. }
  434. $options = $key === null ? 0 : $key;
  435. return json_encode($this->items, $options);
  436. }
  437. /*
  438. * --------------------------------------------------------------
  439. * ArrayAccess interface
  440. * --------------------------------------------------------------
  441. */
  442. /**
  443. * Check if a given key exists
  444. *
  445. * @param int|string $key
  446. * @return bool
  447. */
  448. #[\ReturnTypeWillChange]
  449. public function offsetExists($key)
  450. {
  451. return $this->has($key);
  452. }
  453. /**
  454. * Return the value of a given key
  455. *
  456. * @param int|string $key
  457. * @return mixed
  458. */
  459. #[\ReturnTypeWillChange]
  460. public function offsetGet($key)
  461. {
  462. return $this->get($key);
  463. }
  464. /**
  465. * Set a given value to the given key
  466. *
  467. * @param int|string|null $key
  468. * @param mixed $value
  469. */
  470. #[\ReturnTypeWillChange]
  471. public function offsetSet($key, $value)
  472. {
  473. if (is_null($key)) {
  474. $this->items[] = $value;
  475. return;
  476. }
  477. $this->set($key, $value);
  478. }
  479. /**
  480. * Delete the given key
  481. *
  482. * @param int|string $key
  483. */
  484. #[\ReturnTypeWillChange]
  485. public function offsetUnset($key)
  486. {
  487. $this->delete($key);
  488. }
  489. /*
  490. * --------------------------------------------------------------
  491. * Countable interface
  492. * --------------------------------------------------------------
  493. */
  494. /**
  495. * Return the number of items in a given key
  496. *
  497. * @param int|string|null $key
  498. * @return int
  499. */
  500. #[\ReturnTypeWillChange]
  501. public function count($key = null)
  502. {
  503. return count($this->get($key));
  504. }
  505. /*
  506. * --------------------------------------------------------------
  507. * IteratorAggregate interface
  508. * --------------------------------------------------------------
  509. */
  510. /**
  511. * Get an iterator for the stored items
  512. *
  513. * @return \ArrayIterator
  514. */
  515. #[\ReturnTypeWillChange]
  516. public function getIterator()
  517. {
  518. return new ArrayIterator($this->items);
  519. }
  520. /*
  521. * --------------------------------------------------------------
  522. * JsonSerializable interface
  523. * --------------------------------------------------------------
  524. */
  525. /**
  526. * Return items for JSON serialization
  527. *
  528. * @return array
  529. */
  530. #[\ReturnTypeWillChange]
  531. public function jsonSerialize()
  532. {
  533. return $this->items;
  534. }
  535. }