Completed
Push — master ( 187043...48cde4 )
by
unknown
01:30
created

src/Concerns/AppendsAttributesToResults.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Spatie\QueryBuilder\Concerns;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Spatie\QueryBuilder\Exceptions\InvalidAppendQuery;
8
9
trait AppendsAttributesToResults
10
{
11
    /** @var \Illuminate\Support\Collection */
12
    protected $allowedAppends;
13
14
    public function allowedAppends($appends): self
15
    {
16
        $appends = is_array($appends) ? $appends : func_get_args();
17
18
        $this->allowedAppends = collect($appends);
19
20
        $this->ensureAllAppendsExist();
21
22
        return $this;
23
    }
24
25
    protected function addAppendsToResults(Collection $results)
26
    {
27
        if (! $this->ensureAllAppendsExist()) {
28
            return $results;
29
        }
30
31
        return $results->each(function (Model $result) {
32
            return $result->append($this->request->appends()->toArray());
0 ignored issues
show
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
        });
34
    }
35
36
    protected function ensureAllAppendsExist(): bool
37
    {
38
        $appends = $this->request->appends();
39
40
        $diff = $appends->diff($this->allowedAppends);
41
42
        if ($diff->count()) {
43
            if ($this->throwInvalidQueryExceptions) {
0 ignored issues
show
The property throwInvalidQueryExceptions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
                throw InvalidAppendQuery::appendsNotAllowed($diff, $this->allowedAppends);
45
            } else {
46
                return false;
47
            }
48
        }
49
50
        return true;
51
    }
52
}
53