|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Framework\Core\Model\Gui\Grid; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Framework\Core\Model\Data\FilterInterface; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class DataCollection |
|
10
|
|
|
* |
|
11
|
|
|
* @package eXpansion\Framework\Core\Model\Gui\Grid; |
|
12
|
|
|
* @author oliver de Cramer <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class DataCollection implements DataCollectionInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var array */ |
|
17
|
|
|
protected $data; |
|
18
|
|
|
|
|
19
|
|
|
/** @var array */ |
|
20
|
|
|
protected $filteredData; |
|
21
|
|
|
|
|
22
|
|
|
/** @var FilterInterface */ |
|
23
|
|
|
protected $filterHelper; |
|
24
|
|
|
|
|
25
|
|
|
/** @var array */ |
|
26
|
|
|
protected $filters; |
|
27
|
|
|
|
|
28
|
|
|
/** @var array */ |
|
29
|
|
|
protected $sort = []; |
|
30
|
|
|
|
|
31
|
|
|
/** @var int */ |
|
32
|
|
|
protected $pageSize; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* DataCollection constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $data |
|
38
|
|
|
* @param FilterInterface $filter |
|
39
|
|
|
*/ |
|
40
|
6 |
|
public function __construct($data, FilterInterface $filter) |
|
41
|
|
|
{ |
|
42
|
6 |
|
$this->data = $data; |
|
43
|
6 |
|
$this->filterHelper = $filter; |
|
44
|
6 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the data that needs to be added on a certain page. |
|
48
|
|
|
* |
|
49
|
|
|
* @param int $page The page to get the data for. Pages starts a 1. |
|
50
|
|
|
* |
|
51
|
|
|
* @return array() |
|
|
|
|
|
|
52
|
|
|
*/ |
|
53
|
4 |
|
public function getData($page) |
|
54
|
|
|
{ |
|
55
|
4 |
|
$this->loadData(); |
|
56
|
4 |
|
$start = ($page - 1) * $this->pageSize; |
|
57
|
|
|
|
|
58
|
4 |
|
return array_slice($this->filteredData, $start, $this->pageSize); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Read data on a certain line |
|
63
|
|
|
* |
|
64
|
|
|
* @param mixed $lineData |
|
65
|
|
|
* @param string $key |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getLineData($lineData, $key) |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->filterHelper->getFieldValue($lineData, $key); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the number of the last page. |
|
76
|
|
|
* |
|
77
|
|
|
* @return int |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function getLastPageNumber() |
|
80
|
|
|
{ |
|
81
|
1 |
|
$this->loadData(); |
|
82
|
1 |
|
$count = count($this->filteredData); |
|
83
|
|
|
|
|
84
|
1 |
|
return ceil($count / $this->pageSize); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set filters & sorting to apply to the data. |
|
89
|
|
|
* |
|
90
|
|
|
* @param array $filters List of filters with the fallowing format : |
|
91
|
|
|
* ['key_to_filter'=> ['type_of_filter' , 'wordl"]] |
|
92
|
|
|
* For the possible types of filters check FilterInstance constants. |
|
93
|
|
|
* Example to find a map or author containing the keyword "hello" |
|
94
|
|
|
* ['name'=> ['like', 'hello"], 'author_loin'=> ['like', 'hello"]] |
|
95
|
|
|
* @param string $sortField Field to sort on |
|
96
|
|
|
* @param string $sortOrder Order DESC or ASC. |
|
97
|
|
|
* |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
3 |
|
public function setFiltersAndSort($filters, $sortField, $sortOrder) |
|
101
|
|
|
{ |
|
102
|
3 |
|
$this->reset(); |
|
103
|
|
|
|
|
104
|
3 |
|
$this->filters = $filters; |
|
105
|
3 |
|
$this->sort[$sortField] = $sortOrder; |
|
106
|
|
|
|
|
107
|
3 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set the number of elements to display on each page. |
|
112
|
|
|
* |
|
113
|
|
|
* @param int $size Size of each page |
|
114
|
|
|
* |
|
115
|
|
|
* @return $this |
|
116
|
|
|
*/ |
|
117
|
5 |
|
public function setPageSize($size) |
|
118
|
|
|
{ |
|
119
|
5 |
|
$this->pageSize = $size; |
|
120
|
|
|
|
|
121
|
5 |
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Reset current filters & sorting. |
|
126
|
|
|
* |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
3 |
|
public function reset() |
|
130
|
|
|
{ |
|
131
|
3 |
|
$this->filteredData = null; |
|
|
|
|
|
|
132
|
3 |
|
$this->filters = []; |
|
133
|
3 |
|
$this->sort = []; |
|
134
|
|
|
|
|
135
|
3 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Filter & sort the data. |
|
140
|
|
|
*/ |
|
141
|
5 |
|
protected function loadData() |
|
142
|
|
|
{ |
|
143
|
5 |
|
if (is_null($this->filteredData)) { |
|
144
|
5 |
|
$this->filteredData = $this->filterHelper->filterData( |
|
|
|
|
|
|
145
|
5 |
|
$this->data, |
|
146
|
5 |
|
$this->filters, |
|
147
|
5 |
|
FilterInterface::FILTER_LOGIC_OR |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.