Completed
Pull Request — master (#3)
by Denis
01:50 queued 23s
created

EloquentDataProvider::getData()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 5
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Woo\GridView\DataProviders;
4
5
use Illuminate\Database\Eloquent\Builder;
6
7
class EloquentDataProvider extends BaseDataProvider
8
{
9
    protected $query;
10
11
    /**
12
     * EloquentDataProvider constructor.
13
     * @param Builder $query
14
     */
15
    public function __construct(Builder $query)
16
    {
17
        $this->query = clone $query;
18
    }
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function getCount(): int
24
    {
25
        return $this->query->count();
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function getData(array $filters, string $orderBy, string $orderSort, int $page, int $perPage)
32
    {
33
        $query = clone $this->query;
34
35
        foreach ($filters as $field => $value) {
36
            $query->where($field, 'LIKE', '%' . $value . '%');
37
        }
38
39
        if ($orderBy) {
40
            $query->orderBy($orderBy, $orderSort);
41
        }
42
43
        if ($perPage == 0) {
44
            return $query->get();
45
        }
46
47
        return $query->offset(($page - 1) * $perPage)->limit($perPage)->get();
48
    }
49
}