1: <?php
2: /**
3: * @filesource Kotchasan/Collection.php
4: *
5: * @copyright 2016 Goragod.com
6: * @license http://www.kotchasan.com/license/
7: *
8: * @see http://www.kotchasan.com/
9: */
10:
11: namespace Kotchasan;
12:
13: /**
14: * Collection Class.
15: *
16: * @author Goragod Wiriya <admin@goragod.com>
17: *
18: * @since 1.0
19: */
20: class Collection implements \Countable, \IteratorAggregate, \ArrayAccess
21: {
22: /**
23: * ตัวแปรเก็บสมาชิกของคลาส.
24: *
25: * @var array
26: */
27: private $datas = array();
28:
29: /**
30: * Create new collection.
31: *
32: * @param array $items สมาชิกเริ่มต้นของ Collection
33: */
34: public function __construct(array $items = array())
35: {
36: foreach ($items as $key => $value) {
37: $this->set($key, $value);
38: }
39: }
40:
41: /**
42: * ลบข้อมูลทั้งหมด.
43: */
44: public function clear()
45: {
46: $this->datas = array();
47: }
48:
49: /**
50: * คืนค่าจำนวนข้อมูลทั้งหมด.
51: *
52: * @return int
53: */
54: public function count()
55: {
56: return count($this->datas);
57: }
58:
59: /**
60: * อ่านข้อมูลที่ $key ถ้าไม่พบคืนค่า $default.
61: *
62: * @param string $key
63: * @param mixed $default
64: *
65: * @return mixed
66: */
67: public function get($key, $default = null)
68: {
69: return $this->has($key) ? $this->datas[$key] : $default;
70: }
71:
72: /* * **********************
73: * IteratorAggregate interface
74: * ************************* */
75:
76: /**
77: * Retrieve an external iterator.
78: *
79: * @return \ArrayIterator
80: */
81: public function getIterator()
82: {
83: return new \ArrayIterator($this->datas);
84: }
85:
86: /**
87: * ตรวจสอบว่ามีรายการ $key หรือไม่.
88: *
89: * @param string $key
90: *
91: * @return bool
92: */
93: public function has($key)
94: {
95: return array_key_exists($key, $this->datas);
96: }
97:
98: /**
99: * อ่านรายชื่อ keys
100: * คืนค่าแอเรย์ของรายการ key ทั้งหมด.
101: *
102: * @return array
103: */
104: public function keys()
105: {
106: return array_keys($this->datas);
107: }
108:
109: /* * *****************
110: * ArrayAccess interface
111: * ********************* */
112:
113: /**
114: * ตรวจสอบว่ามีรายการ $key หรือไม่.
115: *
116: * @param string $key
117: *
118: * @return bool
119: */
120: public function offsetExists($key)
121: {
122: return $this->has($key);
123: }
124:
125: /**
126: * อ่านข้อมูลที่ $key.
127: *
128: * @param string $key
129: *
130: * @return mixed
131: */
132: public function offsetGet($key)
133: {
134: return $this->get($key);
135: }
136:
137: /**
138: * กำหนดค่า $value ของ $key.
139: *
140: * @param string $key
141: * @param mixed $value
142: */
143: public function offsetSet($key, $value)
144: {
145: $this->set($key, $value);
146: }
147:
148: /**
149: * ลบรายการที่ $key.
150: *
151: * @param string $key
152: */
153: public function offsetUnset($key)
154: {
155: $this->remove($key);
156: }
157:
158: /**
159: * ลบรายการที่ $key.
160: *
161: * @param string $key
162: */
163: public function remove($key)
164: {
165: unset($this->datas[$key]);
166: }
167:
168: /**
169: * เพิ่มรายการใหม่ แทนที่รายการเดิม
170: *
171: * @param array $items array(array($key => $value), array($key => $value), ...)
172: */
173: public function replace(array $items)
174: {
175: foreach ($items as $key => $value) {
176: $this->set($key, $value);
177: }
178: }
179:
180: /* * ****************
181: * Collection interface
182: * ******************* */
183:
184: /**
185: * กำหนดค่า $value ของ $key.
186: *
187: * @param string $key
188: * @param mixed $value
189: */
190: public function set($key, $value)
191: {
192: $this->datas[$key] = $value;
193: }
194:
195: /**
196: * คืนค่าข้อมูลทั้งหมดเป็น.
197: *
198: * @return array
199: */
200: public function toArray()
201: {
202: return $this->datas;
203: }
204: }
205: