ArraySource   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 140
ccs 60
cts 62
cp 0.9677
rs 10
c 0
b 0
f 0
wmc 27

4 Methods

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