PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/laravel/arr.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 166 lines | 63 code | 23 blank | 80 comment | 10 complexity | cd93b77e62a995f5e44e280e1f447450 MD5 | raw file
  1. <?php namespace Laravel; use Closure;
  2. class Arr {
  3. /**
  4. * Get an item from an array.
  5. *
  6. * "Dot" notation may be used to dig deep into the array.
  7. *
  8. * <code>
  9. * // Get the $array['user']['name'] value from the array
  10. * $name = Arr::get($array, 'user.name');
  11. *
  12. * // Return a default from if the specified item doesn't exist
  13. * $name = Arr::get($array, 'user.name', 'Taylor');
  14. * </code>
  15. *
  16. * @param array $array
  17. * @param string $key
  18. * @param mixed $default
  19. * @return mixed
  20. */
  21. public static function get($array, $key, $default = null)
  22. {
  23. if (is_null($key)) return $array;
  24. foreach (explode('.', $key) as $segment)
  25. {
  26. if ( ! is_array($array) or ! array_key_exists($segment, $array))
  27. {
  28. return ($default instanceof Closure) ? call_user_func($default) : $default;
  29. }
  30. $array = $array[$segment];
  31. }
  32. return $array;
  33. }
  34. /**
  35. * Set an array item to a given value.
  36. *
  37. * The same "dot" syntax used by the "get" method may be used here.
  38. *
  39. * If no key is given to the method, the entire array will be replaced.
  40. *
  41. * <code>
  42. * // Set the $array['user']['name'] value on the array
  43. * Arr::set($array, 'user.name', 'Taylor');
  44. * </code>
  45. *
  46. * @param array $array
  47. * @param string $key
  48. * @param mixed $value
  49. * @return void
  50. */
  51. public static function set(&$array, $key, $value)
  52. {
  53. if (is_null($key)) return $array = $value;
  54. $keys = explode('.', $key);
  55. while (count($keys) > 1)
  56. {
  57. $key = array_shift($keys);
  58. if ( ! isset($array[$key]) or ! is_array($array[$key]))
  59. {
  60. $array[$key] = array();
  61. }
  62. $array =& $array[$key];
  63. }
  64. $array[array_shift($keys)] = $value;
  65. }
  66. /**
  67. * Remove an array item from a given array.
  68. *
  69. * The same "dot" syntax used by the "get" method may be used here.
  70. *
  71. * <code>
  72. * // Remove the $array['user']['name'] item from the array
  73. * Arr::forget($array, 'user.name');
  74. * </code>
  75. *
  76. * @param array $array
  77. * @param string $key
  78. * @return void
  79. */
  80. public static function forget(&$array, $key)
  81. {
  82. if (is_null($key)) return;
  83. $keys = explode('.', $key);
  84. while (count($keys) > 1)
  85. {
  86. $key = array_shift($keys);
  87. if ( ! isset($array[$key]) or ! is_array($array[$key]))
  88. {
  89. return;
  90. }
  91. $array =& $array[$key];
  92. }
  93. unset($array[array_shift($keys)]);
  94. }
  95. /**
  96. * Return the first element in an array which passes a given truth test.
  97. *
  98. * <code>
  99. * // Return the first array element that equals "Taylor"
  100. * $value = Arr::first($array, function($k, $v) {return $v === 'Taylor';});
  101. *
  102. * // Return a default value if no matching element is found
  103. * $value = Arr::first($array, function($k, $v) {return $v === 'Taylor'}, 'Default');
  104. * </code>
  105. *
  106. * @param array $array
  107. * @param Closure $callback
  108. * @param mixed $default
  109. * @return mixed
  110. */
  111. public static function first($array, $callback, $default = null)
  112. {
  113. foreach ($array as $key => $value)
  114. {
  115. if (call_user_func($callback, $key, $value)) return $value;
  116. }
  117. return ($default instanceof Closure) ? call_user_func($default) : $default;
  118. }
  119. /**
  120. * Remove all array values that are contained within a given array of values.
  121. *
  122. * <code>
  123. * // Remove all array values that are empty strings
  124. * $array = Arr::without($array, '');
  125. *
  126. * // Remove all array values that are "One", "Two", or "Three"
  127. * $array = Arr::without($array, array('One', 'Two', 'Three'));
  128. * </code>
  129. *
  130. * @param array $array
  131. * @param array $without
  132. * @return array
  133. */
  134. public static function without($array, $without = array())
  135. {
  136. $without = (array) $without;
  137. foreach ((array) $array as $key => $value)
  138. {
  139. if (in_array($value, $without)) unset($array[$key]);
  140. }
  141. return $array;
  142. }
  143. }