|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sarala\Query; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
|
|
9
|
|
|
class QueryHelper |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var Builder $query */ |
|
12
|
|
|
private $query; |
|
13
|
|
|
|
|
14
|
|
|
/** @var QueryParamBag $includes */ |
|
15
|
|
|
private $includes; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Builder $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) { |
|
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_replace_last('Count', '', camel_case($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
|
|
|
|
It seems like you are assigning to a variable which was imported through a
usestatement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope