Completed
Pull Request — master (#388)
by Anton
05:43
created

ArraySource::setSource()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Bluz\Grid\Source;
13
14
use Bluz\Grid;
15
16
/**
17
 * Array Source Adapter for Grid package
18
 *
19
 * @package  Bluz\Grid
20
 * @author   Anton Shevchuk
21
 */
22
class ArraySource extends AbstractSource
23
{
24
    /**
25
     * Set array source
26
     *
27
     * @param  array $source
28
     * @return self
29
     * @throws Grid\GridException
30
     */
31 33
    public function setSource($source)
32
    {
33 33
        if (!is_array($source) && !($source instanceof \ArrayAccess)) {
34 1
            throw new Grid\GridException("Source of `ArraySource` should be array or implement ArrayAccess interface");
35
        }
36
37 32
        $this->source = $source;
38 32
        return $this;
39
    }
40
41
    /**
42
     * Process
43
     *
44
     * @param  array $settings
45
     * @return \Bluz\Grid\Data
46
     */
47 13
    public function process(array $settings = [])
48
    {
49 13
        $data = $this->source;
50
51
        // process filters
52 13
        if (!empty($settings['filters'])) {
53 5
            $data = array_filter(
54
                $data,
55 5
                function ($row) use ($settings) {
56 5
                    foreach ($settings['filters'] as $column => $filters) {
57 5
                        foreach ($filters as $filter => $value) {
58
                            // switch statement for filter
59
                            switch ($filter) {
60 5
                                case Grid\Grid::FILTER_EQ:
61 2
                                    if ($row[$column] != $value) {
62 2
                                        return false;
63
                                    }
64 2
                                    break;
65 4
                                case Grid\Grid::FILTER_NE:
66 2
                                    if ($row[$column] == $value) {
67 1
                                        return false;
68
                                    }
69 2
                                    break;
70 2
                                case Grid\Grid::FILTER_GT:
71 1
                                    if ($row[$column] <= $value) {
72 1
                                        return false;
73
                                    }
74 1
                                    break;
75 2
                                case Grid\Grid::FILTER_GE:
76 1
                                    if ($row[$column] < $value) {
77
                                        return false;
78
                                    }
79 1
                                    break;
80 2
                                case Grid\Grid::FILTER_LT:
81 1
                                    if ($row[$column] >= $value) {
82 1
                                        return false;
83
                                    }
84 1
                                    break;
85 2
                                case Grid\Grid::FILTER_LE:
86 1
                                    if ($row[$column] > $value) {
87
                                        return false;
88
                                    }
89 1
                                    break;
90 1
                                case Grid\Grid::FILTER_LIKE:
91 1
                                    if (!preg_match('/'.$value.'/', $row[$column])) {
92 1
                                        return false;
93
                                    }
94 5
                                    break;
95
                            }
96
                        }
97
                    }
98 5
                    return true;
99 5
                }
100
            );
101
        }
102
103
        // process orders
104 13
        if (!empty($settings['orders'])) {
105
            // Create empty column stack
106
            $orders = [];
107
            foreach ($settings['orders'] as $column => $order) {
108
                $orders[$column] = [];
109
            }
110
111
            // Obtain a list of columns
112
            foreach ($data as $key => $row) {
113
                foreach ($settings['orders'] as $column => $order) {
114
                    $orders[$column][$key] = $row[$column];
115
                }
116
            }
117
            // Prepare array of arguments
118
            $funcArgs = [];
119
            foreach ($settings['orders'] as $column => $order) {
120
                $funcArgs[] = $orders[$column];
121
                $funcArgs[] = ($order == Grid\Grid::ORDER_ASC) ? SORT_ASC : SORT_DESC;
122
            }
123
            $funcArgs[] = & $data;
124
125
            // Sort the data with volume descending, edition ascending
126
            // Add $data as the last parameter, to sort by the common key
127
            array_multisort(...$funcArgs);
128
        }
129
130 13
        $total = sizeof($data);
131
132
        // process pages
133 13
        $data = array_slice($data, ($settings['limit'] * ($settings['page'] - 1)), $settings['limit']);
134
135 13
        $gridData = new Grid\Data($data);
136 13
        $gridData->setTotal($total);
137 13
        return $gridData;
138
    }
139
}
140