Completed
Push — master ( c362d7...55e77b )
by Adam
03:14
created

EloquentDataSource::isEmpty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Boduch\Grid\Source;
4
5
use Boduch\Grid\Column;
6
use Boduch\Grid\Filters\FilterOperator;
7
use Boduch\Grid\Order;
8
9
class EloquentDataSource implements SourceInterface
10
{
11
    /**
12
     * @var \Illuminate\Database\Eloquent\Builder
13
     */
14
    protected $source;
15
16
    /**
17
     * @param \Illuminate\Database\Eloquent\Builder $source
18
     */
19
    public function __construct($source)
20
    {
21
        $this->source = $source;
22
    }
23
24
    /**
25
     * @param Column[] $columns
26
     */
27
    public function applyFilters($columns)
28
    {
29
        foreach ($columns as $column) {
30
            /** @var \Boduch\Grid\Column $column */
31
            if ($column->isFilterable() && !$column->getFilter()->isEmpty()) {
32
                $this->buildQuery($column);
33
            }
34
        }
35
    }
36
37
    /**
38
     * @param int $perPage
39
     * @param int $currentPage
40
     * @param Order $order
41
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
42
     */
43
    public function execute($perPage, $currentPage, Order $order)
44
    {
45
        return $this
46
            ->source
47
            ->when($order->getColumn(), function ($builder) use ($order) {
0 ignored issues
show
Documentation introduced by
$order->getColumn() is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
                return $builder->orderBy($order->getColumn(), $order->getDirection());
49
            })
50
            ->forPage($currentPage, $perPage)
51
            ->get();
52
    }
53
54
    /**
55
     * @return int
56
     */
57
    public function total()
58
    {
59
        return $this->source->count();
60
    }
61
62
    /**
63
     * @param Column $column
64
     */
65
    protected function buildQuery(Column $column)
66
    {
67
        $name = $column->getFilter()->getName();
68
        $input = $column->getFilter()->getInput();
69
70
        if ($column->getFilter()->getOperator() == FilterOperator::OPERATOR_BETWEEN) {
71
            $this->source->whereBetween($name, $input);
72
        } else {
73
            $this->source->where(
74
                $name,
75
                $column->getFilter()->getOperator(),
76
                $this->normalizeValue($input, $column->getFilter()->getOperator())
0 ignored issues
show
Bug introduced by
It seems like $input defined by $column->getFilter()->getInput() on line 68 can also be of type array; however, Boduch\Grid\Source\Eloqu...ource::normalizeValue() does only seem to accept string|array<integer,string>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
77
            );
78
        }
79
    }
80
81
    /**
82
     * @param string|string[] $value
83
     * @param string $operator
84
     * @return mixed
85
     */
86
    protected function normalizeValue($value, $operator)
87
    {
88
        if (is_array($value)) {
89
            $value = array_filter($value);
90
        }
91
92
        if ($operator == FilterOperator::OPERATOR_LIKE || $operator == FilterOperator::OPERATOR_ILIKE) {
93
            $value = str_replace('*', '%', $value);
94
        }
95
96
        return $value;
97
    }
98
}
99