Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 595fcd1

Browse files
committed
Merge branch 'master' of https://github.com/zendframework/zf2 into zucchi/router-tweaks

6 files changed

Lines changed: 638 additions & 263 deletions

File tree

src/ArrayUtils.php

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

src/IsAssocArray.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/IteratorToArray.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)