1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Kotchasan;
12:
13: 14: 15: 16: 17: 18: 19:
20: class ListItem
21: {
22: 23: 24: 25: 26:
27: public $datas;
28: 29: 30: 31: 32:
33: private $source;
34:
35: 36: 37: 38: 39:
40: public function assign($array)
41: {
42: if (isset($this->datas)) {
43: $this->datas = array_merge($this->datas, $array);
44: } else {
45: $this->datas = $array;
46: }
47: }
48:
49: 50: 51:
52: public function clear()
53: {
54: unset($this->datas);
55: }
56:
57: 58: 59: 60: 61:
62: public function count()
63: {
64: return count($this->datas);
65: }
66:
67: 68: 69: 70: 71: 72: 73: 74:
75: public function delete($key)
76: {
77: if (array_key_exists($key, $this->datas)) {
78: unset($this->datas[$key]);
79:
80: return true;
81: }
82:
83: return false;
84: }
85:
86: 87: 88: 89: 90:
91: public function firstItem()
92: {
93: return reset($this->datas);
94: }
95:
96: 97: 98: 99: 100: 101: 102: 103:
104: public function get($key)
105: {
106: return array_key_exists($key, $this->datas) ? $this->datas[$key] : null;
107: }
108:
109: 110: 111: 112: 113: 114: 115: 116:
117: public function indexOf($value)
118: {
119: return array_search($value, $this->datas);
120: }
121:
122: 123: 124: 125: 126:
127: public function init($config)
128: {
129: $this->datas = $config;
130: }
131:
132: 133: 134: 135: 136: 137:
138: public function insert($key, $item)
139: {
140: if (is_int($key) && $key == count($this->datas)) {
141: $this->datas[] = $item;
142: } else {
143: $temp = $this->datas;
144: $this->datas = array();
145: foreach ($temp as $k => $value) {
146: $this->datas[$k] = $value;
147: if ($k == $key) {
148: $this->datas[$key] = $item;
149: }
150: }
151: }
152: }
153:
154: 155: 156: 157: 158: 159:
160: public function insertBefore($key, $item)
161: {
162: $temp = $this->datas;
163: $this->datas = array();
164: foreach ($temp as $k => $value) {
165: if ($k == $key) {
166: $this->datas[$key] = $item;
167: }
168: $this->datas[$k] = $value;
169: }
170: }
171:
172: 173: 174: 175: 176:
177: public function items()
178: {
179: return $this->datas;
180: }
181:
182: 183: 184: 185: 186: 187:
188: public function keys()
189: {
190: return array_keys($this->datas);
191: }
192:
193: 194: 195: 196: 197:
198: public function lastItem()
199: {
200: return end($this->datas);
201: }
202:
203: 204: 205: 206: 207: 208: 209:
210: public function loadFromFile($file)
211: {
212: if (is_file($file)) {
213: $config = include $file;
214: $this->source = $file;
215: $this->assign($config);
216: }
217:
218: return $this;
219: }
220:
221: 222: 223: 224: 225: 226:
227: public function saveToFile()
228: {
229: if (!isset($this->source) || empty($this->datas)) {
230: return false;
231: } else {
232: $datas = array();
233: foreach ($this->datas as $key => $value) {
234: if (is_array($value)) {
235: $datas[] = (is_int($key) ? $key : "'".strtolower($key)."'")." => array(\n".$this->arrayToString(1, $value)."\n\t)";
236: } else {
237: $datas[] = (is_int($key) ? $key : "'".strtolower($key)."'").' => '.(is_int($value) ? $value : "'".addslashes($value)."'");
238: }
239: }
240: $file = str_replace(ROOT_PATH, '', $this->source);
241: $f = @fopen(ROOT_PATH.$file, 'w');
242: if ($f === false) {
243: return false;
244: } else {
245: fwrite($f, "<?php\n/* $file */\nreturn array (\n\t".implode(",\n\t", $datas)."\n);");
246: fclose($f);
247:
248: return true;
249: }
250: }
251: }
252:
253: 254: 255: 256: 257: 258:
259: public function set($key, $value)
260: {
261: $this->datas[$key] = $value;
262: }
263:
264: 265: 266: 267: 268:
269: public function values()
270: {
271: return array_values($this->datas);
272: }
273:
274: 275: 276: 277: 278: 279: 280: 281:
282: private function arrayToString($indent, $array)
283: {
284: $t = str_repeat("\t", $indent + 1);
285: foreach ($array as $key => $value) {
286: if (is_array($value)) {
287: $datas[] = (is_int($key) ? $key : "'$key'")." => array(\n".$this->arrayToString($indent + 1, $value)."\n$t)";
288: } else {
289: $datas[] = (is_int($key) ? $key : "'$key'").' => '.(is_int($value) ? $value : "'".addslashes($value)."'");
290: }
291: }
292:
293: return $t.implode(",\n$t", $datas);
294: }
295: }
296: