1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Database\Query\Expression; |
3
|
|
|
|
4
|
|
|
use Wandu\Database\Contracts\ExpressionInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* LogicalExpression = '(' ComparisonExpression (' AND '|' OR ') LogicalExpression ')' | ComparisonExpression |
8
|
|
|
* |
9
|
|
|
* @example `abc` = 30 |
10
|
|
|
* @example (`abc` = 30 AND `foo` = 30 OR `foo` = 40) |
11
|
|
|
*/ |
12
|
|
|
class LogicalExpression implements ExpressionInterface |
13
|
|
|
{ |
14
|
|
|
/** @var array */ |
15
|
|
|
protected $operators = []; |
16
|
|
|
|
17
|
|
|
/** @var \Wandu\Database\Contracts\ExpressionInterface[] */ |
18
|
|
|
protected $expressions = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string|array|callable $name |
22
|
|
|
* @param string $operator |
23
|
|
|
* @param string $value |
24
|
|
|
* @return static |
25
|
|
|
*/ |
26
|
10 |
|
public function where($name, $operator = null, $value = null) |
27
|
|
|
{ |
28
|
10 |
|
return $this->andWhere($name, $operator, $value); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string|array|callable $name |
33
|
|
|
* @param string $operator |
34
|
|
|
* @param string $value |
35
|
|
|
* @return static |
36
|
|
|
*/ |
37
|
10 |
|
public function andWhere($name, $operator = null, $value = null) |
38
|
|
|
{ |
39
|
10 |
|
$this->addWhereQuery('AND', $name, $operator, $value); |
40
|
10 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string|array|callable $name |
45
|
|
|
* @param string $operator |
46
|
|
|
* @param string $value |
47
|
|
|
* @return static |
48
|
|
|
*/ |
49
|
10 |
|
public function orWhere($name, $operator = null, $value = null) |
50
|
|
|
{ |
51
|
10 |
|
$this->addWhereQuery('OR', $name, $operator, $value); |
52
|
10 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
10 |
|
protected function addWhereQuery($logicalOp, $name, $operator = null, $value = null) |
56
|
|
|
{ |
57
|
10 |
|
if (!is_string($name) && is_callable($name)) { |
58
|
1 |
|
$newWhere = new LogicalExpression(); |
59
|
1 |
|
$newWhereResult = call_user_func($name, $newWhere); |
60
|
1 |
|
$this->operators[] = $logicalOp; |
61
|
1 |
|
$this->expressions[] = $newWhereResult ?: $newWhere; |
62
|
10 |
|
} elseif (!is_array($name)) { |
63
|
10 |
|
if (!isset($value)) { |
64
|
8 |
|
$value = $operator; |
65
|
8 |
|
$operator = '='; |
66
|
8 |
|
} |
67
|
10 |
|
$this->operators[] = $logicalOp; |
68
|
10 |
|
$this->expressions[] = new ComparisonExpression($name, $operator, $value); |
69
|
10 |
|
} else { |
70
|
4 |
|
foreach ($name as $column => $value) { |
71
|
4 |
|
if (is_array($value)) { |
72
|
4 |
|
$newWhere = new LogicalExpression(); |
73
|
4 |
|
foreach ($value as $innerOperator => $innerValue) { |
74
|
4 |
|
$newWhere->andWhere($column, $innerOperator, $innerValue); |
|
|
|
|
75
|
4 |
|
} |
76
|
4 |
|
$this->operators[] = $logicalOp; |
77
|
4 |
|
$this->expressions[] = $newWhere; |
78
|
4 |
|
} else { |
79
|
1 |
|
$this->operators[] = $logicalOp; |
80
|
1 |
|
$this->expressions[] = new ComparisonExpression($column, '=', $value); |
81
|
|
|
} |
82
|
4 |
|
} |
83
|
|
|
} |
84
|
10 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
5 |
|
public function toSql() |
90
|
|
|
{ |
91
|
5 |
|
$sql = ''; |
92
|
5 |
|
foreach ($this->expressions as $index => $expression) { |
93
|
5 |
|
if ($index) { |
94
|
2 |
|
$sql .= ' ' . $this->operators[$index] . ' '; |
95
|
2 |
|
} |
96
|
5 |
|
$sql .= $expression->toSql(); |
97
|
5 |
|
} |
98
|
5 |
|
if (count($this->expressions) > 1) { |
99
|
2 |
|
$sql = "(" . $sql . ")"; |
100
|
2 |
|
} |
101
|
5 |
|
return $sql; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function getBindings() |
108
|
|
|
{ |
109
|
11 |
|
return array_reduce($this->expressions, function ($carry, ExpressionInterface $expression) { |
110
|
10 |
|
return array_merge($carry, $expression->getBindings()); |
111
|
11 |
|
}, []); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.