PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Ip/Internal/Vendor/Zend/Stdlib/ArrayUtils.php

https://gitlab.com/x33n/ImpressPages
PHP | 275 lines | 121 code | 29 blank | 125 comment | 29 complexity | a253872a913a2662d12155dbd792d434 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Stdlib;
  10. use Traversable;
  11. /**
  12. * Utility class for testing and manipulation of PHP arrays.
  13. *
  14. * Declared abstract, as we have no need for instantiation.
  15. */
  16. abstract class ArrayUtils
  17. {
  18. /**
  19. * Test whether an array contains one or more string keys
  20. *
  21. * @param mixed $value
  22. * @param bool $allowEmpty Should an empty array() return true
  23. * @return bool
  24. */
  25. public static function hasStringKeys($value, $allowEmpty = false)
  26. {
  27. if (!is_array($value)) {
  28. return false;
  29. }
  30. if (!$value) {
  31. return $allowEmpty;
  32. }
  33. return count(array_filter(array_keys($value), 'is_string')) > 0;
  34. }
  35. /**
  36. * Test whether an array contains one or more integer keys
  37. *
  38. * @param mixed $value
  39. * @param bool $allowEmpty Should an empty array() return true
  40. * @return bool
  41. */
  42. public static function hasIntegerKeys($value, $allowEmpty = false)
  43. {
  44. if (!is_array($value)) {
  45. return false;
  46. }
  47. if (!$value) {
  48. return $allowEmpty;
  49. }
  50. return count(array_filter(array_keys($value), 'is_int')) > 0;
  51. }
  52. /**
  53. * Test whether an array contains one or more numeric keys.
  54. *
  55. * A numeric key can be one of the following:
  56. * - an integer 1,
  57. * - a string with a number '20'
  58. * - a string with negative number: '-1000'
  59. * - a float: 2.2120, -78.150999
  60. * - a string with float: '4000.99999', '-10.10'
  61. *
  62. * @param mixed $value
  63. * @param bool $allowEmpty Should an empty array() return true
  64. * @return bool
  65. */
  66. public static function hasNumericKeys($value, $allowEmpty = false)
  67. {
  68. if (!is_array($value)) {
  69. return false;
  70. }
  71. if (!$value) {
  72. return $allowEmpty;
  73. }
  74. return count(array_filter(array_keys($value), 'is_numeric')) > 0;
  75. }
  76. /**
  77. * Test whether an array is a list
  78. *
  79. * A list is a collection of values assigned to continuous integer keys
  80. * starting at 0 and ending at count() - 1.
  81. *
  82. * For example:
  83. * <code>
  84. * $list = array('a', 'b', 'c', 'd');
  85. * $list = array(
  86. * 0 => 'foo',
  87. * 1 => 'bar',
  88. * 2 => array('foo' => 'baz'),
  89. * );
  90. * </code>
  91. *
  92. * @param mixed $value
  93. * @param bool $allowEmpty Is an empty list a valid list?
  94. * @return bool
  95. */
  96. public static function isList($value, $allowEmpty = false)
  97. {
  98. if (!is_array($value)) {
  99. return false;
  100. }
  101. if (!$value) {
  102. return $allowEmpty;
  103. }
  104. return (array_values($value) === $value);
  105. }
  106. /**
  107. * Test whether an array is a hash table.
  108. *
  109. * An array is a hash table if:
  110. *
  111. * 1. Contains one or more non-integer keys, or
  112. * 2. Integer keys are non-continuous or misaligned (not starting with 0)
  113. *
  114. * For example:
  115. * <code>
  116. * $hash = array(
  117. * 'foo' => 15,
  118. * 'bar' => false,
  119. * );
  120. * $hash = array(
  121. * 1995 => 'Birth of PHP',
  122. * 2009 => 'PHP 5.3.0',
  123. * 2012 => 'PHP 5.4.0',
  124. * );
  125. * $hash = array(
  126. * 'formElement,
  127. * 'options' => array( 'debug' => true ),
  128. * );
  129. * </code>
  130. *
  131. * @param mixed $value
  132. * @param bool $allowEmpty Is an empty array() a valid hash table?
  133. * @return bool
  134. */
  135. public static function isHashTable($value, $allowEmpty = false)
  136. {
  137. if (!is_array($value)) {
  138. return false;
  139. }
  140. if (!$value) {
  141. return $allowEmpty;
  142. }
  143. return (array_values($value) !== $value);
  144. }
  145. /**
  146. * Checks if a value exists in an array.
  147. *
  148. * Due to "foo" == 0 === TRUE with in_array when strict = false, an option
  149. * has been added to prevent this. When $strict = 0/false, the most secure
  150. * non-strict check is implemented. if $strict = -1, the default in_array
  151. * non-strict behaviour is used.
  152. *
  153. * @param mixed $needle
  154. * @param array $haystack
  155. * @param int|bool $strict
  156. * @return bool
  157. */
  158. public static function inArray($needle, array $haystack, $strict = false)
  159. {
  160. if (!$strict) {
  161. if (is_int($needle) || is_float($needle)) {
  162. $needle = (string) $needle;
  163. }
  164. if (is_string($needle)) {
  165. foreach ($haystack as &$h) {
  166. if (is_int($h) || is_float($h)) {
  167. $h = (string) $h;
  168. }
  169. }
  170. }
  171. }
  172. return in_array($needle, $haystack, $strict);
  173. }
  174. /**
  175. * Convert an iterator to an array.
  176. *
  177. * Converts an iterator to an array. The $recursive flag, on by default,
  178. * hints whether or not you want to do so recursively.
  179. *
  180. * @param array|Traversable $iterator The array or Traversable object to convert
  181. * @param bool $recursive Recursively check all nested structures
  182. * @throws Exception\InvalidArgumentException if $iterator is not an array or a Traversable object
  183. * @return array
  184. */
  185. public static function iteratorToArray($iterator, $recursive = true)
  186. {
  187. if (!is_array($iterator) && !$iterator instanceof Traversable) {
  188. throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable object');
  189. }
  190. if (!$recursive) {
  191. if (is_array($iterator)) {
  192. return $iterator;
  193. }
  194. return iterator_to_array($iterator);
  195. }
  196. if (method_exists($iterator, 'toArray')) {
  197. return $iterator->toArray();
  198. }
  199. $array = array();
  200. foreach ($iterator as $key => $value) {
  201. if (is_scalar($value)) {
  202. $array[$key] = $value;
  203. continue;
  204. }
  205. if ($value instanceof Traversable) {
  206. $array[$key] = static::iteratorToArray($value, $recursive);
  207. continue;
  208. }
  209. if (is_array($value)) {
  210. $array[$key] = static::iteratorToArray($value, $recursive);
  211. continue;
  212. }
  213. $array[$key] = $value;
  214. }
  215. return $array;
  216. }
  217. /**
  218. * Merge two arrays together.
  219. *
  220. * If an integer key exists in both arrays, the value from the second array
  221. * will be appended the the first array. If both values are arrays, they
  222. * are merged together, else the value of the second array overwrites the
  223. * one of the first array.
  224. *
  225. * @param array $a
  226. * @param array $b
  227. * @return array
  228. */
  229. public static function merge(array $a, array $b)
  230. {
  231. foreach ($b as $key => $value) {
  232. if (array_key_exists($key, $a)) {
  233. if (is_int($key)) {
  234. $a[] = $value;
  235. } elseif (is_array($value) && is_array($a[$key])) {
  236. $a[$key] = static::merge($a[$key], $value);
  237. } else {
  238. $a[$key] = $value;
  239. }
  240. } else {
  241. $a[$key] = $value;
  242. }
  243. }
  244. return $a;
  245. }
  246. }