AllowedInclude::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\QueryBuilder;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Spatie\QueryBuilder\Includes\IncludedCount;
8
use Spatie\QueryBuilder\Includes\IncludedRelationship;
9
use Spatie\QueryBuilder\Includes\IncludeInterface;
10
11
class AllowedInclude
12
{
13
    /** @var string */
14
    protected $name;
15
16
    /** @var IncludeInterface */
17
    protected $includeClass;
18
19
    /** @var string|null */
20
    protected $internalName;
21
22
    public function __construct(string $name, IncludeInterface $includeClass, ?string $internalName = null)
23
    {
24
        $this->name = Str::camel($name);
25
        $this->includeClass = $includeClass;
26
        $this->internalName = $internalName ?? $this->name;
27
    }
28
29
    public static function relationship(string $name, ?string $internalName = null): Collection
30
    {
31
        $internalName = Str::camel($internalName ?? $name);
32
33
        return IncludedRelationship::getIndividualRelationshipPathsFromInclude($internalName)
34
            ->zip(IncludedRelationship::getIndividualRelationshipPathsFromInclude($name))
35
            ->flatMap(function ($args): Collection {
36
                [$relationship, $alias] = $args;
0 ignored issues
show
Bug introduced by
The variable $relationship does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $alias does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
37
38
                $includes = collect([
39
                    new self($alias, new IncludedRelationship, $relationship),
40
                ]);
41
42
                if (! Str::contains($relationship, '.')) {
43
                    $suffix = config('query-builder.count_suffix');
44
45
                    $includes = $includes->merge(self::count(
46
                        $alias.$suffix,
47
                        $relationship.$suffix
48
                    ));
49
                }
50
51
                return $includes;
52
            });
53
    }
54
55
    public static function count(string $name, ?string $internalName = null): Collection
56
    {
57
        return collect([
58
            new static($name, new IncludedCount(), $internalName),
59
        ]);
60
    }
61
62
    public function include(QueryBuilder $query): void
63
    {
64
        ($this->includeClass)($query, $this->internalName);
65
    }
66
67
    public function getName(): string
68
    {
69
        return $this->name;
70
    }
71
72
    public function isForInclude(string $includeName): bool
73
    {
74
        return $this->name === $includeName;
75
    }
76
}
77