1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sanderdekroon\Parlant\Builder; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class WhereClause |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
protected $grammar; |
11
|
|
|
|
12
|
4 |
|
public function __construct($grammar) |
13
|
|
|
{ |
14
|
4 |
|
$this->grammar = $grammar; |
15
|
4 |
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Build a where clause from the supplied column, operator and value. |
19
|
|
|
* @param string|array $column |
20
|
|
|
* @param string $operator |
21
|
|
|
* @param string|null $value |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
4 |
|
public function build($column, $operator, $value) |
25
|
|
|
{ |
26
|
|
|
// If the column is an array, we will assume it is an array of key-value pairs |
27
|
|
|
// and can add them each as a where clause. |
28
|
4 |
|
if (is_array($column)) { |
29
|
|
|
return $this->buildArrayOfWheres($column); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// Here we will make some assumptions about the operator. If only 2 values are |
33
|
|
|
// passed to the method, we will assume that the operator is an equals sign |
34
|
|
|
// and keep going. Otherwise, we'll require the operator to be passed in. |
35
|
4 |
|
list($value, $operator) = $this->prepareValueAndOperator( |
36
|
4 |
|
$value, |
37
|
4 |
|
$operator, |
38
|
4 |
|
(func_num_args() == 2 || is_null($value)) //Is this the best solution? |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
// If the given operator is not found in the list of valid operators we will |
42
|
|
|
// assume that the developer is just short-cutting the '=' operators and |
43
|
|
|
// we will set the operators to '=' and set the values appropriately. |
44
|
3 |
|
if ($this->invalidOperator($operator)) { |
45
|
|
|
list($value, $operator) = [$operator, '=']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Lastly we will check if the combination of column and operator |
49
|
|
|
// results in a different column. For example, author != 13 |
50
|
|
|
// would result in author_not_in 13. |
51
|
|
|
// list($column, $operator, $value) = $this->prepareColumnAndOperator($column, $operator); |
52
|
|
|
|
53
|
3 |
|
return [compact('column', 'operator', 'value')]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* When an array was supplied for where's, we'll loop through it and use the member |
58
|
|
|
* arrays as if it were arguments to the build method. |
59
|
|
|
* @param array $wheres |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
protected function buildArrayOfWheres($wheres) |
63
|
|
|
{ |
64
|
|
|
$build = []; |
65
|
|
|
foreach ($wheres as $where) { |
66
|
|
|
list($column, $operator, $value) = $this->extractWhereValuesFromArray($where); |
67
|
|
|
$build = array_merge($build, $this->build($column, $operator, $value)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $build; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* A where does not have to have three arguments. If two are supplied, that's allright. |
75
|
|
|
* This method makes sure no warnings are emitted and the missing values are set to null. |
76
|
|
|
* @param array $array |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
protected function extractWhereValuesFromArray($array) |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
isset($array[0]) ? $array[0] : null, |
83
|
|
|
isset($array[1]) ? $array[1] : null, |
84
|
|
|
isset($array[2]) ? $array[2] : null, |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Prepare the value and operator. If $useDefault is true, return the default operator (=) |
90
|
|
|
* Throws an exception if the operator is not supported with the current grammer. |
91
|
|
|
* @param mixed $value |
92
|
|
|
* @param string $operator |
93
|
|
|
* @param boolean $useDefault |
94
|
|
|
* @throws InvalidArgumentException |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
4 |
|
protected function prepareValueAndOperator($value, $operator, $useDefault = false, $termDefault = false) |
98
|
|
|
{ |
99
|
4 |
|
if ($useDefault) { |
100
|
2 |
|
return [$operator, $termDefault ? 'IN' : '=']; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
if ($this->invalidOperator($operator) && !is_null($value)) { |
104
|
1 |
|
throw new InvalidArgumentException('Illegal operator and value combination.'); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
return [$value, $operator]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Determine if an operator is invalid or unsupported |
112
|
|
|
* @param string $operator |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
4 |
|
protected function invalidOperator($operator) |
116
|
|
|
{ |
117
|
4 |
|
return !in_array($operator, $this->grammar->getOperators()); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|