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