jablonka.czprosek.czf

freenet-router

Subversion Repositories:
[/] [trunk/] [freenet-router/] [var/] [www/] [freenet-router/] [Framework/] [Core/] [Collection.php] - Blame information for rev 2

 

Line No. Rev Author Line
12simandl<?php
2 
3namespace Phem\Core;
4 
5use Countable;
6use Iterator;
7use JsonSerializable;
8use Phem\ToolSuite;
9 
10/**
11 * @author Jakub PetrŞílka <petrzilka@czweb.net>
12 *
13 */
14class Collection implements Iterator, Countable, JsonSerializable
15{
16 
17 protected $items = array();
18 
19 public function __construct($items = null)
20 {
21 if (is_array($items))
22 {
23 $this->items = $items;
24 }
25 }
26 
27 public function current()
28 {
29 return current($this->items);
30 }
31 
32 public function key()
33 {
34 return key($this->items);
35 }
36 
37 public function next()
38 {
39 return next($this->items);
40 }
41 
42 public function rewind()
43 {
44 reset($this->items);
45 }
46 
47 public function valid()
48 {
49 $key = key($this->items);
50 return ($key !== null && $key !== false);
51 }
52 
53 public function getFirst()
54 {
55 $this->rewind();
56 return $this->current();
57 }
58 
59 public function getFirstKey()
60 {
61 $this->rewind();
62 return $this->key();
63 }
64 
65 public function getLastKey()
66 {
67 end($this->items);
68 return $this->key();
69 }
70 
71 
72 public function contains($value)
73 {
74 return ($this->getKey($value) != null);
75 }
76 
77 public function containsKey($key)
78 {
79 return array_key_exists($key, $this->items);
80 }
81 
82 public function containsAnyKeyOf(array $keys)
83 {
84 foreach ($keys as $key)
85 {
86 if (array_key_exists($key, $this->items))
87 return true;
88 }
89 return false;
90 }
91 
92 public function put($key, $value)
93 {
94 if (!($this->containsKey($key)))
95 {
96 $this->items[$key] = $value;
97 }
98 }
99 
100 public function add($value)
101 {
102 $this->items[] = $value;
103 }
104 
105 public function set($key, $value)
106 {
107 $this->items[$key] = $value;
108 }
109 
110 public function get($key)
111 {
112 if (array_key_exists($key, $this->items))
113 return $this->items[$key];
114 else
115 return null;
116 }
117 
118 public function getKey($value)
119 {
120 foreach ($this->items as $key => $currentValue)
121 {
122 if ($currentValue == $value)
123 return $key;
124 }
125 return null;
126 }
127 
128 public function count()
129 {
130 return count($this->items);
131 }
132 
133 public function uniqueValues()
134 {
135 return array_unique($this->items);
136 }
137 
138 public function getKeys()
139 {
140 return array_keys($this->items);
141 }
142 
143 public function toArray()
144 {
145 return $this->items;
146 }
147 
148 public function isEmpty()
149 {
150 return (count($this->items) == 0);
151 }
152 
153 public function removeKey($key)
154 {
155 if (array_key_exists($key, $this->items))
156 {
157 $this->items[$key] = null;
158 unset($this->items[$key]);
159 }
160 }
161 
162 public function getSubCollection($path)
163 {
164 return ToolSuite::getSubCollection($this->items, $path);
165 }
166 
167 public function join(Collection $collection)
168 {
169 $this->items = array_merge($this->items,$collection->toArray());
170 }
171 
172 public function findNearestGreaterThanOrEqual($needleKey)
173 {
174 $min = null;
175 $result = null;
176 
177 foreach ($this->items as $key => $value)
178 {
179 if ($key >= $needleKey)
180 {
181 if (($min === null)||($key <= $min))
182 {
183 $min = $key;
184 $result = $value;
185 }
186 }
187 
188 }
189 
190 return $result;
191 }
192 
193 /**
194 * Sorts the collection according to strategy given in $sortBy argument.
195 * Available strategies can be found in CollectionSortType.
196 *
197 * Sort on nested object attributes can be done using $sortOnAttribute,
198 * Provide an optional path string to identifiy the field to sort on.
199 * Eg. 'prop1/attr2' will be translated to $collectionEntry->getProp1()->getAttr2();
200 *
201 * @param int $sortBy CollectionSortType strategy
202 * @param string $sortOnAttribute Path to field to sort on
203 */
204 public function sort($sortType, $sortOnAttribute = null)
205 {
206 if (($sortType & CollectionSortType::VALUE) == 0)
207 {
208 
209 
210 if ($sortOnAttribute != null)
211 {
212 
213 $cmpFunction = function($a, $b) use ($sortOnAttribute, $sortType)
214 {
215 $comparedA = $a;
216 $comparedB = $a;
217 if ($sortOnAttribute != null)
218 {
219 $comparedA = ToolSuite::getObjectValueFromPath($a, $sortOnAttribute);
220 $comparedB = ToolSuite::getObjectValueFromPath($b, $sortOnAttribute);
221 }
222 
223 if ($sortType & CollectionSortType::CASE_STRING)
224 {
225 if (($sortType & CollectionSortType::DESC) == 0)
226 {
227 strcasecmp($comparedA, $comparedB);
228 }
229 else
230 {
231 -strcasecmp($comparedA, $comparedB);
232 }
233 }
234 else if (($sortType & CollectionSortType::STRING) == 0)
235 {
236 if (($sortType & CollectionSortType::DESC) == 0)
237 {
238 
239 return (int) $comparedA - (int) $comparedB;
240 }
241 else
242 {
243 return -($comparedA - $comparedB);
244 }
245 }
246 else if ($sortType & CollectionSortType::STRING)
247 {
248 if (($sortType & CollectionSortType::DESC) == 0)
249 {
250 return strcmp($comparedA, $comparedB);
251 }
252 else
253 {
254 return -strcmp($comparedA, $comparedB);
255 }
256 }
257 };
258 uasort($this->items, $cmpFunction);
259 }
260 else
261 {
262 if ($sortType & CollectionSortType::NUMERIC)
263 {
264 if ($sortType & CollectionSortType::ASC)
265 {
266 asort($this->items, SORT_NUMERIC);
267 }
268 else
269 {
270 arsort($this->items, SORT_NUMERIC);
271 }
272 }
273 else if ($sortType & CollectionSortType::STRING)
274 {
275 if ($sortType & CollectionSortType::ASC)
276 {
277 asort($this->items, SORT_STRING);
278 }
279 else
280 {
281 arsort($this->items, SORT_STRING);
282 }
283 }
284 else if ($sortType & CollectionSortType::CASE_STRING)
285 {
286 if ($sortType & CollectionSortType::ASC)
287 {
288 asort($this->items, SORT_STRING | SORT_FLAG_CASE);
289 }
290 else
291 {
292 arsort($this->items, SORT_STRING | SORT_FLAG_CASE);
293 }
294 }
295 }
296 }
297 else
298 {
299 if ($sortType & CollectionSortType::NUMERIC)
300 {
301 if ($sortType & CollectionSortType::ASC)
302 {
303 ksort($this->items, SORT_NUMERIC);
304 }
305 else
306 {
307 krsort($this->items, SORT_NUMERIC);
308 }
309 }
310 else if ($sortType & CollectionSortType::STRING)
311 {
312 if ($sortType & CollectionSortType::ASC)
313 {
314 ksort($this->items, SORT_STRING);
315 }
316 else
317 {
318 krsort($this->items, SORT_STRING);
319 }
320 }
321 else if ($sortType & CollectionSortType::CASE_STRING)
322 {
323 if ($sortType & CollectionSortType::ASC)
324 {
325 ksort($this->items, SORT_STRING | SORT_FLAG_CASE);
326 }
327 else
328 {
329 krsort($this->items, SORT_STRING | SORT_FLAG_CASE);
330 }
331 }
332 }
333 }
334 
335 public function reverse($preserveKeys = false)
336 {
337 $this->items = array_reverse($this->items, $preserveKeys);
338 }
339 
340 public function jsonSerialize()
341 {
342 return json_encode($this->toArray());
343 }
344 
345}

Powered by WebSVN 2.2.1