1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Puzzle\QueryBuilder\Conditions; |
6
|
|
|
|
7
|
|
|
use Puzzle\QueryBuilder\Escaper; |
8
|
|
|
use Puzzle\QueryBuilder\Type; |
9
|
|
|
|
10
|
|
|
abstract class AbstractComparisonOperatorCondition extends AbstractCondition |
11
|
|
|
{ |
12
|
|
|
protected |
13
|
|
|
$leftOperand, |
|
|
|
|
14
|
|
|
$rightOperand; |
15
|
|
|
|
16
|
158 |
|
public function __construct(Type $leftOperand, $rightOperand) |
17
|
|
|
{ |
18
|
158 |
|
$this->leftOperand = $leftOperand; |
19
|
158 |
|
$this->rightOperand = $rightOperand; |
20
|
158 |
|
} |
21
|
|
|
|
22
|
192 |
|
public function toString(Escaper $escaper): string |
23
|
|
|
{ |
24
|
192 |
|
if($this->isEmpty()) |
25
|
|
|
{ |
26
|
11 |
|
return ''; |
27
|
|
|
} |
28
|
|
|
|
29
|
181 |
|
return sprintf( |
30
|
181 |
|
'%s %s %s', |
31
|
181 |
|
$this->generateFieldOperand($this->leftOperand), |
32
|
181 |
|
$this->getConditionOperator(), |
33
|
181 |
|
$this->generateRightOperand($escaper) |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
181 |
|
private function generateFieldOperand(Type $field): string |
38
|
|
|
{ |
39
|
181 |
|
return $field->getName(); |
40
|
|
|
} |
41
|
|
|
|
42
|
181 |
|
private function generateRightOperand(Escaper $escaper) |
43
|
|
|
{ |
44
|
181 |
|
if($this->rightOperand instanceof Type) |
45
|
|
|
{ |
46
|
36 |
|
return $this->generateFieldOperand($this->rightOperand); |
47
|
|
|
} |
48
|
|
|
|
49
|
145 |
|
return $this->escapeValue($this->rightOperand, $escaper); |
50
|
|
|
} |
51
|
|
|
|
52
|
206 |
|
public function isEmpty(): bool |
53
|
|
|
{ |
54
|
206 |
|
$columnName = $this->leftOperand->getName(); |
55
|
|
|
|
56
|
206 |
|
return empty($columnName); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
abstract protected function getConditionOperator(): string; |
60
|
|
|
|
61
|
145 |
View Code Duplication |
private function escapeValue($value, Escaper $escaper) |
|
|
|
|
62
|
|
|
{ |
63
|
145 |
|
$value = $this->leftOperand->format($value); |
64
|
|
|
|
65
|
145 |
|
if($this->leftOperand->isEscapeRequired()) |
66
|
|
|
{ |
67
|
105 |
|
$value = $escaper->escape($value); |
68
|
|
|
} |
69
|
|
|
|
70
|
145 |
|
return $value; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.