PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libraries/anahita/helper/array.php

https://github.com/bhar1red/anahita
PHP | 290 lines | 142 code | 28 blank | 120 comment | 22 complexity | 1a7a5e518887b072c5668594de1f7f1d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * LICENSE: ##LICENSE##
  4. *
  5. * @category Anahita
  6. * @package Anahita_Helper
  7. * @author Arash Sanieyan <ash@anahitapolis.com>
  8. * @author Rastin Mehr <rastin@anahitapolis.com>
  9. * @copyright 2008 - 2010 rmdStudio Inc./Peerglobe Technology Inc
  10. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  11. * @version SVN: $Id$
  12. * @link http://www.anahitapolis.com
  13. */
  14. define('PHP_INT_MIN', ~PHP_INT_MAX);
  15. /**
  16. * Array Helper
  17. *
  18. * @category Anahita
  19. * @package Anahita_Helper
  20. * @author Arash Sanieyan <ash@anahitapolis.com>
  21. * @author Rastin Mehr <rastin@anahitapolis.com>
  22. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  23. * @link http://www.anahitapolis.com
  24. */
  25. class AnHelperArray extends KHelperArray
  26. {
  27. /**
  28. * Index flags
  29. */
  30. const LAST_INDEX = PHP_INT_MAX;
  31. const FIRST_INDEX = PHP_INT_MIN;
  32. /**
  33. * Index an object (or an array) using one of it's attribute
  34. *
  35. * @param array $items An array of object or associative array
  36. * @param string $key Attribute by which to index the array
  37. *
  38. * @return array
  39. */
  40. public static function indexBy($items, $key)
  41. {
  42. $array = array();
  43. foreach($items as $item)
  44. {
  45. $array[self::getValue($item, $key)] = $item;
  46. }
  47. return $array;
  48. }
  49. /**
  50. * Collects $key from an array of items
  51. *
  52. * @param array $items An array of object or associative array
  53. * @param string|array $key The key to collect the value for
  54. *
  55. * @return array
  56. */
  57. static public function collect($items, $key)
  58. {
  59. $array = array();
  60. foreach($items as $v)
  61. {
  62. if ( is_array($key) ) {
  63. foreach($key as $index => $k) {
  64. if ( !isset($array[$k]) ) {
  65. $array[$k] = array();
  66. }
  67. $array[$k][] = self::getValue($v, $k);
  68. }
  69. }
  70. else $array[] = self::getValue($v, $key);
  71. }
  72. return $array;
  73. }
  74. /**
  75. * Groups an array of items by their common $key
  76. *
  77. * @param array $items An array of object or associative array
  78. * @param string $key Attribute by which to index the array
  79. *
  80. * @return array
  81. */
  82. public static function groupBy($items, $key)
  83. {
  84. $array = array();
  85. foreach($items as $item)
  86. {
  87. $value = self::getValue($item, $key);
  88. if ( !isset($array[$value]) ) {
  89. $array[$value] = array();
  90. }
  91. $array[$value][] = $item;
  92. }
  93. return $array;
  94. }
  95. /**
  96. * Return a unique array of $array. This method also handles object as value
  97. *
  98. * @param array $array An Array
  99. *
  100. * @return array
  101. */
  102. static public function unique($array)
  103. {
  104. $unique = array();
  105. foreach($array as $item) {
  106. if (!in_array($item, $unique, true)) {
  107. $unique[] = $item;
  108. }
  109. }
  110. return $unique;
  111. }
  112. /**
  113. * Insert into an array. If no offset is given then the values
  114. * are inserted at the end of the list. Returns the new array with
  115. * value inserted into
  116. *
  117. * @param array $array The orignal array
  118. * @param array $values An array of values to be inserted
  119. *
  120. * @return array
  121. */
  122. static public function insert($array, $values, $index = null)
  123. {
  124. $values = (array)KConfig::unbox($values);
  125. $array = (array)KConfig::unbox($array);
  126. if ( $index === null ) {
  127. foreach($values as $value) {
  128. array_push($array, $value);
  129. }
  130. } else {
  131. array_splice($array, $index, 0, $values);
  132. }
  133. return $array;
  134. }
  135. /**
  136. * Unset a list of values from an array. This method both unset any key that exists
  137. * in the $values array as well as any values that exists in the $values array. This method
  138. * modifies the $array object
  139. *
  140. * @param array $array An Array values to unset
  141. *
  142. * @return array
  143. */
  144. static public function unsetValues($array, $values)
  145. {
  146. settype($values, 'array');
  147. foreach($array as $index => $item)
  148. {
  149. foreach($values as $value)
  150. {
  151. if ( !is_numeric($index) ) {
  152. $item = $index;
  153. }
  154. if ( $value == $item ) {
  155. unset($array[$index]);
  156. }
  157. }
  158. }
  159. return $array;
  160. }
  161. /**
  162. * Flattens a multi-dimensial array and return all the values as one single array
  163. *
  164. * @param array $array The array to be flattened
  165. *
  166. * @return array
  167. */
  168. static public function getValues($array)
  169. {
  170. settype($array,'array');
  171. $values = array();
  172. foreach($array as $key => $value)
  173. {
  174. if ( is_array($value) ) {
  175. $values = array_merge($values, self::getValues($value));
  176. } else
  177. $values[] = $value;
  178. }
  179. return $values;
  180. }
  181. /**
  182. * Return the simple scalar array of the object
  183. *
  184. * @param mixed $object An object to be converted to an array
  185. *
  186. * @return array
  187. */
  188. public static function toArray($object)
  189. {
  190. $object = KConfig::unbox($object);
  191. if ( is($object, 'KObjectArray') || is($object, 'KObjectSet') )
  192. return $object->toArray();
  193. return (array) $object;
  194. }
  195. /**
  196. * Get the value of an item (array|object) using a $key. The $key can be a string path
  197. *
  198. * @param object $item The object whose attribute value is being returend
  199. * @param string $key The attribute name
  200. *
  201. * @return mixed
  202. */
  203. public static function getValue($item, $key)
  204. {
  205. $parts = explode('.', $key);
  206. $value = $item;
  207. foreach($parts as $part)
  208. {
  209. if ( $value ) {
  210. if ( is($value, 'KObject') )
  211. $value = $value->get($part);
  212. else
  213. $value = is_array($value) ? $value[$part] : $value->$part;
  214. }
  215. }
  216. return $value;
  217. }
  218. /**
  219. * Return an interator for an object
  220. *
  221. * @param mixed $object An Iteratable or NonInterable object
  222. *
  223. * @return Iteratorable
  224. */
  225. public static function getIterator($object)
  226. {
  227. if ( !self::isIterable($object) )
  228. $object = array($object);
  229. return $object;
  230. }
  231. /**
  232. * Get the value at an index. The index can be an integer or 'first' or 'last'. If the
  233. * index doesn't exists it returns null
  234. *
  235. * @param array $array
  236. * @param mixed $index
  237. *
  238. * @return null or value
  239. */
  240. public function getValueAtIndex($array, $index)
  241. {
  242. $value = null;
  243. if ( abs((int)$index) == self::LAST_INDEX ) {
  244. $index == self::LAST_INDEX ? end($array) : reset($array);
  245. $value = current($array);
  246. } else if ( isset($array[$index]) ) {
  247. $value = $array[$index];
  248. }
  249. return $value;
  250. }
  251. /**
  252. * Return true if the array is some of kind of iterative array.
  253. *
  254. * @param array $array An array of object or associative array
  255. *
  256. * @return boolean
  257. */
  258. public static function isIterable($array)
  259. {
  260. return is_array($array) || $array instanceof Iterator;
  261. }
  262. }