Completed
Push — master ( ca7ffd...251636 )
by Denis
01:28
created

ArrayDataProvider::processData()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 5
nop 1
dl 0
loc 34
rs 8.1315
c 0
b 0
f 0
1
<?php
2
3
namespace Woo\GridView\DataProviders;
4
5
use Woo\GridView\GridViewRequest;
6
7
class ArrayDataProvider extends BaseDataProvider
8
{
9
    /**
10
     * @var array
11
     */
12
    private $data;
13
14
    public function __construct(array $data)
15
    {
16
        $this->data = $data;
17
    }
18
19
    /**
20
     * @param GridViewRequest $request
21
     * @return array
22
     */
23
    protected function processData(GridViewRequest $request) : array
24
    {
25
        if (empty($this->data)) {
26
            return [];
27
        }
28
29
        $tmp = collect($this->data);
30
31
        if (!empty($request->filters)) {
32
            $tmp->filter(function($item) use ($request) {
33
                foreach ($request->filters as $filterKey => $filterValue) {
34
35
                    if (!isset($item[$filterKey])) {
36
                        return false;
37
                    }
38
39
                    if (strpos($item[$filterKey], $filterValue) === false) {
40
                        return false;
41
                    }
42
                }
43
44
                return true;
45
            });
46
        }
47
48
        if (!empty($request->sortColumn)) {
49
            $tmp = $tmp->sortBy(
50
                $request->sortColumn,
51
                $request->sortOrder == 'DESC' ? SORT_DESC : SORT_ASC
52
            );
53
        }
54
55
        return $tmp;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function getCount(GridViewRequest $request) : int
62
    {
63
        return count($this->processData($request));
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function getData(GridViewRequest $request)
70
    {
71
        return array_splice(
72
            $this->processData($request),
0 ignored issues
show
Bug introduced by
$this->processData($request) cannot be passed to array_splice() as the parameter $input expects a reference.
Loading history...
73
            ($request->page -1) * $request->perPage, $request->perPage
74
        );
75
    }
76
}