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
  • NTokenizerException
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  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 item from array or $default if item is not set.
 36:      * @return mixed
 37:      */
 38:     public static function get(array $arr, $key, $default = NULL)
 39:     {
 40:         foreach (is_array($key) ? $key : array($key) as $k) {
 41:             if (is_array($arr) && array_key_exists($k, $arr)) {
 42:                 $arr = $arr[$k];
 43:             } else {
 44:                 if (func_num_args() < 3) {
 45:                     throw new InvalidArgumentException("Missing item '$k'.");
 46:                 }
 47:                 return $default;
 48:             }
 49:         }
 50:         return $arr;
 51:     }
 52: 
 53: 
 54: 
 55:     /**
 56:      * Returns reference to array item or $default if item is not set.
 57:      * @return mixed
 58:      */
 59:     public static function & getRef(& $arr, $key)
 60:     {
 61:         foreach (is_array($key) ? $key : array($key) as $k) {
 62:             if (is_array($arr) || $arr === NULL) {
 63:                 $arr = & $arr[$k];
 64:             } else {
 65:                 throw new InvalidArgumentException('Traversed item is not an array.');
 66:             }
 67:         }
 68:         return $arr;
 69:     }
 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:     /**
 91:      * Searches the array for a given key and returns the offset if successful.
 92:      * @return int    offset if it is found, FALSE otherwise
 93:      */
 94:     public static function searchKey($arr, $key)
 95:     {
 96:         $foo = array($key => NULL);
 97:         return array_search(key($foo), array_keys($arr), TRUE);
 98:     }
 99: 
100: 
101: 
102:     /**
103:      * Inserts new array before item specified by key.
104:      * @return void
105:      */
106:     public static function insertBefore(array &$arr, $key, array $inserted)
107:     {
108:         $offset = self::searchKey($arr, $key);
109:         $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
110:     }
111: 
112: 
113: 
114:     /**
115:      * Inserts new array after item specified by key.
116:      * @return void
117:      */
118:     public static function insertAfter(array &$arr, $key, array $inserted)
119:     {
120:         $offset = self::searchKey($arr, $key);
121:         $offset = $offset === FALSE ? count($arr) : $offset + 1;
122:         $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
123:     }
124: 
125: 
126: 
127:     /**
128:      * Renames key in array.
129:      * @return void
130:      */
131:     public static function renameKey(array &$arr, $oldKey, $newKey)
132:     {
133:         $offset = self::searchKey($arr, $oldKey);
134:         if ($offset !== FALSE) {
135:             $keys = array_keys($arr);
136:             $keys[$offset] = $newKey;
137:             $arr = array_combine($keys, $arr);
138:         }
139:     }
140: 
141: 
142: 
143:     /**
144:      * Returns array entries that match the pattern.
145:      * @return array
146:      */
147:     public static function grep(array $arr, $pattern, $flags = 0)
148:     {
149:         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
150:             restore_error_handler();
151:             throw new NRegexpException("$message in pattern: $pattern");
152:         '));
153:         $res = preg_grep($pattern, $arr, $flags);
154:         restore_error_handler();
155:         if (preg_last_error()) { // run-time error
156:             throw new NRegexpException(NULL, preg_last_error(), $pattern);
157:         }
158:         return $res;
159:     }
160: 
161: 
162: 
163:     /**
164:      * Returns flattened array.
165:      * @return array
166:      */
167:     public static function flatten(array $arr)
168:     {
169:         $res = array();
170:         array_walk_recursive($arr, create_function('$a', 'extract($GLOBALS[0]['.array_push($GLOBALS[0], array('res'=>& $res)).'-1], EXTR_REFS); $res[] = $a; '));
171:         return $res;
172:     }
173: 
174: }
175: 
Nette Framework 2.0.10 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0