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\Http\Request; |
10
|
|
|
use Illuminate\Support\Traits\ForwardsCalls; |
11
|
|
|
use Zing\QueryBuilder\Concerns\Pageable; |
12
|
|
|
use Zing\QueryBuilder\Concerns\WithFilters; |
13
|
|
|
use Zing\QueryBuilder\Concerns\WithSearchable; |
14
|
|
|
use Zing\QueryBuilder\Concerns\WithSorts; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @mixin \Illuminate\Database\Eloquent\Builder |
18
|
|
|
*/ |
19
|
|
|
class QueryBuilder |
20
|
|
|
{ |
21
|
|
|
use WithFilters; |
22
|
|
|
use WithSearchable; |
23
|
|
|
use WithSorts; |
24
|
|
|
use Pageable; |
25
|
|
|
use ForwardsCalls; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Illuminate\Http\Request |
29
|
|
|
*/ |
30
|
|
|
protected $request; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \Illuminate\Database\Eloquent\Builder |
34
|
|
|
*/ |
35
|
|
|
protected $builder; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param \Illuminate\Http\Request $request |
39
|
|
|
*/ |
40
|
40 |
|
public function __construct(Builder $builder, $request) |
41
|
|
|
{ |
42
|
40 |
|
$this->builder = $builder; |
43
|
40 |
|
$this->request = $request; |
44
|
40 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $baseQuery |
48
|
|
|
*/ |
49
|
40 |
|
public static function fromBuilder($baseQuery, Request $request): self |
50
|
|
|
{ |
51
|
40 |
|
if (is_subclass_of($baseQuery, Model::class)) { |
52
|
40 |
|
$baseQuery = forward_static_call([$baseQuery, 'query']); |
53
|
|
|
} |
54
|
|
|
|
55
|
40 |
|
return new self($baseQuery, $request); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $name |
60
|
|
|
* @param mixed[] $arguments |
61
|
|
|
* |
62
|
|
|
* @return $this|mixed |
63
|
|
|
*/ |
64
|
39 |
|
public function __call($name, $arguments) |
65
|
|
|
{ |
66
|
39 |
|
$result = $this->forwardCallTo($this->builder, $name, $arguments); |
67
|
|
|
|
68
|
|
|
/* |
69
|
|
|
* If the forwarded method call is part of a chain we can return $this |
70
|
|
|
* instead of the actual $result to keep the chain going. |
71
|
|
|
*/ |
72
|
39 |
|
if ($result === $this->builder) { |
73
|
3 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
39 |
|
return $result; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|