/vendor/anahkiasen/underscore-php/src/Underscore/Methods/ArraysMethods.php

https://bitbucket.org/larryg/powerhut · PHP · 450 lines · 205 code · 76 blank · 169 comment · 24 complexity · 045d4ef62789dcaef6f5df4748ed110e MD5 · raw file

  1. <?php
  2. namespace Underscore\Methods;
  3. use Closure;
  4. /**
  5. * Methods to manage arrays
  6. */
  7. class ArraysMethods extends CollectionMethods
  8. {
  9. ////////////////////////////////////////////////////////////////////
  10. ///////////////////////////// GENERATE /////////////////////////////
  11. ////////////////////////////////////////////////////////////////////
  12. /**
  13. * Generate an array from a range
  14. *
  15. * @param integer $_base The base number
  16. * @param integer $stop The stopping point
  17. * @param integer $step How many to increment of
  18. *
  19. * @return array
  20. */
  21. public static function range($_base, $stop = null, $step = 1)
  22. {
  23. // Dynamic arguments
  24. if (!is_null($stop)) {
  25. $start = $_base;
  26. } else {
  27. $start = 1;
  28. $stop = $_base;
  29. }
  30. return range($start, $stop, $step);
  31. }
  32. /**
  33. * Fill an array with $times times some $data
  34. *
  35. * @param mixed $data
  36. * @param integer $times
  37. *
  38. * @return array
  39. */
  40. public static function repeat($data, $times)
  41. {
  42. $times = abs($times);
  43. if ($times == 0) return array();
  44. return array_fill(0, $times, $data);
  45. }
  46. ////////////////////////////////////////////////////////////////////
  47. ///////////////////////////// ANALYZE //////////////////////////////
  48. ////////////////////////////////////////////////////////////////////
  49. /**
  50. * Search for the index of a value in an array
  51. */
  52. public static function search($array, $value)
  53. {
  54. return array_search($value, $array);
  55. }
  56. /**
  57. * Check if all items in an array match a truth test
  58. */
  59. public static function matches($array, Closure $closure)
  60. {
  61. // Reduce the array to only booleans
  62. $array = (array) ArraysMethods::each($array, $closure);
  63. // Check the results
  64. if (sizeof($array) === 0) return true;
  65. $array = array_search(false, $array, false);
  66. return is_bool($array);
  67. }
  68. /**
  69. * Check if any item in an array matches a truth test
  70. */
  71. public static function matchesAny($array, Closure $closure)
  72. {
  73. // Reduce the array to only booleans
  74. $array = (array) ArraysMethods::each($array, $closure);
  75. // Check the results
  76. if (sizeof($array) === 0) return true;
  77. $array = array_search(true, $array, false);
  78. return is_int($array);
  79. }
  80. /**
  81. * Check if an item is in an array
  82. */
  83. public static function contains($array, $value)
  84. {
  85. return in_array($value, $array);
  86. }
  87. /**
  88. * Returns the average value of an array
  89. *
  90. * @param array $array The source array
  91. * @param integer $decimals The number of decimals to return
  92. * @return integer The average value
  93. */
  94. public static function average($array, $decimals = 0)
  95. {
  96. return round((array_sum($array) / sizeof($array)), $decimals);
  97. }
  98. /**
  99. * Get the size of an array
  100. */
  101. public static function size($array)
  102. {
  103. return sizeof($array);
  104. }
  105. /**
  106. * Get the max value from an array
  107. */
  108. public static function max($array, $closure = null)
  109. {
  110. // If we have a closure, apply it to the array
  111. if ($closure) $array = ArraysMethods::each($array, $closure);
  112. // Sort from max to min
  113. arsort($array);
  114. return ArraysMethods::first($array);
  115. }
  116. /**
  117. * Get the min value from an array
  118. */
  119. public static function min($array, $closure = null)
  120. {
  121. // If we have a closure, apply it to the array
  122. if ($closure) $array = ArraysMethods::each($array, $closure);
  123. // Sort from max to min
  124. asort($array);
  125. return ArraysMethods::first($array);
  126. }
  127. ////////////////////////////////////////////////////////////////////
  128. //////////////////////////// FETCH FROM ////////////////////////////
  129. ////////////////////////////////////////////////////////////////////
  130. /**
  131. * Find the first item in an array that passes the truth test
  132. */
  133. public static function find($array, Closure $closure)
  134. {
  135. foreach ($array as $key => $value) {
  136. if ($closure($value, $key)) return $value;
  137. }
  138. return $array;
  139. }
  140. /**
  141. * Clean all falsy values from an array
  142. */
  143. public static function clean($array)
  144. {
  145. return ArraysMethods::filter($array, function($value) {
  146. return (bool) $value;
  147. });
  148. }
  149. /**
  150. * Get a random string from an array
  151. */
  152. public static function random($array, $take = null)
  153. {
  154. if (!$take) return $array[array_rand($array)];
  155. shuffle($array);
  156. return ArraysMethods::first($array, $take);
  157. }
  158. /**
  159. * Return an array without all instances of certain values
  160. */
  161. public static function without()
  162. {
  163. $arguments = func_get_args();
  164. $array = array_shift($arguments);
  165. return ArraysMethods::filter($array, function($value) use ($arguments) {
  166. return !in_array($value, $arguments);
  167. });
  168. }
  169. ////////////////////////////////////////////////////////////////////
  170. ///////////////////////////// SLICERS //////////////////////////////
  171. ////////////////////////////////////////////////////////////////////
  172. /**
  173. * Get the first value from an array
  174. */
  175. public static function first($array, $take = null)
  176. {
  177. if (!$take) return array_shift($array);
  178. return array_splice($array, 0, $take, true);
  179. }
  180. /**
  181. * Get the last value from an array
  182. */
  183. public static function last($array, $take = null)
  184. {
  185. if (!$take) return array_pop($array);
  186. return ArraysMethods::rest($array, -$take);
  187. }
  188. /**
  189. * Get everything but the last $to items
  190. */
  191. public static function initial($array, $to = 1)
  192. {
  193. $slice = sizeof($array) - $to;
  194. return ArraysMethods::first($array, $slice);
  195. }
  196. /**
  197. * Get the last elements from index $from
  198. */
  199. public static function rest($array, $from = 1)
  200. {
  201. return array_splice($array, $from);
  202. }
  203. ////////////////////////////////////////////////////////////////////
  204. ///////////////////////////// ACT UPON /////////////////////////////
  205. ////////////////////////////////////////////////////////////////////
  206. /**
  207. * Iterate over an array and execute a callback for each loop
  208. */
  209. public static function at($array, Closure $closure)
  210. {
  211. foreach ($array as $key => $value) {
  212. $closure($value, $key);
  213. }
  214. return $array;
  215. }
  216. ////////////////////////////////////////////////////////////////////
  217. ////////////////////////////// ALTER ///////////////////////////////
  218. ////////////////////////////////////////////////////////////////////
  219. /**
  220. * Replace a value in an array
  221. *
  222. * @param array $array The array
  223. * @param string $replace The string to replace
  224. * @param string $with What to replace it with
  225. *
  226. * @return array
  227. */
  228. public static function replaceValue($array, $replace, $with)
  229. {
  230. return ArraysMethods::each($array, function($value) use ($replace, $with) {
  231. return str_replace($replace, $with, $value);
  232. });
  233. }
  234. /**
  235. * Replace the keys in an array with another set
  236. *
  237. * @param array $array The array
  238. * @param array $keys An array of keys matching the array's size
  239. *
  240. * @return array
  241. */
  242. public static function replaceKeys($array, $keys)
  243. {
  244. $values = array_values($array);
  245. return array_combine($keys, $values);
  246. }
  247. /**
  248. * Iterate over an array and modify the array's value
  249. */
  250. public static function each($array, Closure $closure)
  251. {
  252. foreach ($array as $key => $value) {
  253. $array[$key] = $closure($value, $key);
  254. }
  255. return $array;
  256. }
  257. /**
  258. * Shuffle an array
  259. */
  260. public static function shuffle($array)
  261. {
  262. shuffle($array);
  263. return $array;
  264. }
  265. /**
  266. * Sort an array by key
  267. */
  268. public static function sortKeys($array, $direction = 'ASC')
  269. {
  270. $direction = (strtolower($direction) == 'desc') ? SORT_DESC : SORT_ASC;
  271. if ($direction == SORT_ASC) ksort($array);
  272. else krsort($array);
  273. return $array;
  274. }
  275. /**
  276. * Implodes an array
  277. *
  278. * @param array $array The array
  279. * @param string $with What to implode it with
  280. *
  281. * @return String
  282. */
  283. public static function implode($array, $with = '')
  284. {
  285. return implode($with, $array);
  286. }
  287. /**
  288. * Find all items in an array that pass the truth test
  289. */
  290. public static function filter($array, $closure = null)
  291. {
  292. if (!$closure) return ArraysMethods::clean($array);
  293. return array_filter($array, $closure);
  294. }
  295. /**
  296. * Flattens an array to dot notation
  297. *
  298. * @param array $array An array
  299. * @param string $separator The characater to flatten with
  300. * @param string $parent The parent passed to the child (private)
  301. * @return array Flattened array to one level
  302. */
  303. public static function flatten($array, $separator = '.', $parent = null)
  304. {
  305. if(!is_array($array)) return $array;
  306. $_flattened = array();
  307. // Rewrite keys
  308. foreach ($array as $key => $value) {
  309. if($parent) $key = $parent.$separator.$key;
  310. $_flattened[$key] = ArraysMethods::flatten($value, $separator, $key);
  311. }
  312. // Flatten
  313. $flattened = array();
  314. foreach ($_flattened as $key => $value) {
  315. if(is_array($value)) $flattened = array_merge($flattened, $value);
  316. else $flattened[$key] = $value;
  317. }
  318. return $flattened;
  319. }
  320. /**
  321. * Invoke a function on all of an array's values
  322. */
  323. public static function invoke($array, $callable, $arguments = array())
  324. {
  325. // If one argument given for each iteration, create an array for it
  326. if (!is_array($arguments)) $arguments = ArraysMethods::repeat($arguments, sizeof($array));
  327. // If the callable has arguments, pass them
  328. if ($arguments) return array_map($callable, $array, $arguments);
  329. return array_map($callable, $array);
  330. }
  331. /**
  332. * Return all items that fail the truth test
  333. */
  334. public static function reject($array, Closure $closure)
  335. {
  336. $filtered = array();
  337. foreach ($array as $key => $value) {
  338. if (!$closure($value, $key)) $filtered[$key] = $value;
  339. }
  340. return $filtered;
  341. }
  342. /**
  343. * Remove the first value from an array
  344. */
  345. public static function removeFirst($array)
  346. {
  347. array_shift($array);
  348. return $array;
  349. }
  350. /**
  351. * Remove the last value from an array
  352. */
  353. public static function removeLast($array)
  354. {
  355. array_pop($array);
  356. return $array;
  357. }
  358. /**
  359. * Prepend a value to an array
  360. */
  361. public static function prepend($array, $value)
  362. {
  363. array_unshift($array, $value);
  364. return $array;
  365. }
  366. /**
  367. * Append a value to an array
  368. */
  369. public static function append($array, $value)
  370. {
  371. array_push($array, $value);
  372. return $array;
  373. }
  374. }