PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Nette/ArrayTools.php

https://github.com/DocX/nette
PHP | 133 lines | 49 code | 23 blank | 61 comment | 7 complexity | c2c897c8e92853d8a9af74ce3b329477 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette
  17. */
  18. /*namespace Nette;*/
  19. /**
  20. * Array tools library.
  21. *
  22. * @author David Grudl
  23. * @copyright Copyright (c) 2004, 2009 David Grudl
  24. * @package Nette
  25. */
  26. final class ArrayTools
  27. {
  28. /**
  29. * Static class - cannot be instantiated.
  30. */
  31. final public function __construct()
  32. {
  33. throw new /*\*/LogicException("Cannot instantiate static class " . get_class($this));
  34. }
  35. /**
  36. * Returns array item or $default if item is not set.
  37. * Example: $val = ArrayTools::get($arr, 'i', 123);
  38. * @param mixed array
  39. * @param mixed key
  40. * @param mixed default value
  41. * @return mixed
  42. */
  43. public static function get(array $arr, $key, $default = NULL)
  44. {
  45. if (is_array($key)) {
  46. foreach ($key as $k) {
  47. if (is_array($arr) && array_key_exists($k, $arr)) {
  48. $arr = $arr[$k];
  49. } else {
  50. return $default;
  51. }
  52. }
  53. return $arr;
  54. } else {
  55. return array_key_exists($key, $arr) ? $arr[$key] : $default;
  56. }
  57. }
  58. /**
  59. * Recursively appends elements of remaining keys from the second array to the first.
  60. * @param array
  61. * @param array
  62. * @return array
  63. */
  64. public static function mergeTree($arr1, $arr2)
  65. {
  66. $res = $arr1 + $arr2;
  67. foreach (array_intersect_key($arr1, $arr2) as $k => $v) {
  68. if (is_array($v) && is_array($arr2[$k])) {
  69. $res[$k] = self::mergeTree($v, $arr2[$k]);
  70. }
  71. }
  72. return $res;
  73. }
  74. /**
  75. * Searches the array for a given key and returns the offset if successful.
  76. * @param array input array
  77. * @param mixed key
  78. * @return int offset if it is found, FALSE otherwise
  79. */
  80. public static function searchKey($arr, $key)
  81. {
  82. $foo = array($key => NULL);
  83. return array_search(key($foo), array_keys($arr), TRUE);
  84. }
  85. /**
  86. * Inserts new array before item specified by key.
  87. * @param array input array
  88. * @param mixed key
  89. * @param array inserted array
  90. * @return void
  91. */
  92. public static function insertBefore(array &$arr, $key, array $inserted)
  93. {
  94. $offset = self::searchKey($arr, $key);
  95. $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, NULL, TRUE);
  96. }
  97. /**
  98. * Inserts new array after item specified by key.
  99. * @param array input array
  100. * @param mixed key
  101. * @param array inserted array
  102. * @return void
  103. */
  104. public static function insertAfter(array &$arr, $key, array $inserted)
  105. {
  106. $offset = self::searchKey($arr, $key);
  107. $offset = $offset === FALSE ? NULL : $offset + 1;
  108. $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, NULL, TRUE);
  109. }
  110. }