|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Todd Burry <[email protected]> |
|
4
|
|
|
* @copyright 2009-2017 Vanilla Forums Inc. |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Garden\Db; |
|
9
|
|
|
|
|
10
|
|
|
class ArraySet implements \IteratorAggregate, DatasetInterface { |
|
11
|
|
|
use Utils\DatasetTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var array The internal data array. |
|
15
|
|
|
*/ |
|
16
|
|
|
private $data; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var bool Indicates that the data needs to be sorted. |
|
20
|
|
|
*/ |
|
21
|
|
|
private $toSort = false; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Construct a new {@link ArraySet} object. |
|
25
|
|
|
* |
|
26
|
|
|
* @param array|\Traversable $data The initial data in the array. |
|
27
|
|
|
* @param string[] $order The default sort order. Supply this to prevent a manual sort. |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function __construct($data = [], $order = []) { |
|
30
|
4 |
|
$this->setData($data); |
|
31
|
4 |
|
$this->order = $order; |
|
32
|
4 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Sort the internal array. |
|
36
|
|
|
*/ |
|
37
|
2 |
|
protected function sortData() { |
|
38
|
2 |
|
$columns = $this->getOrder(); |
|
39
|
2 |
|
$order = []; |
|
40
|
2 |
|
foreach ($columns as $column) { |
|
41
|
2 |
|
if ($column[0] === '-') { |
|
42
|
2 |
|
$column = substr($column, 1); |
|
43
|
2 |
|
$order[$column] = -1; |
|
44
|
|
|
} else { |
|
45
|
2 |
|
$order[$column] = 1; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$cmp = function ($a, $b) use ($order) { |
|
50
|
2 |
|
foreach ($order as $column => $desc) { |
|
51
|
2 |
|
$r = strnatcmp($a[$column], $b[$column]); |
|
52
|
|
|
|
|
53
|
2 |
|
if ($r !== 0) { |
|
54
|
2 |
|
return $r * $desc; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
return 0; |
|
58
|
2 |
|
}; |
|
59
|
|
|
|
|
60
|
2 |
|
usort($this->data, $cmp); |
|
61
|
2 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the underlying data array. |
|
65
|
|
|
* |
|
66
|
|
|
* @return array Returns the data. |
|
67
|
|
|
*/ |
|
68
|
3 |
|
public function getData() { |
|
69
|
3 |
|
if ($this->toSort && !empty($this->getOrder())) { |
|
70
|
2 |
|
$this->sortData(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
if ($this->getLimit()) { |
|
74
|
1 |
|
return array_slice($this->data, $this->getOffset(), $this->getLimit()); |
|
75
|
|
|
} else { |
|
76
|
2 |
|
return $this->data; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Set the underlying data array. |
|
82
|
|
|
* |
|
83
|
|
|
* @param array|\Traversable $data |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
4 |
|
public function setData($data) { |
|
87
|
4 |
|
if ($data instanceof \Traversable) { |
|
88
|
4 |
|
$this->data = iterator_to_array($data); |
|
89
|
|
|
} else { |
|
90
|
1 |
|
$this->data = $data; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
if (!empty($this->getOrder())) { |
|
94
|
1 |
|
$this->toSort = true; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
4 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
2 |
|
public function setOrder(string ...$columns) { |
|
104
|
2 |
|
if ($columns !== $this->order) { |
|
105
|
2 |
|
$this->toSort = true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
$this->order = $columns; |
|
109
|
2 |
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function count(): int { |
|
116
|
1 |
|
$count = count($this->data); |
|
117
|
1 |
|
$limit = $this->getLimit(); |
|
118
|
1 |
|
$offset = $this->getOffset(); |
|
119
|
|
|
|
|
120
|
1 |
|
$count = $count - $offset; |
|
121
|
1 |
|
if ($limit > 0) { |
|
122
|
1 |
|
$count = min($count, $limit); |
|
123
|
|
|
} |
|
124
|
1 |
|
$count = max($count, 0); |
|
125
|
|
|
|
|
126
|
1 |
|
return $count; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|