1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Hydrogen package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace RDS\Hydrogen\Query; |
11
|
|
|
|
12
|
|
|
use RDS\Hydrogen\Criteria\Group; |
13
|
|
|
use RDS\Hydrogen\Criteria\Where; |
14
|
|
|
use RDS\Hydrogen\Query; |
15
|
|
|
use RDS\Hydrogen\Query\WhereProvider\WhereBetweenProvider; |
16
|
|
|
use RDS\Hydrogen\Query\WhereProvider\WhereInProvider; |
17
|
|
|
use RDS\Hydrogen\Query\WhereProvider\WhereLikeProvider; |
18
|
|
|
use RDS\Hydrogen\Query\WhereProvider\WhereNullProvider; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Trait WhereProvider |
22
|
|
|
* @property-read Query|$this $or |
23
|
|
|
* @property-read Query|$this $and |
24
|
|
|
* @mixin Query |
25
|
|
|
*/ |
26
|
|
|
trait WhereProvider |
27
|
|
|
{ |
28
|
|
|
use WhereInProvider; |
29
|
|
|
use WhereLikeProvider; |
30
|
|
|
use WhereNullProvider; |
31
|
|
|
use WhereBetweenProvider; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* - AND if true |
35
|
|
|
* - OR if false |
36
|
|
|
* |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
protected $conjunction = true; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param \Closure|null $group |
43
|
|
|
* @return Query|$this|self |
44
|
|
|
*/ |
45
|
17 |
|
public function or(\Closure $group = null): self |
46
|
|
|
{ |
47
|
17 |
|
$this->conjunction = false; |
48
|
|
|
|
49
|
17 |
|
if ($group !== null) { |
50
|
1 |
|
$this->add(new Group($this, $group, $this->mode())); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
17 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param \Closure|null $group |
58
|
|
|
* @return Query|$this|self |
59
|
|
|
*/ |
60
|
1 |
|
public function and(\Closure $group = null): self |
61
|
|
|
{ |
62
|
1 |
|
$this->conjunction = true; |
63
|
|
|
|
64
|
1 |
|
if ($group !== null) { |
65
|
1 |
|
$this->add(new Group($this, $group, $this->mode())); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string|\Closure $field |
73
|
|
|
* @param $valueOrOperator |
74
|
|
|
* @param null $value |
75
|
|
|
* @return Query|$this|self |
76
|
|
|
*/ |
77
|
11 |
|
public function orWhere($field, $valueOrOperator = null, $value = null): self |
78
|
|
|
{ |
79
|
11 |
|
return $this->or->where($field, $valueOrOperator, $value); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string|\Closure $field |
84
|
|
|
* @param $valueOrOperator |
85
|
|
|
* @param null $value |
86
|
|
|
* @return Query|$this|self |
87
|
|
|
*/ |
88
|
25 |
|
public function where($field, $valueOrOperator = null, $value = null): self |
89
|
|
|
{ |
90
|
25 |
|
if (\is_string($field)) { |
91
|
25 |
|
[$operator, $value] = Where::completeMissingParameters($valueOrOperator, $value); |
|
|
|
|
92
|
|
|
|
93
|
25 |
|
return $this->add(new Where($field, $operator, $value, $this->mode())); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
if ($field instanceof \Closure) { |
97
|
1 |
|
return $this->add(new Group($this, $field, $this->mode())); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$error = \vsprintf('Selection set should be a type of string or Closure, but %s given', [ |
101
|
|
|
\studly_case(\gettype($field)), |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
throw new \InvalidArgumentException($error); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
protected function mode(): bool |
112
|
|
|
{ |
113
|
35 |
|
return \tap($this->conjunction, function () { |
114
|
35 |
|
$this->conjunction = true; |
115
|
35 |
|
}); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.