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
|
|
|
|
11
|
|
|
class ArraySet implements \IteratorAggregate, DatasetInterface { |
|
|
|
|
12
|
|
|
use DatasetTrait; |
13
|
|
|
|
14
|
|
|
private $data; |
15
|
|
|
|
16
|
|
|
private $toSort = false; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Construct a new {@link ArraySet} object. |
20
|
|
|
* |
21
|
|
|
* @param array|\Traversable $data The initial data in the |
22
|
|
|
*/ |
23
|
4 |
|
public function __construct($data = []) { |
24
|
4 |
|
$this->setData($data); |
25
|
4 |
|
} |
26
|
|
|
|
27
|
2 |
|
protected function sortData() { |
28
|
2 |
|
$columns = $this->getOrder(); |
29
|
2 |
|
$order = []; |
|
|
|
|
30
|
2 |
|
foreach ($columns as $column) { |
31
|
2 |
|
if ($column[0] === '-') { |
32
|
2 |
|
$column = substr($column, 1); |
|
|
|
|
33
|
2 |
|
$order[$column] = -1; |
34
|
|
|
} else { |
35
|
2 |
|
$order[$column] = 1; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
$cmp = function ($a, $b) use ($order) { |
40
|
2 |
|
foreach ($order as $column => $desc) { |
41
|
2 |
|
$r = strnatcmp($a[$column], $b[$column]); |
42
|
|
|
|
43
|
2 |
|
if ($r !== 0) { |
44
|
2 |
|
return $r * $desc; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
return 0; |
48
|
2 |
|
}; |
49
|
|
|
|
50
|
2 |
|
usort($this->data, $cmp); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the underlying data array. |
55
|
|
|
* |
56
|
|
|
* @return array Returns the data. |
57
|
|
|
*/ |
58
|
3 |
|
public function getData() { |
59
|
3 |
|
if ($this->toSort && !empty($this->getOrder())) { |
60
|
2 |
|
$this->sortData(); |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
if ($this->getLimit()) { |
64
|
1 |
|
return array_slice($this->data, $this->getOffset(), $this->getLimit()); |
65
|
|
|
} else { |
66
|
2 |
|
return $this->data; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the underlying data array. |
72
|
|
|
* |
73
|
|
|
* @param array|\Traversable $data |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
4 |
|
public function setData($data) { |
77
|
4 |
|
if ($data instanceof \Traversable) { |
78
|
4 |
|
$this->data = iterator_to_array($data); |
79
|
|
|
} else { |
80
|
1 |
|
$this->data = $data; |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
if (!empty($this->getOrder())) { |
84
|
1 |
|
$this->toSort = true; |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
2 |
|
public function setOrder(...$columns) { |
94
|
2 |
|
if ($columns !== $this->order) { |
95
|
2 |
|
$this->toSort = true; |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
$this->order = $columns; |
99
|
2 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
1 |
|
public function count() { |
106
|
1 |
|
$count = count($this->data); |
|
|
|
|
107
|
1 |
|
$limit = $this->getLimit(); |
|
|
|
|
108
|
1 |
|
$offset = $this->getOffset(); |
109
|
|
|
|
110
|
1 |
|
$count = $count - $offset; |
111
|
1 |
|
if ($limit > 0) { |
112
|
1 |
|
$count = min($count, $limit); |
113
|
|
|
} |
114
|
1 |
|
$count = max($count, 0); |
115
|
|
|
|
116
|
1 |
|
return $count; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|