Passed
Push — master ( 0d1e3a...a8104a )
by Jonas
11:45
created

BuildsGroupLimitQueries::groupLimit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Staudenmeir\EloquentEagerLimit\Traits;
4
5
trait BuildsGroupLimitQueries
6
{
7
    /**
8
     * The maximum number of records to return per group.
9
     *
10
     * @var array
11
     */
12
    public $groupLimit;
13
14
    /**
15
     * Add a "group limit" clause to the query.
16
     *
17
     * @param int $value
18
     * @param string $column
19
     * @return $this
20
     */
21 116
    public function groupLimit($value, $column)
22
    {
23 116
        if ($value >= 0) {
24 116
            $this->groupLimit = compact('value', 'column');
25
        }
26
27 116
        return $this;
28
    }
29
30
    /**
31
     * Execute the query as a "select" statement.
32
     *
33
     * @param array $columns
34
     * @return \Illuminate\Support\Collection
35
     */
36 128
    public function get($columns = ['*'])
37
    {
38 128
        $items = parent::get($columns);
39
40 128
        if (!$this->groupLimit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->groupLimit of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
41 128
            return $items;
42
        }
43
44 96
        $column = last(explode('.', $this->groupLimit['column']));
45
46 96
        $keys = [
47
            'laravel_row',
48 96
            '@laravel_partition := '.$this->grammar->wrap($column),
49 96
            '@laravel_partition := '.$this->grammar->wrap('pivot_'.$column),
50
        ];
51
52 96
        foreach ($items as $item) {
53 96
            unset($item->{$keys[0]}, $item->{$keys[1]}, $item->{$keys[2]});
54
        }
55
56 96
        return $items;
57
    }
58
}
59