Completed
Pull Request — master (#127)
by De Cramer
04:50
created

DataCollection   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 64.71%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 181
ccs 33
cts 51
cp 0.6471
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getData() 0 7 1
A getLineData() 0 4 1
A getLastPageNumber() 0 7 1
A setFiltersAndSort() 0 11 3
A setDataByIndex() 0 5 1
A setPageSize() 0 6 1
A reset() 0 8 1
A loadData() 0 21 4
A getPageSize() 0 4 1
A getAll() 0 4 1
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 null|array */
29
    protected $sort = null;
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 = null, $sortOrder = "ASC")
101
    {
102 3
        $this->reset();
103
104 3
        $this->filters = $filters;
105 3
        if ($sortField && $sortOrder) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sortField of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
106
            $this->sort = [$sortField, $sortOrder];
107
        }
108
109 3
        return $this;
110
    }
111
112
    /**
113
     * sets new data to line
114
     *
115
     * @param $index
116
     * @param $data
117
     */
118
    public function setDataByIndex($line, $data)
119
    {
120
        $this->data[$line] = $data;
121
        $this->filteredData = null;
122
    }
123
124
    /**
125
     * Set the number of elements to display on each page.
126
     *
127
     * @param int $size Size of each page
128
     *
129
     * @return $this
130
     */
131 5
    public function setPageSize($size)
132
    {
133 5
        $this->pageSize = $size;
134
135 5
        return $this;
136
    }
137
138
    /**
139
     * Reset current filters & sorting.
140
     *
141
     * @return $this
142
     */
143 3
    public function reset()
144
    {
145 3
        $this->filteredData = null;
146 3
        $this->filters = [];
147 3
        $this->sort = null;
148
149 3
        return $this;
150
    }
151
152
    /**
153
     * Filter & sort the data.
154
     */
155 5
    protected function loadData()
156
    {
157 5
        if (is_null($this->filteredData)) {
158 5
            $this->filteredData = $this->filterHelper->filterData(
159 5
                $this->data,
160 5
                $this->filters,
161 5
                FilterInterface::FILTER_LOGIC_OR
162
            );
163
        }
164 5
        if (!is_null($this->sort)) {
165
            $sort = $this->sort;
166
            uasort($this->filteredData, function ($a, $b) use ($sort) {
167
                $comp = (strcmp($a[$sort[0]], $b[$sort[0]]));
168
                if ($sort[1] == "DESC") {
169
                    return -1 * $comp;
170
                } else {
171
                    return $comp;
172
                }
173
            });
174
        }
175 5
    }
176
177
    /**
178
     * @return int
179
     */
180
    public function getPageSize()
181
    {
182
        return $this->pageSize;
183
    }
184
185
186
    /**
187
     * @return array
188
     */
189
    public function getAll()
190
    {
191
        return $this->data;
192
    }
193
194
}
195