1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004, 2011 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: */
20: class ArrayHash implements ArrayAccess, Countable, IteratorAggregate
21: {
22:
23: /**
24: * @param array to wrap
25: * @return ArrayHash
26: */
27: public static function from($arr)
28: {
29: $obj = new self;
30: foreach ($arr as $key => $value) {
31: $obj->$key = $value;
32: }
33: return $obj;
34: }
35:
36:
37:
38: /**
39: * Returns an iterator over all items.
40: * @return ArrayIterator
41: */
42: public function getIterator()
43: {
44: return new ArrayIterator($this);
45: }
46:
47:
48:
49: /**
50: * Returns items count.
51: * @return int
52: */
53: public function count()
54: {
55: return count((array) $this);
56: }
57:
58:
59:
60: /**
61: * Replaces or appends a item.
62: * @param mixed
63: * @param mixed
64: * @return void
65: */
66: public function offsetSet($key, $value)
67: {
68: if (!is_scalar($key)) { // prevents NULL
69: throw new InvalidArgumentException("Key must be either a string or an integer, " . gettype($key) ." given.");
70: }
71: $this->$key = $value;
72: }
73:
74:
75:
76: /**
77: * Returns a item.
78: * @param mixed
79: * @return mixed
80: */
81: public function offsetGet($key)
82: {
83: return $this->$key;
84: }
85:
86:
87:
88: /**
89: * Determines whether a item exists.
90: * @param mixed
91: * @return bool
92: */
93: public function offsetExists($key)
94: {
95: return isset($this->$key);
96: }
97:
98:
99:
100: /**
101: * Removes the element from this list.
102: * @param mixed
103: * @return void
104: */
105: public function offsetUnset($key)
106: {
107: unset($this->$key);
108: }
109:
110: }
111: