1+ <?php
2+ namespace Mirakl \Core \Domain \Collection ;
3+
4+ use Mirakl \Core \Domain \ArrayableInterface ;
5+
6+ class MiraklCollection implements ArrayableInterface, \ArrayAccess, \IteratorAggregate, \Countable
7+ {
8+ /**
9+ * Collection items
10+ *
11+ * @var array
12+ */
13+ protected $ items = [];
14+
15+ /**
16+ * @var string|null
17+ */
18+ protected $ itemClass ;
19+
20+ /**
21+ * @var int
22+ */
23+ protected $ totalCount ;
24+
25+ /**
26+ * @param array $items
27+ * @param int|null $totalCount
28+ */
29+ public function __construct (array $ items = [], $ totalCount = null )
30+ {
31+ $ this ->setItems ($ items );
32+ if (null !== $ totalCount ) {
33+ $ this ->setTotalCount ($ totalCount );
34+ }
35+ }
36+
37+ /**
38+ * @param mixed $item
39+ * @return $this
40+ */
41+ public function add ($ item )
42+ {
43+ $ this ->items [] = !is_object ($ item ) ? $ this ->newItem ($ item ) : $ item ;
44+
45+ return $ this ;
46+ }
47+
48+ /**
49+ * @return int
50+ */
51+ public function count ()
52+ {
53+ return count ($ this ->items );
54+ }
55+
56+ /**
57+ * @param array $items
58+ * @param int|null $totalCount
59+ * @return $this
60+ */
61+ public static function create (array $ items = [], $ totalCount = null )
62+ {
63+ return new static ($ items , $ totalCount );
64+ }
65+
66+ /**
67+ * @return mixed
68+ */
69+ public function current ()
70+ {
71+ return current ($ this ->items );
72+ }
73+
74+ /**
75+ * Useful method for requests returning collections
76+ *
77+ * @param string|null $key
78+ * @return \Mirakl\Core\Response\Decorator\MiraklCollection
79+ */
80+ public static function decorator ($ key = null )
81+ {
82+ return new \Mirakl \Core \Response \Decorator \MiraklCollection (static ::class, $ key );
83+ }
84+
85+ /**
86+ * @param mixed $offset
87+ * @return bool
88+ */
89+ public function exists ($ offset )
90+ {
91+ return $ this ->offsetExists ($ offset );
92+ }
93+
94+ /**
95+ * @return mixed
96+ */
97+ public function first ()
98+ {
99+ return reset ($ this ->items );
100+ }
101+
102+ /**
103+ * @param $offset
104+ * @return mixed
105+ */
106+ public function get ($ offset )
107+ {
108+ return $ this ->offsetGet ($ offset );
109+ }
110+
111+ /**
112+ * @inheritdoc
113+ */
114+ public function getEmptyValue ()
115+ {
116+ return [];
117+ }
118+
119+ /**
120+ * @return array
121+ */
122+ public function getItems ()
123+ {
124+ return $ this ->items ;
125+ }
126+
127+ /**
128+ * @return int
129+ */
130+ public function getTotalCount ()
131+ {
132+ return $ this ->totalCount ;
133+ }
134+
135+ /**
136+ * @return \ArrayIterator
137+ */
138+ public function getIterator ()
139+ {
140+ return new \ArrayIterator ($ this ->items );
141+ }
142+
143+ /**
144+ * @inheritdoc
145+ */
146+ public function isEmpty ()
147+ {
148+ return empty ($ this ->items );
149+ }
150+
151+ /**
152+ * @return mixed
153+ */
154+ public function last ()
155+ {
156+ return end ($ this ->items );
157+ }
158+
159+ /**
160+ * @return mixed
161+ */
162+ public function next ()
163+ {
164+ return next ($ this ->items );
165+ }
166+
167+ /**
168+ * @param array $item
169+ * @return array|object
170+ */
171+ public function newItem (array $ item )
172+ {
173+ return strlen ($ this ->itemClass ) ? new $ this ->itemClass ($ item ) : $ item ;
174+ }
175+
176+ /**
177+ * @param mixed $offset
178+ * @return bool
179+ */
180+ public function offsetExists ($ offset )
181+ {
182+ return isset ($ this ->items [$ offset ]);
183+ }
184+
185+ /**
186+ * @param mixed $offset
187+ * @return mixed
188+ */
189+ public function offsetGet ($ offset )
190+ {
191+ return isset ($ this ->items [$ offset ]) ? $ this ->items [$ offset ] : null ;
192+ }
193+
194+ /**
195+ * @param mixed $key
196+ * @param mixed $value
197+ * @return $this
198+ */
199+ public function offsetSet ($ key , $ value )
200+ {
201+ $ this ->items [$ key ] = $ value ;
202+
203+ return $ this ;
204+ }
205+
206+ /**
207+ * @param mixed $offset
208+ * @return $this
209+ */
210+ public function offsetUnset ($ offset )
211+ {
212+ if (isset ($ this ->items [$ offset ])) {
213+ unset($ this ->items [$ offset ]);
214+ }
215+
216+ return $ this ;
217+ }
218+
219+ /**
220+ * @return mixed
221+ */
222+ public function prev ()
223+ {
224+ return prev ($ this ->items );
225+ }
226+
227+ /**
228+ * @param mixed $offset
229+ * @return $this
230+ */
231+ public function remove ($ offset )
232+ {
233+ return $ this ->offsetUnset ($ offset );
234+ }
235+
236+ /**
237+ * @return $this
238+ */
239+ public function reset ()
240+ {
241+ $ this ->items = [];
242+
243+ return $ this ;
244+ }
245+
246+ /**
247+ * @param mixed $key
248+ * @param mixed $value
249+ * @return $this
250+ */
251+ public function set ($ key , $ value )
252+ {
253+ return $ this ->offsetSet ($ key , $ value );
254+ }
255+
256+ /**
257+ * @param string $class
258+ * @return $this
259+ */
260+ public function setItemClass ($ class )
261+ {
262+ $ this ->itemClass = $ class ;
263+
264+ return $ this ;
265+ }
266+
267+ /**
268+ * @param array $items
269+ * @return $this
270+ */
271+ public function setItems (array $ items )
272+ {
273+ if ($ this ->itemClass ) {
274+ array_walk ($ items , function (&$ item ) {
275+ if (is_array ($ item )) {
276+ $ item = $ this ->newItem ($ item );
277+ }
278+ });
279+ }
280+
281+ $ this ->items = $ items ;
282+
283+ return $ this ;
284+ }
285+
286+ /**
287+ * @param int $totalCount
288+ * @return $this
289+ */
290+ public function setTotalCount ($ totalCount )
291+ {
292+ $ this ->totalCount = $ totalCount ;
293+
294+ return $ this ;
295+ }
296+
297+ /**
298+ * @return array
299+ */
300+ public function toArray ()
301+ {
302+ $ result = [];
303+ foreach ($ this as $ item ) {
304+ if (is_object ($ item ) && $ item instanceof ArrayableInterface) {
305+ $ item = $ item ->toArray ();
306+ }
307+ $ result [] = $ item ;
308+ }
309+
310+ return $ result ;
311+ }
312+
313+ /**
314+ * @param string $method
315+ * @param array $args
316+ * @return array
317+ */
318+ public function walk ($ method , array $ args = [])
319+ {
320+ $ result = [];
321+ foreach ($ this as $ item ) {
322+ $ result [] = call_user_func_array ([$ item , $ method ], $ args );
323+ }
324+
325+ return $ result ;
326+ }
327+ }
0 commit comments