1 | <?php |
||
14 | trait CollectionReadTrait |
||
15 | { |
||
16 | /** |
||
17 | * Returns reference to array storing collection items. |
||
18 | * |
||
19 | * @return array |
||
20 | */ |
||
21 | abstract protected function &items(); |
||
22 | |||
23 | /** |
||
24 | * Creates collection of items. |
||
25 | * |
||
26 | * Override it if you need to implement |
||
27 | * derived collection that requires specific initialization. |
||
28 | * |
||
29 | * @param array $items |
||
30 | * |
||
31 | * @return static |
||
32 | */ |
||
33 | protected function createCollection(array $items) |
||
41 | |||
42 | /** |
||
43 | * Returns collection items in array. |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | 15 | public function toArray() |
|
51 | |||
52 | /** |
||
53 | * Returns true if collection is empty. |
||
54 | * |
||
55 | * @return bool |
||
56 | */ |
||
57 | 12 | public function isEmpty() |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 5 | public function count() |
|
69 | |||
70 | /** |
||
71 | * Checks that collections contains target item. |
||
72 | * |
||
73 | * @param $item |
||
74 | * |
||
75 | * @return bool |
||
76 | */ |
||
77 | 6 | public function contains($item) |
|
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | 12 | public function getIterator() |
|
92 | |||
93 | /** |
||
94 | * Returns first item of the collection or null if collection is empty. |
||
95 | * |
||
96 | * @return mixed|null |
||
97 | */ |
||
98 | 4 | public function first() |
|
102 | |||
103 | /** |
||
104 | * Iterates over each value in the <b>collection</b> |
||
105 | * passing them to the <b>callback</b> function. |
||
106 | * If the <b>callback</b> function returns true, the |
||
107 | * current value from <b>collection</b> is returned into |
||
108 | * the result collection. |
||
109 | * |
||
110 | * @param callable $callback the callback function to use |
||
111 | * @param array|null $optionalArguments [optional] additional arguments passed to callback |
||
112 | * |
||
113 | * @return CollectionReadInterface|static filtered collection |
||
114 | */ |
||
115 | 5 | public function filter(callable $callback, array $optionalArguments = null) |
|
122 | |||
123 | /** |
||
124 | * Iterates over each value in the <b>collection</b> |
||
125 | * passing them to the <b>callback</b> function. |
||
126 | * If the <b>callback</b> function returns true, the |
||
127 | * current value from <b>collection</b> is returned. |
||
128 | * |
||
129 | * @param callable $callback the callback function to use |
||
130 | * @param array|null $optionalArguments [optional] additional arguments passed to callback |
||
131 | * |
||
132 | * @return mixed|FALSE collection item or false if item is not found. |
||
133 | */ |
||
134 | 5 | public function find(callable $callback, array $optionalArguments = null) |
|
147 | |||
148 | |||
149 | /** |
||
150 | * @param callable $callback the callback function to use |
||
151 | * @param array|null $optionalArguments [optional] additional arguments passed to callback |
||
152 | * @return CollectionReadInterface|static |
||
153 | */ |
||
154 | public function map(callable $callback, array $optionalArguments = null) |
||
161 | |||
162 | /** |
||
163 | * Sorts collection items. |
||
164 | * |
||
165 | * This method preserves key order (stable sort) using Schwartzian Transform. |
||
166 | * @see http://stackoverflow.com/questions/4353739/preserve-key-order-stable-sort-when-sorting-with-phps-uasort |
||
167 | * @see http://en.wikipedia.org/wiki/Schwartzian_transform |
||
168 | * |
||
169 | * @param callable $compareFunction |
||
170 | * @return CollectionReadInterface|static new collection with sorted items. |
||
171 | */ |
||
172 | public function sort(callable $compareFunction) |
||
194 | |||
195 | /** |
||
196 | * @return mixed|null |
||
197 | */ |
||
198 | 4 | public function random() |
|
206 | |||
207 | /** |
||
208 | * @param $item |
||
209 | * @return CollectionReadInterface|static |
||
210 | */ |
||
211 | public function beforeItem($item) |
||
228 | |||
229 | /** |
||
230 | * @param $item |
||
231 | * @return CollectionReadInterface|static |
||
232 | */ |
||
233 | public function afterItem($item) |
||
254 | |||
255 | public function isWritable() |
||
259 | |||
260 | 5 | protected static function bindAdditionalArguments(callable $callback, array $additionalArguments = null) |
|
270 | } |
||
271 |