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
11: */
12:
13:
14:
15: /**
16: * Provides objects to work as array.
17: *
18: * @author David Grudl
19: * @package Nette
20: */
21: class ArrayHash extends stdClass implements ArrayAccess, Countable, IteratorAggregate
22: {
23:
24: /**
25: * @param array to wrap
26: * @param bool
27: * @return ArrayHash
28: */
29: public static function from($arr, $recursive = TRUE)
30: {
31: $obj = new self;
32: foreach ($arr as $key => $value) {
33: if ($recursive && is_array($value)) {
34: $obj->$key = self::from($value, TRUE);
35: } else {
36: $obj->$key = $value;
37: }
38: }
39: return $obj;
40: }
41:
42:
43: /**
44: * Returns an iterator over all items.
45: * @return RecursiveArrayIterator
46: */
47: public function getIterator()
48: {
49: return new RecursiveArrayIterator($this);
50: }
51:
52:
53: /**
54: * Returns items count.
55: * @return int
56: */
57: public function count()
58: {
59: return count((array) $this);
60: }
61:
62:
63: /**
64: * Replaces or appends a item.
65: * @return void
66: */
67: public function offsetSet($key, $value)
68: {
69: if (!is_scalar($key)) { // prevents NULL
70: throw new InvalidArgumentException("Key must be either a string or an integer, " . gettype($key) ." given.");
71: }
72: $this->$key = $value;
73: }
74:
75:
76: /**
77: * Returns a item.
78: * @return mixed
79: */
80: public function offsetGet($key)
81: {
82: return $this->$key;
83: }
84:
85:
86: /**
87: * Determines whether a item exists.
88: * @return bool
89: */
90: public function offsetExists($key)
91: {
92: return isset($this->$key);
93: }
94:
95:
96: /**
97: * Removes the element from this list.
98: * @return void
99: */
100: public function offsetUnset($key)
101: {
102: unset($this->$key);
103: }
104:
105: }
106: