Completed
Pull Request — master (#124)
by
unknown
02:35
created

DataCollection::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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|null */
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
     * sets new data to line
112
     *
113
     * @param $index
114
     * @param $data
115
     */
116
    public function setDataByIndex($line, $data)
117
    {
118
        $this->data[$line] = $data;
119
        $this->filteredData = null;
120
    }
121
122
    /**
123
     * Set the number of elements to display on each page.
124
     *
125
     * @param int $size Size of each page
126
     *
127
     * @return $this
128
     */
129 5
    public function setPageSize($size)
130
    {
131 5
        $this->pageSize = $size;
132
133 5
        return $this;
134
    }
135
136
    /**
137
     * Reset current filters & sorting.
138
     *
139
     * @return $this
140
     */
141 3
    public function reset()
142
    {
143 3
        $this->filteredData = null;
144 3
        $this->filters = [];
145 3
        $this->sort = [];
146
147 3
        return $this;
148
    }
149
150
    /**
151
     * Filter & sort the data.
152
     */
153 5
    protected function loadData()
154
    {
155 5
        if (is_null($this->filteredData)) {
156 5
            $this->filteredData = $this->filterHelper->filterData(
157 5
                $this->data,
158 5
                $this->filters,
159 5
                FilterInterface::FILTER_LOGIC_OR
160
            );
161
        }
162 5
    }
163
164
    /**
165
     * @return int
166
     */
167
    public function getPageSize()
168
    {
169
        return $this->pageSize;
170
    }
171
172
173
    /**
174
     * @return array
175
     */
176
    public function getAll()
177
    {
178
        return $this->data;
179
    }
180
181
182
}
183