scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | /* |
||
4 | * This file is part of Fusonic-linq. |
||
5 | * https://github.com/fusonic/fusonic-linq |
||
6 | * |
||
7 | * (c) Fusonic GmbH |
||
8 | * |
||
9 | * For the full copyright and license information, please view the LICENSE |
||
10 | * file that was distributed with this source code. |
||
11 | */ |
||
12 | |||
13 | namespace Fusonic\Linq\Iterator; |
||
14 | |||
15 | use Iterator; |
||
16 | use ArrayIterator; |
||
17 | use Fusonic\Linq; |
||
18 | use Fusonic\Linq\Helper; |
||
19 | |||
20 | class OrderIterator implements Iterator |
||
21 | { |
||
22 | private $iterator; |
||
23 | private $direction; |
||
24 | private $orderedIterator; |
||
25 | private $orderKeyFunc; |
||
26 | |||
27 | public function __construct(Iterator $items, $orderKeyFunc, $direction) |
||
28 | { |
||
29 | $this->iterator = $items; |
||
30 | $this->direction = $direction; |
||
31 | $this->orderKeyFunc = $orderKeyFunc; |
||
32 | } |
||
33 | |||
34 | public function current() |
||
35 | { |
||
36 | return $this->orderedIterator->current(); |
||
37 | } |
||
38 | |||
39 | public function next() |
||
40 | { |
||
41 | $this->orderedIterator->next(); |
||
42 | } |
||
43 | |||
44 | public function key() |
||
45 | { |
||
46 | return $this->orderedIterator->key(); |
||
47 | } |
||
48 | |||
49 | public function valid() |
||
50 | { |
||
51 | return $this->orderedIterator->valid(); |
||
52 | } |
||
53 | |||
54 | public function rewind() |
||
55 | { |
||
56 | if ($this->orderedIterator == null) { |
||
57 | $this->orderItems(); |
||
58 | } |
||
59 | $this->orderedIterator->rewind(); |
||
60 | } |
||
61 | |||
62 | public function orderItems() |
||
63 | { |
||
64 | $orderKeyFunc = $this->orderKeyFunc; |
||
65 | $direction = $this->direction; |
||
66 | |||
67 | $itemIterator = $this->iterator; |
||
68 | $itemIterator->rewind(); |
||
69 | if (!$itemIterator->valid()) { |
||
70 | $this->orderedIterator = new ArrayIterator(); |
||
71 | return; |
||
72 | } |
||
73 | |||
74 | $firstOrderKey = $orderKeyFunc($itemIterator->current()); |
||
75 | |||
76 | $sortType = Helper\LinqHelper::LINQ_ORDER_TYPE_NUMERIC; |
||
77 | |||
78 | if ($firstOrderKey instanceof \DateTime) { |
||
79 | $sortType = Helper\LinqHelper::LINQ_ORDER_TYPE_DATETIME; |
||
80 | } elseif (!is_numeric($firstOrderKey)) { |
||
81 | $sortType = Helper\LinqHelper::LINQ_ORDER_TYPE_ALPHANUMERIC; |
||
82 | } |
||
83 | |||
84 | $keyMap = array(); |
||
85 | $valueMap = array(); |
||
86 | |||
87 | foreach ($itemIterator as $value) { |
||
88 | $orderKey = $orderKeyFunc != null ? $orderKeyFunc($value) : $value; |
||
89 | if ($sortType == Helper\LinqHelper::LINQ_ORDER_TYPE_DATETIME) { |
||
90 | $orderKey = $orderKey->getTimeStamp(); |
||
91 | } |
||
92 | $keyMap[] = $orderKey; |
||
93 | $valueMap[] = $value; |
||
94 | } |
||
95 | |||
96 | if ($sortType == Helper\LinqHelper::LINQ_ORDER_TYPE_DATETIME) { |
||
97 | $sortType = Helper\LinqHelper::LINQ_ORDER_TYPE_NUMERIC; |
||
98 | } |
||
99 | |||
100 | if ($direction == Helper\LinqHelper::LINQ_ORDER_ASC) { |
||
101 | asort($keyMap, $sortType == Helper\LinqHelper::LINQ_ORDER_TYPE_NUMERIC ? SORT_NUMERIC : SORT_LOCALE_STRING); |
||
102 | } else { |
||
103 | arsort($keyMap, $sortType == Helper\LinqHelper::LINQ_ORDER_TYPE_NUMERIC ? SORT_NUMERIC : SORT_LOCALE_STRING); |
||
104 | } |
||
105 | |||
106 | $sorted = new ArrayIterator(array()); |
||
107 | foreach ($keyMap as $key => $value) { |
||
108 | $sorted[] = $valueMap[$key]; |
||
109 | } |
||
110 | |||
111 | $this->orderedIterator = $sorted; |
||
112 | } |
||
113 | } |