1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace Pry\Config;
14:
15: 16: 17: 18: 19: 20: 21: 22: 23:
24: class Config implements \Countable, \Iterator
25: {
26:
27: 28: 29: 30:
31: protected $index = 0;
32:
33: 34: 35: 36:
37: protected $count;
38:
39: 40: 41: 42:
43: private $skipNextIteration = false;
44:
45: 46: 47: 48:
49: protected $datas = array();
50:
51: 52: 53: 54:
55: protected $errorStr = null;
56:
57: 58: 59: 60: 61: 62:
63: protected $_extends = array();
64:
65: public function __construct(array $array)
66: {
67: foreach ($array as $key => $value) {
68: if (is_array($value))
69: {
70: $this->datas[$key] = new self($value);
71: }
72: else
73: {
74: $this->datas[$key] = $value;
75: }
76:
77: $this->count++;
78: }
79: }
80:
81: 82: 83: 84: 85: 86: 87:
88: protected function errorHandler($errno, $errstr, $errfile, $errline)
89: {
90: if ($this->errorStr === null)
91: {
92: $this->errorStr = $errno . ':' . $errstr . ' on ' . $errfile . ' at ' . $errline;
93: }
94: else
95: {
96: $this->errorStr .= (PHP_EOL . $errstr);
97: }
98: }
99:
100: 101: 102: 103: 104: 105: 106: 107: 108:
109: protected function checkForCircularInheritance($extendingSection, $extendedSection)
110: {
111:
112: $extendedSectionCurrent = $extendedSection;
113: while (array_key_exists($extendedSectionCurrent, $this->_extends)) {
114: if ($this->_extends[$extendedSectionCurrent] == $extendingSection)
115: {
116: throw new \Exception('Illegal circular inheritance detected');
117: }
118: $extendedSectionCurrent = $this->_extends[$extendedSectionCurrent];
119: }
120:
121: $this->_extends[$extendingSection] = $extendedSection;
122: }
123:
124: 125: 126: 127:
128: public function toArray()
129: {
130: $array = array();
131: foreach ($this->datas as $key => $value) {
132: if ($value instanceof Config_Config)
133: $array[$key] = $value->toArray();
134: else
135: $array[$key] = $value;
136: }
137: return $array;
138: }
139:
140: 141: 142: 143: 144: 145: 146: 147:
148: protected function _arrayMergeRecursive($firstArray, $secondArray)
149: {
150: if (is_array($firstArray) && is_array($secondArray))
151: {
152: foreach ($secondArray as $key => $value) {
153: if (isset($firstArray[$key]))
154: {
155: $firstArray[$key] = $this->_arrayMergeRecursive($firstArray[$key], $value);
156: }
157: else
158: {
159: if ($key === 0)
160: {
161: $firstArray = array(0 => $this->_arrayMergeRecursive($firstArray, $value));
162: }
163: else
164: {
165: $firstArray[$key] = $value;
166: }
167: }
168: }
169: }
170: else
171: {
172: $firstArray = $secondArray;
173: }
174:
175: return $firstArray;
176: }
177:
178: private function get($name, $default = null)
179: {
180: $result = $default;
181: if (array_key_exists($name, $this->datas))
182: {
183: $result = $this->datas[$name];
184: }
185: return $result;
186: }
187:
188: public function __get($name)
189: {
190: return $this->get($name);
191: }
192:
193: public function __isset($name)
194: {
195: return isset($this->datas[$name]);
196: }
197:
198: public function __unset($name)
199: {
200: unset($this->datas[$name]);
201: $this->count = count($this->datas);
202: $this->skipNextIteration = true;
203: }
204:
205: public function count()
206: {
207: return count($this->datas);
208: }
209:
210: public function current()
211: {
212: $this->skipNextIteration = false;
213: return current($this->datas);
214: }
215:
216: public function key()
217: {
218: return key($this->datas);
219: }
220:
221: public function next()
222: {
223: if ($this->skipNextIteration)
224: {
225: $this->skipNextIteration = false;
226: return;
227: }
228: next($this->datas);
229: $this->index++;
230: }
231:
232: public function rewind()
233: {
234: $this->_skipNextIteration = false;
235: reset($this->datas);
236: $this->index = 0;
237: }
238:
239: public function valid()
240: {
241: return $this->index < count($this->datas);
242: }
243:
244: }