1 | <?php |
||
5 | class PredicateBuilder |
||
6 | { |
||
7 | |||
8 | /** |
||
9 | * @var \SplStack |
||
10 | */ |
||
11 | private $predicateStack; |
||
12 | |||
13 | 8 | public function __construct() |
|
18 | |||
19 | 3 | public function where($expression) |
|
20 | { |
||
21 | 3 | if (! $expression instanceof Predicate) { |
|
22 | 3 | $expression = new LiteralPredicate($expression); |
|
23 | } |
||
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 | } |
||
36 | |||
37 | 4 | if ($this->predicateStack->count() > 1 && ! $this->predicateStack->top() instanceof AndPredicate) { |
|
38 | 1 | $this->startAndGroup(); |
|
39 | } |
||
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 | } |
||
52 | |||
53 | 4 | if ($this->predicateStack->count() > 1 && ! $this->predicateStack->top() instanceof OrPredicate) { |
|
54 | 1 | $this->startOrGroup(); |
|
55 | } |
||
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() |
|
72 | |||
73 | 4 | public function startOrGroup() |
|
82 | |||
83 | 5 | public function endGroup() |
|
93 | |||
94 | public function endAllGroups() |
||
95 | { |
||
96 | while ($this->predicateStack->count() > 1) { |
||
100 | |||
101 | 7 | public function getPredicate() |
|
113 | } |
||
114 |