1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the miBadger package. |
5
|
|
|
* |
6
|
|
|
* @author Michael Webbers <[email protected]> |
7
|
|
|
* @license http://opensource.org/licenses/Apache-2.0 Apache v2 License |
8
|
|
|
*/ |
9
|
|
|
namespace miBadger\Query; |
10
|
|
|
|
11
|
|
|
class QueryCondition implements QueryExpression |
12
|
|
|
{ |
13
|
|
|
/* @var string The lhs of the query condition. */ |
14
|
|
|
private $leftOperand; |
15
|
|
|
|
16
|
|
|
/* @var boolean|string|int|Array The rhs of the query condition. */ |
17
|
|
|
private $rightOperand; |
18
|
|
|
|
19
|
|
|
/* @var string The binary operator of this condition. */ |
20
|
|
|
private $operator; |
21
|
|
|
|
22
|
|
|
/* @var string|Array The binding(s) for the rhs of the condition */ |
23
|
|
|
private $binding; |
24
|
|
|
|
25
|
15 |
|
public function __construct($left, $operator, $right) |
26
|
|
|
{ |
27
|
15 |
|
$this->leftOperand = $left; |
28
|
15 |
|
$this->operator = $operator; |
29
|
15 |
|
$this->binding = null; |
30
|
|
|
|
31
|
|
|
switch ($operator) { |
32
|
15 |
|
case '<': |
33
|
15 |
|
case '>': |
34
|
15 |
|
case '=': |
35
|
9 |
|
case '<>': |
36
|
9 |
|
case 'LIKE': |
37
|
5 |
|
case 'NOT LIKE': |
38
|
4 |
|
case 'IS': |
39
|
4 |
|
case 'IS NOT': |
40
|
12 |
|
$this->rightOperand = $right; |
41
|
12 |
|
break; |
42
|
|
|
|
43
|
3 |
|
case 'NOT IN': |
44
|
3 |
|
case 'IN': |
45
|
3 |
|
if (is_string($right)) { |
46
|
1 |
|
$this->rightOperand = explode(', ', $right); |
47
|
|
|
} else { |
48
|
2 |
|
$this->rightOperand = $right; |
49
|
|
|
} |
50
|
3 |
|
break; |
51
|
|
|
|
52
|
|
|
default: |
53
|
|
|
throw new QueryException(sprintf("Unsupported operator \"%s\"", $operator)); |
54
|
|
|
} |
55
|
15 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
7 |
|
public function getFlattenedConditions() |
61
|
|
|
{ |
62
|
7 |
|
return [$this]; |
63
|
|
|
} |
64
|
|
|
|
65
|
7 |
|
public function bind(Query $query) |
66
|
|
|
{ |
67
|
7 |
|
if (is_array($this->rightOperand)) { |
68
|
1 |
|
$this->binding = $query->addBindings('where', $this->rightOperand); |
69
|
|
|
} else { |
70
|
6 |
|
$this->binding = $query->addBinding('where', $this->rightOperand); |
71
|
|
|
} |
72
|
7 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
15 |
|
public function __toString() |
78
|
|
|
{ |
79
|
15 |
|
if ($this->binding === null) { |
80
|
8 |
|
$rhs = $this->rightOperand; |
81
|
|
|
// throw new QueryException("Currently unbound conditions are not supported!"); |
82
|
|
|
} else { |
83
|
7 |
|
$rhs = $this->binding; |
84
|
|
|
} |
85
|
|
|
|
86
|
15 |
|
if (is_array($rhs)) { |
87
|
3 |
|
$rhs = sprintf('(%s)', implode(', ', $rhs)); |
88
|
|
|
} |
89
|
|
|
|
90
|
15 |
|
return sprintf('%s %s %s', $this->leftOperand, $this->operator, $rhs); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|