1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Puzzle\QueryBuilder\Conditions\Sets; |
6
|
|
|
|
7
|
|
|
use Puzzle\QueryBuilder\Conditions\AbstractCondition; |
8
|
|
|
use Puzzle\QueryBuilder\Conditions\CompositeCondition; |
9
|
|
|
use Puzzle\QueryBuilder\Escaper; |
10
|
|
|
use Puzzle\QueryBuilder\Condition; |
11
|
|
|
|
12
|
|
|
abstract class AbstractSet extends AbstractCondition implements CompositeCondition |
13
|
|
|
{ |
14
|
|
|
private |
15
|
|
|
$conditions; |
|
|
|
|
16
|
|
|
|
17
|
36 |
|
public function __construct() |
18
|
|
|
{ |
19
|
36 |
|
$this->conditions = array(); |
20
|
36 |
|
} |
21
|
|
|
|
22
|
20 |
|
public function toString(Escaper $escaper): string |
23
|
|
|
{ |
24
|
20 |
|
if($this->isEmpty()) |
25
|
|
|
{ |
26
|
6 |
|
return ''; |
27
|
|
|
} |
28
|
|
|
|
29
|
14 |
|
$condition = $this->buildCompositeCondition(); |
30
|
|
|
|
31
|
14 |
|
return $condition->toString($escaper); |
32
|
|
|
} |
33
|
|
|
|
34
|
32 |
|
public function add(Condition $condition): self |
35
|
|
|
{ |
36
|
32 |
|
$this->conditions[] = $condition; |
37
|
|
|
|
38
|
32 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
36 |
|
public function isEmpty(): bool |
42
|
|
|
{ |
43
|
36 |
|
foreach($this->conditions as $condition) |
44
|
|
|
{ |
45
|
32 |
|
if(! $condition->isEmpty()) |
46
|
|
|
{ |
47
|
20 |
|
return false; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
20 |
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
14 |
|
private function buildCompositeCondition(): Condition |
55
|
|
|
{ |
56
|
14 |
|
$conditions = $this->getNotEmptyConditions(); |
57
|
|
|
|
58
|
14 |
|
$compositeCondition = array_shift($conditions); |
59
|
|
|
|
60
|
14 |
|
foreach($conditions as $condition) |
61
|
|
|
{ |
62
|
12 |
|
$compositeCondition = $this->joinConditions($compositeCondition, $condition); |
63
|
|
|
} |
64
|
|
|
|
65
|
14 |
|
return $compositeCondition; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function getNotEmptyConditions(): array |
69
|
|
|
{ |
70
|
14 |
|
return array_filter($this->conditions, function (Condition $item) { |
71
|
14 |
|
return $item->isEmpty() === false; |
72
|
14 |
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
abstract protected function joinConditions(Condition $leftCondition, Condition $rightCondition): Condition; |
76
|
|
|
} |
77
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.