Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • none

Classes

Exceptions

  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  * @package Nette\Utils
  7:  */
  8: 
  9: 
 10: 
 11: /**
 12:  * Array tools library.
 13:  *
 14:  * @author     David Grudl
 15:  * @package Nette\Utils
 16:  */
 17: class Arrays
 18: {
 19: 
 20:     /**
 21:      * Static class - cannot be instantiated.
 22:      */
 23:     final public function __construct()
 24:     {
 25:         throw new StaticClassException;
 26:     }
 27: 
 28: 
 29:     /**
 30:      * Returns item from array or $default if item is not set.
 31:      * @param  array
 32:      * @param  string|int|array one or more keys
 33:      * @param  mixed
 34:      * @return mixed
 35:      * @throws InvalidArgumentException if item does not exist and default value is not provided
 36:      */
 37:     public static function get(array $arr, $key, $default = NULL)
 38:     {
 39:         foreach (is_array($key) ? $key : array($key) as $k) {
 40:             if (is_array($arr) && array_key_exists($k, $arr)) {
 41:                 $arr = $arr[$k];
 42:             } else {
 43:                 if (func_num_args() < 3) {
 44:                     throw new InvalidArgumentException("Missing item '$k'.");
 45:                 }
 46:                 return $default;
 47:             }
 48:         }
 49:         return $arr;
 50:     }
 51: 
 52: 
 53:     /**
 54:      * Returns reference to array item or $default if item is not set.
 55:      * @param  array
 56:      * @param  string|int|array one or more keys
 57:      * @return mixed
 58:      * @throws InvalidArgumentException if traversed item is not an array
 59:      */
 60:     public static function & getRef(& $arr, $key)
 61:     {
 62:         foreach (is_array($key) ? $key : array($key) as $k) {
 63:             if (is_array($arr) || $arr === NULL) {
 64:                 $arr = & $arr[$k];
 65:             } else {
 66:                 throw new InvalidArgumentException('Traversed item is not an array.');
 67:             }
 68:         }
 69:         return $arr;
 70:     }
 71: 
 72: 
 73:     /**
 74:      * Recursively appends elements of remaining keys from the second array to the first.
 75:      * @return array
 76:      */
 77:     public static function mergeTree($arr1, $arr2)
 78:     {
 79:         $res = $arr1 + $arr2;
 80:         foreach (array_intersect_key($arr1, $arr2) as $k => $v) {
 81:             if (is_array($v) && is_array($arr2[$k])) {
 82:                 $res[$k] = self::mergeTree($v, $arr2[$k]);
 83:             }
 84:         }
 85:         return $res;
 86:     }
 87: 
 88: 
 89:     /**
 90:      * Searches the array for a given key and returns the offset if successful.
 91:      * @return int|FALSE offset if it is found, FALSE otherwise
 92:      */
 93:     public static function searchKey($arr, $key)
 94:     {
 95:         $foo = array($key => NULL);
 96:         return array_search(key($foo), array_keys($arr), TRUE);
 97:     }
 98: 
 99: 
100:     /**
101:      * Inserts new array before item specified by key.
102:      * @return void
103:      */
104:     public static function insertBefore(array & $arr, $key, array $inserted)
105:     {
106:         $offset = self::searchKey($arr, $key);
107:         $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
108:     }
109: 
110: 
111:     /**
112:      * Inserts new array after item specified by key.
113:      * @return void
114:      */
115:     public static function insertAfter(array & $arr, $key, array $inserted)
116:     {
117:         $offset = self::searchKey($arr, $key);
118:         $offset = $offset === FALSE ? count($arr) : $offset + 1;
119:         $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
120:     }
121: 
122: 
123:     /**
124:      * Renames key in array.
125:      * @return void
126:      */
127:     public static function renameKey(array & $arr, $oldKey, $newKey)
128:     {
129:         $offset = self::searchKey($arr, $oldKey);
130:         if ($offset !== FALSE) {
131:             $keys = array_keys($arr);
132:             $keys[$offset] = $newKey;
133:             $arr = array_combine($keys, $arr);
134:         }
135:     }
136: 
137: 
138:     /**
139:      * Returns array entries that match the pattern.
140:      * @return array
141:      */
142:     public static function grep(array $arr, $pattern, $flags = 0)
143:     {
144:         set_error_handler(create_function('$severity, $message', 'extract($GLOBALS[0]['.array_push($GLOBALS[0], array('pattern'=>$pattern)).'-1], EXTR_REFS); // preg_last_error does not return compile errors
145:             restore_error_handler();
146:             throw new RegexpException("$message in pattern: $pattern");
147:         '));
148:         $res = preg_grep($pattern, $arr, $flags);
149:         restore_error_handler();
150:         if (preg_last_error()) { // run-time error
151:             throw new RegexpException(NULL, preg_last_error(), $pattern);
152:         }
153:         return $res;
154:     }
155: 
156: 
157:     /**
158:      * Returns flattened array.
159:      * @return array
160:      */
161:     public static function flatten(array $arr)
162:     {
163:         $res = array();
164:         array_walk_recursive($arr, create_function('$a', 'extract($GLOBALS[0]['.array_push($GLOBALS[0], array('res'=>& $res)).'-1], EXTR_REFS); $res[] = $a; '));
165:         return $res;
166:     }
167: 
168: }
169: 
Nette Framework 2.0.18 (for PHP 5.2, un-prefixed) API documentation generated by ApiGen 2.8.0