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