Namespaces

  • 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
  • PHP

Classes

  • Arrays
  • Finder
  • Html
  • Json
  • LimitedScope
  • MimeTypeDetector
  • Neon
  • NeonEntity
  • Paginator
  • Strings
  • Tokenizer
  • Validators

Exceptions

  • AssertionException
  • JsonException
  • NeonException
  • RegexpException
  • TokenizerException
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Utils;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * Array tools library.
 20:  *
 21:  * @author     David Grudl
 22:  */
 23: final class Arrays
 24: {
 25: 
 26:     /**
 27:      * Static class - cannot be instantiated.
 28:      */
 29:     final public function __construct()
 30:     {
 31:         throw new Nette\StaticClassException;
 32:     }
 33: 
 34: 
 35: 
 36:     /**
 37:      * Returns array item or $default if item is not set.
 38:      * Example: $val = Arrays::get($arr, 'i', 123);
 39:      * @param  mixed  array
 40:      * @param  mixed  key
 41:      * @param  mixed  default value
 42:      * @return mixed
 43:      */
 44:     public static function get(array $arr, $key, $default = NULL)
 45:     {
 46:         foreach (is_array($key) ? $key : array($key) as $k) {
 47:             if (is_array($arr) && array_key_exists($k, $arr)) {
 48:                 $arr = $arr[$k];
 49:             } else {
 50:                 if (func_num_args() < 3) {
 51:                     throw new Nette\InvalidArgumentException("Missing item '$k'.");
 52:                 }
 53:                 return $default;
 54:             }
 55:         }
 56:         return $arr;
 57:     }
 58: 
 59: 
 60: 
 61:     /**
 62:      * Returns reference to array item or $default if item is not set.
 63:      * @param  mixed  array
 64:      * @param  mixed  key
 65:      * @return mixed
 66:      */
 67:     public static function & getRef(& $arr, $key)
 68:     {
 69:         foreach (is_array($key) ? $key : array($key) as $k) {
 70:             if (is_array($arr) || $arr === NULL) {
 71:                 $arr = & $arr[$k];
 72:             } else {
 73:                 throw new Nette\InvalidArgumentException('Traversed item is not an array.');
 74:             }
 75:         }
 76:         return $arr;
 77:     }
 78: 
 79: 
 80: 
 81:     /**
 82:      * Recursively appends elements of remaining keys from the second array to the first.
 83:      * @param  array
 84:      * @param  array
 85:      * @return array
 86:      */
 87:     public static function mergeTree($arr1, $arr2)
 88:     {
 89:         $res = $arr1 + $arr2;
 90:         foreach (array_intersect_key($arr1, $arr2) as $k => $v) {
 91:             if (is_array($v) && is_array($arr2[$k])) {
 92:                 $res[$k] = self::mergeTree($v, $arr2[$k]);
 93:             }
 94:         }
 95:         return $res;
 96:     }
 97: 
 98: 
 99: 
100:     /**
101:      * Searches the array for a given key and returns the offset if successful.
102:      * @param  array  input array
103:      * @param  mixed  key
104:      * @return int    offset if it is found, FALSE otherwise
105:      */
106:     public static function searchKey($arr, $key)
107:     {
108:         $foo = array($key => NULL);
109:         return array_search(key($foo), array_keys($arr), TRUE);
110:     }
111: 
112: 
113: 
114:     /**
115:      * Inserts new array before item specified by key.
116:      * @param  array  input array
117:      * @param  mixed  key
118:      * @param  array  inserted array
119:      * @return void
120:      */
121:     public static function insertBefore(array &$arr, $key, array $inserted)
122:     {
123:         $offset = self::searchKey($arr, $key);
124:         $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
125:     }
126: 
127: 
128: 
129:     /**
130:      * Inserts new array after item specified by key.
131:      * @param  array  input array
132:      * @param  mixed  key
133:      * @param  array  inserted array
134:      * @return void
135:      */
136:     public static function insertAfter(array &$arr, $key, array $inserted)
137:     {
138:         $offset = self::searchKey($arr, $key);
139:         $offset = $offset === FALSE ? count($arr) : $offset + 1;
140:         $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
141:     }
142: 
143: 
144: 
145:     /**
146:      * Renames key in array.
147:      * @param  array
148:      * @param  mixed  old key
149:      * @param  mixed  new key
150:      * @return void
151:      */
152:     public static function renameKey(array &$arr, $oldKey, $newKey)
153:     {
154:         $offset = self::searchKey($arr, $oldKey);
155:         if ($offset !== FALSE) {
156:             $keys = array_keys($arr);
157:             $keys[$offset] = $newKey;
158:             $arr = array_combine($keys, $arr);
159:         }
160:     }
161: 
162: 
163: 
164:     /**
165:      * Returns array entries that match the pattern.
166:      * @param  array
167:      * @param  string
168:      * @param  int
169:      * @return array
170:      */
171:     public static function grep(array $arr, $pattern, $flags = 0)
172:     {
173:         Nette\Diagnostics\Debugger::tryError();
174:         $res = preg_grep($pattern, $arr, $flags);
175:         if (Nette\Diagnostics\Debugger::catchError($e) || preg_last_error()) { // compile error XOR run-time error
176:             throw new RegexpException($e ? $e->getMessage() : NULL, $e ? NULL : preg_last_error(), $pattern);
177:         }
178:         return $res;
179:     }
180: 
181: 
182: 
183:     /**
184:      * Returns flattened array.
185:      * @param  array
186:      * @return array
187:      */
188:     public static function flatten(array $arr)
189:     {
190:         $res = array();
191:         array_walk_recursive($arr, function($a) use (& $res) { $res[] = $a; });
192:         return $res;
193:     }
194: 
195: }
196: 
Nette Framework 2.0.4 API API documentation generated by ApiGen 2.7.0