Completed
Push — dev ( 845ef5...8eacd8 )
by
unknown
02:50
created

DataCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 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 */
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()
0 ignored issues
show
Documentation introduced by
The doc-type array() could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

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.

Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $filteredData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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(
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->filterHelper->fil...rface::FILTER_LOGIC_OR) of type * is incompatible with the declared type array of property $filteredData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
145 5
                $this->data,
146 5
                    $this->filters,
147 5
                    FilterInterface::FILTER_LOGIC_OR
148
            );
149
        }
150
    }
151
}