|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Zing\QueryBuilder; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
10
|
|
|
use Illuminate\Http\Request; |
|
11
|
|
|
use Illuminate\Support\Traits\ForwardsCalls; |
|
12
|
|
|
use Zing\QueryBuilder\Concerns\Pageable; |
|
13
|
|
|
use Zing\QueryBuilder\Concerns\WithFilters; |
|
14
|
|
|
use Zing\QueryBuilder\Concerns\WithSearchable; |
|
15
|
|
|
use Zing\QueryBuilder\Concerns\WithSorts; |
|
16
|
|
|
use Zing\QueryBuilder\Concerns\WithTypedFilter; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @mixin \Illuminate\Database\Eloquent\Builder |
|
20
|
|
|
*/ |
|
21
|
|
|
class QueryBuilder |
|
22
|
|
|
{ |
|
23
|
|
|
use WithFilters; |
|
24
|
|
|
use WithSearchable; |
|
25
|
|
|
use WithSorts; |
|
26
|
|
|
use Pageable; |
|
27
|
|
|
use ForwardsCalls; |
|
28
|
|
|
use WithTypedFilter; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \Illuminate\Http\Request |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $request; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $builder; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $builder |
|
42
|
|
|
* @param \Illuminate\Http\Request $request |
|
43
|
|
|
*/ |
|
44
|
49 |
|
public function __construct($builder, $request) |
|
45
|
|
|
{ |
|
46
|
49 |
|
$this->builder = $builder; |
|
47
|
49 |
|
$this->request = $request; |
|
48
|
49 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation|string $baseQuery |
|
52
|
|
|
*/ |
|
53
|
49 |
|
public static function fromBuilder($baseQuery, Request $request): self |
|
54
|
|
|
{ |
|
55
|
49 |
|
if (is_subclass_of($baseQuery, Model::class)) { |
|
56
|
48 |
|
$baseQuery = forward_static_call([$baseQuery, 'query']); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
49 |
|
return new self($baseQuery, $request); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
41 |
|
public function getBuilder(): Builder |
|
63
|
|
|
{ |
|
64
|
41 |
|
if ($this->builder instanceof Relation) { |
|
65
|
1 |
|
return $this->builder->getQuery(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
40 |
|
return $this->builder; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $name |
|
73
|
|
|
* |
|
74
|
|
|
* @return \Illuminate\Database\Eloquent\HigherOrderBuilderProxy|mixed |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function __get($name) |
|
77
|
|
|
{ |
|
78
|
1 |
|
return $this->builder->{$name}; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $name |
|
83
|
|
|
* @param mixed[] $arguments |
|
84
|
|
|
* |
|
85
|
|
|
* @return $this|mixed |
|
86
|
|
|
*/ |
|
87
|
48 |
|
public function __call($name, $arguments) |
|
88
|
|
|
{ |
|
89
|
48 |
|
$result = $this->forwardCallTo($this->builder, $name, $arguments); |
|
90
|
|
|
|
|
91
|
48 |
|
return $result === $this->builder ? $this : $result; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
public function clone(): self |
|
95
|
|
|
{ |
|
96
|
1 |
|
return clone $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
public function __clone() |
|
100
|
|
|
{ |
|
101
|
2 |
|
$this->builder = clone $this->builder; |
|
102
|
2 |
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|