Completed
Push — master ( 78beea...a25989 )
by Anton
22s queued 12s
created

ArraySource   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 97.01%

Importance

Changes 0
Metric Value
wmc 27
eloc 63
dl 0
loc 140
ccs 65
cts 67
cp 0.9701
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C applyFilters() 0 52 17
A applyOrders() 0 28 6
A setSource() 0 6 3
A process() 0 18 1
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Grid\Source;
12
13
use Bluz\Grid;
14
use Bluz\Grid\Data;
15
16
/**
17
 * Array Source Adapter for Grid package
18
 *
19
 * @package  Bluz\Grid
20
 * @author   Anton Shevchuk
21
 *
22
 * @method   array getSource()
23
 */
24
class ArraySource extends AbstractSource
25
{
26
    /**
27
     * Set array source
28
     *
29
     * @param  array $source
30
     *
31
     * @return void
32
     * @throws Grid\GridException
33
     */
34 31
    public function setSource($source): void
35
    {
36 31
        if (!\is_array($source) && !($source instanceof \ArrayAccess)) {
0 ignored issues
show
introduced by
The condition is_array($source) is always true.
Loading history...
37 1
            throw new Grid\GridException('Source of `ArraySource` should be array or implement ArrayAccess interface');
38
        }
39 30
        parent::setSource($source);
40 30
    }
41
42
    /**
43
     * {@inheritdoc}
44
     * @throws Grid\GridException
45
     */
46 13
    public function process(int $page, int $limit, array $filters = [], array $orders = []): Data
47
    {
48
        // process filters
49 13
        $this->applyFilters($filters);
50
51
        // process orders
52 13
        $this->applyOrders($orders);
53
54 13
        $data = $this->getSource();
55
56 13
        $total = \count($data);
57
58
        // process pages
59 13
        $data = \array_slice($data, $limit * ($page - 1), $limit);
60
61 13
        $gridData = new Data($data);
62 13
        $gridData->setTotal($total);
63 13
        return $gridData;
64
    }
65
66
    /**
67
     * Apply filters to array
68
     *
69
     * @param  array $settings
70
     *
71
     * @return void
72
     * @throws Grid\GridException
73
     */
74 13
    private function applyFilters(array $settings): void
75
    {
76 13
        $data = $this->getSource();
77 13
        $data = array_filter(
78 13
            $data,
79
            function ($row) use ($settings) {
80 13
                foreach ($settings as $column => $filters) {
81 5
                    foreach ($filters as $filter => $value) {
82
                        // switch statement for filter
83
                        switch ($filter) {
84 5
                            case Grid\Grid::FILTER_EQ:
85 2
                                if ($row[$column] != $value) {
86 2
                                    return false;
87
                                }
88 2
                                break;
89 4
                            case Grid\Grid::FILTER_NE:
90 2
                                if ($row[$column] == $value) {
91 1
                                    return false;
92
                                }
93 2
                                break;
94 2
                            case Grid\Grid::FILTER_GT:
95 1
                                if ($row[$column] <= $value) {
96 1
                                    return false;
97
                                }
98 1
                                break;
99 2
                            case Grid\Grid::FILTER_GE:
100 1
                                if ($row[$column] < $value) {
101
                                    return false;
102
                                }
103 1
                                break;
104 2
                            case Grid\Grid::FILTER_LT:
105 1
                                if ($row[$column] >= $value) {
106 1
                                    return false;
107
                                }
108 1
                                break;
109 2
                            case Grid\Grid::FILTER_LE:
110 1
                                if ($row[$column] > $value) {
111
                                    return false;
112
                                }
113 1
                                break;
114 1
                            case Grid\Grid::FILTER_LIKE:
115 1
                                if (!preg_match('/' . $value . '/', $row[$column])) {
116 1
                                    return false;
117
                                }
118 5
                                break;
119
                        }
120
                    }
121
                }
122 13
                return true;
123 13
            }
124
        );
125 13
        $this->setSource($data);
126 13
    }
127
128
    /**
129
     * Apply order to array
130
     *
131
     * @param  array $settings
132
     *
133
     * @return void
134
     * @throws Grid\GridException
135
     */
136 13
    private function applyOrders(array $settings): void
137
    {
138 13
        $data = $this->getSource();
139
        // Create empty column stack
140 13
        $orders = [];
141 13
        foreach ($settings as $column => $order) {
142 1
            $orders[$column] = [];
143
        }
144
145
        // Obtain a list of columns
146 13
        foreach ($data as $key => $row) {
147 13
            foreach ($settings as $column => $order) {
148 13
                $orders[$column][$key] = $row[$column];
149
            }
150
        }
151
152
        // Prepare array of arguments
153 13
        $funcArgs = [];
154 13
        foreach ($settings as $column => $order) {
155 1
            $funcArgs[] = $orders[$column];
156 1
            $funcArgs[] = ($order === Grid\Grid::ORDER_ASC) ? SORT_ASC : SORT_DESC;
157
        }
158 13
        $funcArgs[] = &$data;
159
160
        // Sort the data with volume descending, edition ascending
161
        // Add $data as the last parameter, to sort by the common key
162 13
        array_multisort(...$funcArgs);
163 13
        $this->setSource($data);
164 13
    }
165
}
166