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\CriterionInterface; |
13
|
|
|
use RDS\Hydrogen\Criteria\Having; |
14
|
|
|
use RDS\Hydrogen\Criteria\HavingGroup; |
15
|
|
|
use RDS\Hydrogen\Criteria\Where; |
16
|
|
|
use RDS\Hydrogen\Criteria\WhereGroup; |
17
|
|
|
use RDS\Hydrogen\Query; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Trait ModeProvider |
21
|
|
|
* @property-read Query|$this $or |
22
|
|
|
* @property-read Query|$this $and |
23
|
|
|
* @mixin Query |
24
|
|
|
*/ |
25
|
|
|
trait ModeProvider |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* - AND if true |
29
|
|
|
* - OR if false |
30
|
|
|
* |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
protected $conjunction = true; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param \Closure|null $group |
37
|
|
|
* @return Query|$this|self |
38
|
|
|
*/ |
39
|
19 |
View Code Duplication |
public function or(\Closure $group = null): self |
|
|
|
|
40
|
|
|
{ |
41
|
19 |
|
$this->conjunction = false; |
42
|
|
|
|
43
|
19 |
|
if ($group !== null) { |
44
|
|
|
$this->add($this->createGroup(__FUNCTION__, $group)); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
19 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \Closure|null $group |
52
|
|
|
* @return Query|$this|self |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function and(\Closure $group = null): self |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->conjunction = true; |
57
|
|
|
|
58
|
|
|
if ($group !== null) { |
59
|
|
|
$this->add($this->createGroup(__FUNCTION__, $group)); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $fn |
67
|
|
|
* @param \Closure $group |
68
|
|
|
* @return CriterionInterface |
69
|
|
|
*/ |
70
|
|
|
private function createGroup(string $fn, \Closure $group): CriterionInterface |
71
|
|
|
{ |
72
|
|
|
$latest = \count($this->criteria) |
|
|
|
|
73
|
|
|
? \get_class(\array_last($this->criteria)) |
74
|
|
|
: null; |
75
|
|
|
|
76
|
|
|
switch($latest) { |
77
|
|
|
case Where::class: |
78
|
|
|
case WhereGroup::class: |
79
|
|
|
return new WhereGroup($this, $group, $this->mode()); |
80
|
|
|
|
81
|
|
|
case Having::class: |
82
|
|
|
case HavingGroup::class: |
83
|
|
|
return new HavingGroup($this, $group, $this->mode()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$error = 'Operator "%s" can be added only after Where or Having clauses, but %s given'; |
87
|
|
|
$given = $latest ? \class_basename($latest) : 'none'; |
88
|
|
|
|
89
|
|
|
throw new \LogicException(\sprintf($error, \strtoupper($fn), $given)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
protected function mode(): bool |
96
|
|
|
{ |
97
|
43 |
|
return \tap($this->conjunction, function (): void { |
98
|
43 |
|
$this->conjunction = true; |
99
|
43 |
|
}); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.