1 <?php
2 /**
3 * Cross - a micro PHP 5 framework
4 *
5 * @link http://www.crossphp.com
6 * @license MIT License
7 */
8 namespace Cross\Core;
9
10 use ArrayIterator;
11
12 /**
13 * @author wonli <wonli@live.com>
14 * Class ArrayMap
15 * @package Cross\Core
16 */
17 class ArrayMap extends ArrayIterator
18 {
19 /**
20 * 构造函数
21 *
22 * @param array $array
23 */
24 public function __construct(array $array = array())
25 {
26 foreach ($array as &$value) {
27 if (is_array($value) && isset($value)) {
28 $value = new self($value);
29 }
30 }
31 parent::__construct($array);
32 }
33
34 /**
35 * 实例化类
36 *
37 * @param $array
38 * @return ArrayMap
39 */
40 static public function init($array)
41 {
42 return new self($array);
43 }
44
45 /**
46 * _get
47 *
48 * @param $index
49 * @return mixed
50 */
51 public function __get($index)
52 {
53 return $this->offsetGet($index);
54 }
55
56 /**
57 * 设置
58 *
59 * @param $index
60 * @param $value
61 */
62 public function __set($index, $value)
63 {
64 if (is_array($value) && isset($value)) {
65 $value = new self($value);
66 }
67
68 $this->offsetSet($index, $value);
69 }
70
71 /**
72 * 值是否存在
73 *
74 * @param $index
75 * @return bool
76 */
77 public function __isset($index)
78 {
79 return $this->offsetExists($index);
80 }
81
82 /**
83 * 清空值
84 *
85 * @param $index
86 */
87 public function __unset($index)
88 {
89 $this->offsetUnset($index);
90 }
91
92 /**
93 * 转换为数组
94 *
95 * @param array $array
96 * @return array
97 */
98 public function toArray($array = array())
99 {
100 if (empty($array)) {
101 $array = $this->getArrayCopy();
102 }
103
104 foreach ($array as &$value) {
105 if ($value instanceof self) {
106 $value = $value->toArray();
107 }
108 }
109
110 return $array;
111 }
112
113 /**
114 * 输出字符串
115 *
116 * @return mixed
117 */
118 public function __toString()
119 {
120 return var_export($this->toArray(), true);
121 }
122
123 /**
124 * 输出为json
125 *
126 * @return string
127 */
128 public function json()
129 {
130 return json_encode($this->toArray());
131 }
132
133 /**
134 * 设置值
135 *
136 * @param $index
137 * @param $value
138 */
139 public function put($index, $value)
140 {
141 if (is_array($value) && isset($value)) {
142 $value = new self($value);
143 }
144 $this->offsetSet($index, $value);
145 }
146
147 /**
148 * @see put()
149 * @param $index
150 * @param $value
151 */
152 public function set($index, $value)
153 {
154 $this->put($index, $value);
155 }
156
157 /**
158 * 获取值
159 * <ul>
160 * <li>$index为字符串的时候 获取配置数组,此时设定$key 则获取数组中指定项的值</li>
161 * <li>$index为数组的时候 获取数组中指定的配置项</li>
162 * </ul>
163 *
164 * @param $index
165 * @param null $key
166 * @return array|bool|mixed
167 */
168 public function get($index, $key = null)
169 {
170 if (is_array($index)) {
171 $result = array();
172 foreach ($index as $i) {
173 if ($this->__isset($i)) {
174 $result[$i] = $this->offsetGet($i);
175 }
176 }
177
178 return $result;
179 } else {
180 if ($this->__isset($index)) {
181 $index_value = $this->offsetGet($index);
182 if (null !== $key && $index_value instanceof self) {
183 return $index_value->get($key);
184 }
185
186 return $index_value;
187 }
188
189 return false;
190 }
191 }
192 }
193