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