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

Classes

  • NArrays
  • NFinder
  • NHtml
  • NJson
  • NLimitedScope
  • NMimeTypeDetector
  • NNeon
  • NNeonEntity
  • NPaginator
  • NStrings
  • NTokenizer
  • NValidators

Exceptions

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