|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alchemy\Phraseanet\Predicate; |
|
4
|
|
|
|
|
5
|
|
|
class PredicateBuilder |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @var \SplStack |
|
10
|
|
|
*/ |
|
11
|
|
|
private $predicateStack; |
|
12
|
|
|
|
|
13
|
8 |
|
public function __construct() |
|
14
|
|
|
{ |
|
15
|
8 |
|
$this->predicateStack = new \SplStack(); |
|
16
|
8 |
|
$this->predicateStack->push(new AndPredicate(new NullPredicate())); |
|
17
|
8 |
|
} |
|
18
|
|
|
|
|
19
|
3 |
|
public function where($expression) |
|
20
|
|
|
{ |
|
21
|
3 |
|
if (! $expression instanceof Predicate) { |
|
22
|
3 |
|
$expression = new LiteralPredicate($expression); |
|
23
|
3 |
|
} |
|
24
|
|
|
|
|
25
|
3 |
|
$this->predicateStack = new \SplStack(); |
|
26
|
3 |
|
$this->predicateStack->push(new AndPredicate($expression)); |
|
27
|
|
|
|
|
28
|
3 |
|
return $this; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
4 |
|
public function andWhere($expression) |
|
32
|
|
|
{ |
|
33
|
4 |
|
if (! $expression instanceof Predicate) { |
|
34
|
4 |
|
$expression = new LiteralPredicate($expression); |
|
35
|
4 |
|
} |
|
36
|
|
|
|
|
37
|
4 |
|
if ($this->predicateStack->count() > 1 && ! $this->predicateStack->top() instanceof AndPredicate) { |
|
38
|
1 |
|
$this->startAndGroup(); |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
4 |
|
$predicate = $this->predicateStack->pop()->andPredicate($expression); |
|
42
|
4 |
|
$this->predicateStack->push($predicate); |
|
43
|
|
|
|
|
44
|
4 |
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
4 |
|
public function orWhere($expression) |
|
48
|
|
|
{ |
|
49
|
4 |
|
if (! $expression instanceof Predicate) { |
|
50
|
4 |
|
$expression = new LiteralPredicate($expression); |
|
51
|
4 |
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
if ($this->predicateStack->count() > 1 && ! $this->predicateStack->top() instanceof OrPredicate) { |
|
54
|
1 |
|
$this->startOrGroup(); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
4 |
|
$predicate = $this->predicateStack->pop()->orPredicate($expression); |
|
58
|
4 |
|
$this->predicateStack->push($predicate); |
|
59
|
|
|
|
|
60
|
4 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
public function startAndGroup() |
|
64
|
|
|
{ |
|
65
|
3 |
|
$predicate = new AndPredicate(new NullPredicate()); |
|
66
|
|
|
|
|
67
|
3 |
|
$this->predicateStack->top()->pushPredicate($predicate); |
|
68
|
3 |
|
$this->predicateStack->push($predicate); |
|
69
|
|
|
|
|
70
|
3 |
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
4 |
|
public function startOrGroup() |
|
74
|
|
|
{ |
|
75
|
4 |
|
$predicate = new OrPredicate(new NullPredicate()); |
|
76
|
|
|
|
|
77
|
4 |
|
$this->predicateStack->top()->pushPredicate($predicate); |
|
78
|
4 |
|
$this->predicateStack->push($predicate); |
|
79
|
|
|
|
|
80
|
4 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
5 |
|
public function endGroup() |
|
84
|
|
|
{ |
|
85
|
5 |
|
if ($this->predicateStack->count() <= 1) { |
|
86
|
1 |
|
throw new \BadMethodCallException('Invalid operation: Not in a condition group.'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
$this->predicateStack->pop(); |
|
90
|
|
|
|
|
91
|
4 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function endAllGroups() |
|
95
|
|
|
{ |
|
96
|
|
|
while ($this->predicateStack->count() > 1) { |
|
97
|
|
|
$this->endGroup(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
7 |
|
public function getPredicate() |
|
102
|
|
|
{ |
|
103
|
7 |
|
$predicate = $this->predicateStack->bottom() |
|
104
|
7 |
|
->pruneInstancesOf(NullPredicate::class, true) |
|
105
|
7 |
|
->pruneRedundantComposites(true); |
|
106
|
|
|
|
|
107
|
7 |
|
if ($predicate instanceof CompositePredicate) { |
|
108
|
6 |
|
$predicate = $predicate->pruneInstancesOf(NullPredicate::class, true); |
|
109
|
6 |
|
} |
|
110
|
|
|
|
|
111
|
7 |
|
return $predicate; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|