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

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

Exceptions

  • AssertionException
  • JsonException
  • NeonException
  • RegexpException
  • TokenizerException
  • 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 Arrays
 22: {
 23: 
 24:     /**
 25:      * Static class - cannot be instantiated.
 26:      */
 27:     final public function __construct()
 28:     {
 29:         throw new StaticClassException;
 30:     }
 31: 
 32: 
 33:     /**
 34:      * Returns item from array or $default if item is not set.
 35:      * @return mixed
 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:      * @return mixed
 56:      */
 57:     public static function & getRef(& $arr, $key)
 58:     {
 59:         foreach (is_array($key) ? $key : array($key) as $k) {
 60:             if (is_array($arr) || $arr === NULL) {
 61:                 $arr = & $arr[$k];
 62:             } else {
 63:                 throw new InvalidArgumentException('Traversed item is not an array.');
 64:             }
 65:         }
 66:         return $arr;
 67:     }
 68: 
 69: 
 70:     /**
 71:      * Recursively appends elements of remaining keys from the second array to the first.
 72:      * @return array
 73:      */
 74:     public static function mergeTree($arr1, $arr2)
 75:     {
 76:         $res = $arr1 + $arr2;
 77:         foreach (array_intersect_key($arr1, $arr2) as $k => $v) {
 78:             if (is_array($v) && is_array($arr2[$k])) {
 79:                 $res[$k] = self::mergeTree($v, $arr2[$k]);
 80:             }
 81:         }
 82:         return $res;
 83:     }
 84: 
 85: 
 86:     /**
 87:      * Searches the array for a given key and returns the offset if successful.
 88:      * @return int    offset if it is found, FALSE otherwise
 89:      */
 90:     public static function searchKey($arr, $key)
 91:     {
 92:         $foo = array($key => NULL);
 93:         return array_search(key($foo), array_keys($arr), TRUE);
 94:     }
 95: 
 96: 
 97:     /**
 98:      * Inserts new array before item specified by key.
 99:      * @return void
100:      */
101:     public static function insertBefore(array & $arr, $key, array $inserted)
102:     {
103:         $offset = self::searchKey($arr, $key);
104:         $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
105:     }
106: 
107: 
108:     /**
109:      * Inserts new array after item specified by key.
110:      * @return void
111:      */
112:     public static function insertAfter(array & $arr, $key, array $inserted)
113:     {
114:         $offset = self::searchKey($arr, $key);
115:         $offset = $offset === FALSE ? count($arr) : $offset + 1;
116:         $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
117:     }
118: 
119: 
120:     /**
121:      * Renames key in array.
122:      * @return void
123:      */
124:     public static function renameKey(array & $arr, $oldKey, $newKey)
125:     {
126:         $offset = self::searchKey($arr, $oldKey);
127:         if ($offset !== FALSE) {
128:             $keys = array_keys($arr);
129:             $keys[$offset] = $newKey;
130:             $arr = array_combine($keys, $arr);
131:         }
132:     }
133: 
134: 
135:     /**
136:      * Returns array entries that match the pattern.
137:      * @return array
138:      */
139:     public static function grep(array $arr, $pattern, $flags = 0)
140:     {
141:         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
142:             restore_error_handler();
143:             throw new RegexpException("$message in pattern: $pattern");
144:         '));
145:         $res = preg_grep($pattern, $arr, $flags);
146:         restore_error_handler();
147:         if (preg_last_error()) { // run-time error
148:             throw new RegexpException(NULL, preg_last_error(), $pattern);
149:         }
150:         return $res;
151:     }
152: 
153: 
154:     /**
155:      * Returns flattened array.
156:      * @return array
157:      */
158:     public static function flatten(array $arr)
159:     {
160:         $res = array();
161:         array_walk_recursive($arr, create_function('$a', 'extract($GLOBALS[0]['.array_push($GLOBALS[0], array('res'=>& $res)).'-1], EXTR_REFS); $res[] = $a; '));
162:         return $res;
163:     }
164: 
165: }
166: 
Nette Framework 2.0.12 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.8.0