Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
ArrayUtils.php
001 <?php
002 /**
003 * Zend Framework (http://framework.zend.com/)
004 *
005 * @link http://github.com/zendframework/zf2 for the canonical source repository
006 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
007 * @license http://framework.zend.com/license/new-bsd New BSD License
008 */
009
010 namespace Zend\Stdlib;
011
012 use Traversable;
013 use Zend\Stdlib\ArrayUtils\MergeRemoveKey;
014 use Zend\Stdlib\ArrayUtils\MergeReplaceKeyInterface;
015
016 /**
017 * Utility class for testing and manipulation of PHP arrays.
018 *
019 * Declared abstract, as we have no need for instantiation.
020 */
021 abstract class ArrayUtils
022 {
023 /**
024 * Compatibility Flag for ArrayUtils::filter
025 */
026 const ARRAY_FILTER_USE_BOTH = 1;
027
028 /**
029 * Compatibility Flag for ArrayUtils::filter
030 */
031 const ARRAY_FILTER_USE_KEY = 2;
032
033 /**
034 * Test whether an array contains one or more string keys
035 *
036 * @param mixed $value
037 * @param bool $allowEmpty Should an empty array() return true
038 * @return bool
039 */
040 public static function hasStringKeys($value, $allowEmpty = false)
041 {
042 if (!is_array($value)) {
043 return false;
044 }
045
046 if (!$value) {
047 return $allowEmpty;
048 }
049
050 return count(array_filter(array_keys($value), 'is_string')) > 0;
051 }
052
053 /**
054 * Test whether an array contains one or more integer keys
055 *
056 * @param mixed $value
057 * @param bool $allowEmpty Should an empty array() return true
058 * @return bool
059 */
060 public static function hasIntegerKeys($value, $allowEmpty = false)
061 {
062 if (!is_array($value)) {
063 return false;
064 }
065
066 if (!$value) {
067 return $allowEmpty;
068 }
069
070 return count(array_filter(array_keys($value), 'is_int')) > 0;
071 }
072
073 /**
074 * Test whether an array contains one or more numeric keys.
075 *
076 * A numeric key can be one of the following:
077 * - an integer 1,
078 * - a string with a number '20'
079 * - a string with negative number: '-1000'
080 * - a float: 2.2120, -78.150999
081 * - a string with float: '4000.99999', '-10.10'
082 *
083 * @param mixed $value
084 * @param bool $allowEmpty Should an empty array() return true
085 * @return bool
086 */
087 public static function hasNumericKeys($value, $allowEmpty = false)
088 {
089 if (!is_array($value)) {
090 return false;
091 }
092
093 if (!$value) {
094 return $allowEmpty;
095 }
096
097 return count(array_filter(array_keys($value), 'is_numeric')) > 0;
098 }
099
100 /**
101 * Test whether an array is a list
102 *
103 * A list is a collection of values assigned to continuous integer keys
104 * starting at 0 and ending at count() - 1.
105 *
106 * For example:
107 * <code>
108 * $list = array('a', 'b', 'c', 'd');
109 * $list = array(
110 * 0 => 'foo',
111 * 1 => 'bar',
112 * 2 => array('foo' => 'baz'),
113 * );
114 * </code>
115 *
116 * @param mixed $value
117 * @param bool $allowEmpty Is an empty list a valid list?
118 * @return bool
119 */
120 public static function isList($value, $allowEmpty = false)
121 {
122 if (!is_array($value)) {
123 return false;
124 }
125
126 if (!$value) {
127 return $allowEmpty;
128 }
129
130 return (array_values($value) === $value);
131 }
132
133 /**
134 * Test whether an array is a hash table.
135 *
136 * An array is a hash table if:
137 *
138 * 1. Contains one or more non-integer keys, or
139 * 2. Integer keys are non-continuous or misaligned (not starting with 0)
140 *
141 * For example:
142 * <code>
143 * $hash = array(
144 * 'foo' => 15,
145 * 'bar' => false,
146 * );
147 * $hash = array(
148 * 1995 => 'Birth of PHP',
149 * 2009 => 'PHP 5.3.0',
150 * 2012 => 'PHP 5.4.0',
151 * );
152 * $hash = array(
153 * 'formElement,
154 * 'options' => array( 'debug' => true ),
155 * );
156 * </code>
157 *
158 * @param mixed $value
159 * @param bool $allowEmpty Is an empty array() a valid hash table?
160 * @return bool
161 */
162 public static function isHashTable($value, $allowEmpty = false)
163 {
164 if (!is_array($value)) {
165 return false;
166 }
167
168 if (!$value) {
169 return $allowEmpty;
170 }
171
172 return (array_values($value) !== $value);
173 }
174
175 /**
176 * Checks if a value exists in an array.
177 *
178 * Due to "foo" == 0 === TRUE with in_array when strict = false, an option
179 * has been added to prevent this. When $strict = 0/false, the most secure
180 * non-strict check is implemented. if $strict = -1, the default in_array
181 * non-strict behaviour is used.
182 *
183 * @param mixed $needle
184 * @param array $haystack
185 * @param int|bool $strict
186 * @return bool
187 */
188 public static function inArray($needle, array $haystack, $strict = false)
189 {
190 if (!$strict) {
191 if (is_int($needle) || is_float($needle)) {
192 $needle = (string) $needle;
193 }
194 if (is_string($needle)) {
195 foreach ($haystack as &$h) {
196 if (is_int($h) || is_float($h)) {
197 $h = (string) $h;
198 }
199 }
200 }
201 }
202 return in_array($needle, $haystack, $strict);
203 }
204
205 /**
206 * Convert an iterator to an array.
207 *
208 * Converts an iterator to an array. The $recursive flag, on by default,
209 * hints whether or not you want to do so recursively.
210 *
211 * @param array|Traversable $iterator The array or Traversable object to convert
212 * @param bool $recursive Recursively check all nested structures
213 * @throws Exception\InvalidArgumentException if $iterator is not an array or a Traversable object
214 * @return array
215 */
216 public static function iteratorToArray($iterator, $recursive = true)
217 {
218 if (!is_array($iterator) && !$iterator instanceof Traversable) {
219 throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable object');
220 }
221
222 if (!$recursive) {
223 if (is_array($iterator)) {
224 return $iterator;
225 }
226
227 return iterator_to_array($iterator);
228 }
229
230 if (method_exists($iterator, 'toArray')) {
231 return $iterator->toArray();
232 }
233
234 $array = array();
235 foreach ($iterator as $key => $value) {
236 if (is_scalar($value)) {
237 $array[$key] = $value;
238 continue;
239 }
240
241 if ($value instanceof Traversable) {
242 $array[$key] = static::iteratorToArray($value, $recursive);
243 continue;
244 }
245
246 if (is_array($value)) {
247 $array[$key] = static::iteratorToArray($value, $recursive);
248 continue;
249 }
250
251 $array[$key] = $value;
252 }
253
254 return $array;
255 }
256
257 /**
258 * Merge two arrays together.
259 *
260 * If an integer key exists in both arrays and preserveNumericKeys is false, the value
261 * from the second array will be appended to the first array. If both values are arrays, they
262 * are merged together, else the value of the second array overwrites the one of the first array.
263 *
264 * @param array $a
265 * @param array $b
266 * @param bool $preserveNumericKeys
267 * @return array
268 */
269 public static function merge(array $a, array $b, $preserveNumericKeys = false)
270 {
271 foreach ($b as $key => $value) {
272 if ($value instanceof MergeReplaceKeyInterface) {
273 $a[$key] = $value->getData();
274 } elseif (isset($a[$key]) || array_key_exists($key, $a)) {
275 if ($value instanceof MergeRemoveKey) {
276 unset($a[$key]);
277 } elseif (!$preserveNumericKeys && is_int($key)) {
278 $a[] = $value;
279 } elseif (is_array($value) && is_array($a[$key])) {
280 $a[$key] = static::merge($a[$key], $value, $preserveNumericKeys);
281 } else {
282 $a[$key] = $value;
283 }
284 } else {
285 if (!$value instanceof MergeRemoveKey) {
286 $a[$key] = $value;
287 }
288 }
289 }
290
291 return $a;
292 }
293
294 /**
295 * Compatibility Method for array_filter on <5.6 systems
296 *
297 * @param array $data
298 * @param callable $callback
299 * @param null|int $flag
300 * @return array
301 */
302 public static function filter(array $data, $callback, $flag = null)
303 {
304 if (! is_callable($callback)) {
305 throw new Exception\InvalidArgumentException(sprintf(
306 'Second parameter of %s must be callable',
307 __METHOD__
308 ));
309 }
310
311 if (version_compare(PHP_VERSION, '5.6.0') >= 0) {
312 return array_filter($data, $callback, $flag);
313 }
314
315 $output = array();
316 foreach ($data as $key => $value) {
317 $params = array($value);
318
319 if ($flag === static::ARRAY_FILTER_USE_BOTH) {
320 $params[] = $key;
321 }
322
323 if ($flag === static::ARRAY_FILTER_USE_KEY) {
324 $params = array($key);
325 }
326
327 $response = call_user_func_array($callback, $params);
328 if ($response) {
329 $output[$key] = $value;
330 }
331 }
332
333 return $output;
334 }
335 }
336