Completed
Push — master ( 6b56a3...d3d796 )
by
unknown
16:05 queued 14:46
created

src/Concerns/AddsIncludesToQuery.php (1 issue)

Labels
Severity

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\Support\Collection;
6
use Illuminate\Support\Str;
7
use Spatie\QueryBuilder\AllowedInclude;
8
use Spatie\QueryBuilder\Exceptions\InvalidIncludeQuery;
9
use Spatie\QueryBuilder\Includes\IncludeInterface;
10
11
trait AddsIncludesToQuery
12
{
13
    /** @var \Illuminate\Support\Collection */
14
    protected $allowedIncludes;
15
16
    public function allowedIncludes($includes): self
17
    {
18
        $includes = is_array($includes) ? $includes : func_get_args();
19
20
        $this->allowedIncludes = collect($includes)
21
            ->reject(function ($include) {
22
                return empty($include);
23
            })
24
            ->flatMap(function ($include): Collection {
25
                if ($include instanceof Collection) {
26
                    return $include;
27
                }
28
29
                if ($include instanceof IncludeInterface) {
30
                    return collect([$include]);
31
                }
32
33
                if (Str::endsWith($include, config('query-builder.count_suffix'))) {
34
                    return AllowedInclude::count($include);
35
                }
36
37
                return AllowedInclude::relationship($include);
38
            })
39
            ->unique(function (AllowedInclude $allowedInclude) {
40
                return $allowedInclude->getName();
41
            });
42
43
        $this->ensureAllIncludesExist();
44
45
        $this->addIncludesToQuery($this->request->includes());
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...
46
47
        return $this;
48
    }
49
50
    protected function addIncludesToQuery(Collection $includes)
51
    {
52
        $includes->each(function ($include) {
53
            $include = $this->findInclude($include);
54
55
            $include->include($this);
56
        });
57
    }
58
59
    protected function findInclude(string $include): ?AllowedInclude
60
    {
61
        return $this->allowedIncludes
62
            ->first(function (AllowedInclude $included) use ($include) {
63
                return $included->isForInclude($include);
64
            });
65
    }
66
67
    protected function ensureAllIncludesExist()
68
    {
69
        $includes = $this->request->includes();
70
71
        $allowedIncludeNames = $this->allowedIncludes->map(function (AllowedInclude $allowedInclude) {
72
            return $allowedInclude->getName();
73
        });
74
75
        $diff = $includes->diff($allowedIncludeNames);
76
77
        if ($diff->count()) {
78
            throw InvalidIncludeQuery::includesNotAllowed($diff, $allowedIncludeNames);
79
        }
80
81
        // TODO: Check for non-existing relationships?
82
    }
83
}
84