1 | <?php |
||
14 | class HasByNonDependentSubqueryMacro |
||
15 | { |
||
16 | /** |
||
17 | * @var \Illuminate\Database\Eloquent\Builder |
||
18 | */ |
||
19 | protected $query; |
||
20 | |||
21 | /** |
||
22 | * HasByNonDependentSubqueryMacro constructor. |
||
23 | * |
||
24 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
25 | */ |
||
26 | 24 | public function __construct(Builder $query) |
|
30 | |||
31 | /** |
||
32 | * @param string|string[] $relationMethod |
||
33 | * @param callable[]|null[] $constraints |
||
34 | * @return \Illuminate\Database\Eloquent\Builder |
||
35 | */ |
||
36 | 21 | public function has($relationMethod, ?callable ...$constraints): Builder |
|
40 | |||
41 | /** |
||
42 | * @param string|string[] $relationMethod |
||
43 | * @param callable[]|null[] $constraints |
||
44 | * @return \Illuminate\Database\Eloquent\Builder |
||
45 | */ |
||
46 | 1 | public function orHas($relationMethod, ?callable ...$constraints): Builder |
|
47 | { |
||
48 | 1 | return $this->apply($relationMethod, 'orWhereIn', ...$constraints); |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param string|string[] $relationMethod |
||
53 | * @param callable[]|null[] $constraints |
||
54 | * @return \Illuminate\Database\Eloquent\Builder |
||
55 | */ |
||
56 | 1 | public function doesntHave($relationMethod, ?callable ...$constraints): Builder |
|
57 | { |
||
58 | 1 | return $this->apply($relationMethod, 'whereNotIn', ...$constraints); |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param string|string[] $relationMethod |
||
63 | * @param callable[]|null[] $constraints |
||
64 | * @return \Illuminate\Database\Eloquent\Builder |
||
65 | */ |
||
66 | 1 | public function orDoesntHave($relationMethod, ?callable ...$constraints): Builder |
|
67 | { |
||
68 | 1 | return $this->apply($relationMethod, 'orWhereNotIn', ...$constraints); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Parse nested constraints and iterate them to apply. |
||
73 | * |
||
74 | * @param string|string[] $relationMethod |
||
75 | * @param string $whereInMethod |
||
76 | * @param callable[]|null[] $constraints |
||
77 | * @return \Illuminate\Database\Eloquent\Builder |
||
78 | */ |
||
79 | 24 | protected function apply($relationMethod, string $whereInMethod, ?callable ...$constraints): Builder |
|
80 | { |
||
81 | // Extract dot-chained expressions |
||
82 | 24 | $relationMethods = \is_string($relationMethod) ? \explode('.', $relationMethod) : \array_values($relationMethod); |
|
83 | |||
84 | // Pick the first relation if exists |
||
85 | 24 | if ($currentRelationMethod = \array_shift($relationMethods)) { |
|
86 | 24 | $this->applyForCurrentRelation( |
|
87 | 24 | $currentRelationMethod, |
|
88 | $whereInMethod, |
||
89 | 24 | function (Relation $query) use ($relationMethods, $whereInMethod, $constraints) { |
|
90 | // Apply optional constraints |
||
91 | 21 | if ($currentConstraints = \array_shift($constraints)) { |
|
92 | 5 | $currentConstraints($this->adjustArgumentTypeOfOptionalConstraints($currentConstraints, $query)); |
|
93 | } |
||
94 | // Apply relations nested under |
||
95 | 21 | if ($relationMethods) { |
|
|
|||
96 | 3 | (new static($query->getQuery()))->apply($relationMethods, $whereInMethod, ...$constraints); |
|
97 | } |
||
98 | 24 | } |
|
99 | ); |
||
100 | } |
||
101 | |||
102 | 21 | return $this->query; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * Apply the current relation as a non-dependent subquery. |
||
107 | * |
||
108 | * @param string $relationMethod |
||
109 | * @param string $whereInMethod |
||
110 | * @param null|callable $constraints |
||
111 | */ |
||
112 | 24 | protected function applyForCurrentRelation(string $relationMethod, string $whereInMethod, callable $constraints): void |
|
136 | |||
137 | /** |
||
138 | * From v1.1: |
||
139 | * Relation will be automatically converted to Builder to prevent common mistakes on demand. |
||
140 | * |
||
141 | * @param callable $constraint |
||
142 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
143 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation |
||
144 | */ |
||
145 | 5 | protected function adjustArgumentTypeOfOptionalConstraints(callable $constraint, Relation $relation) |
|
155 | } |
||
156 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.