QueryHelper::appendExact()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
rs 9.9666
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sarala\Query;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Support\Str;
9
10
class QueryHelper
11
{
12
    private $query;
13
14
    /** @var QueryParamBag */
15
    private $includes;
16
17
    public function __construct($query, QueryParamBag $includes)
18
    {
19
        $this->query = $query;
20
        $this->includes = $includes;
21
    }
22
23
    public function exact($fields): self
24
    {
25
        return $this->appendExact($fields, 'appendQuery');
26
    }
27
28
    public function countExact($fields)
29
    {
30
        return $this->appendExact($fields, 'appendCountQuery');
31
    }
32
33
    private function appendExact($fields, $method)
34
    {
35
        if (is_string($fields)) {
36
            $this->{$method}($fields, $fields);
37
38
            return $this;
39
        }
40
41
        if (! is_array($fields)) {
42
            throw new \InvalidArgumentException(
43
                'Expects a string or an array. '.gettype($fields).' given'
44
            );
45
        }
46
47
        foreach (array_intersect($this->includes->keys(), $fields) as $field) {
48
            $this->{$method}($field, $field);
49
        }
50
51
        return $this;
52
    }
53
54
    public function alias($fields, $value = null): self
55
    {
56
        return $this->appendAlias($fields, $value, 'appendQuery');
57
    }
58
59
    public function countAlias($fields, $value = null)
60
    {
61
        return $this->appendAlias($fields, $value, 'appendCountQuery');
62
    }
63
64
    private function appendAlias($fields, $value, $method): self
65
    {
66
        if (is_array($fields) && is_null($value)) {
67
            foreach ($fields as $field => $value) {
0 ignored issues
show
introduced by
$value is overwriting one of the parameters of this function.
Loading history...
68
                $this->{$method}($field, $value);
69
            }
70
        }
71
72
        if (is_string($fields) && ! is_null($value)) {
73
            $this->{$method}($fields, $value);
74
        }
75
76
        return $this;
77
    }
78
79
    private function appendCountQuery($field, $relationship)
80
    {
81
        if (is_string($relationship)) {
82
            $this->appendQuery($field, function ($query) use ($relationship) {
83
                $relationship = Str::replaceLast('Count', '', Str::camel($relationship));
84
85
                $query->withCount($relationship);
86
            });
87
88
            return;
89
        }
90
91
        throw new \InvalidArgumentException(
92
            'countAlias() method expects second parameter to be a string. '.gettype($relationship).' given'
93
        );
94
    }
95
96
    private function appendQuery($name, $value)
97
    {
98
        if (is_callable($value)) {
99
            $this->query->when($this->includes->has($name), $value);
100
101
            return;
102
        }
103
104
        $this->query->when($this->includes->has($name), function (Builder $query) use ($value) {
105
            $query->with($value);
106
        });
107
    }
108
}
109