1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Atlance\HttpDbalFilter\Query\Expression; |
6
|
|
|
|
7
|
|
|
use Atlance\HttpDbalFilter\Query\Builder; |
8
|
|
|
use Webmozart\Assert\Assert; |
9
|
|
|
|
10
|
|
|
final class Condition |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Expression. |
14
|
|
|
*/ |
15
|
|
|
private readonly string $exprMethod; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Table alias in current instance DBAL\QueryBuilder. |
19
|
|
|
*/ |
20
|
|
|
private readonly string $tableAlias; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The column name. Optional. Defaults to the field name. |
24
|
|
|
*/ |
25
|
|
|
private readonly string $columnName; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Is LIKE operator? |
29
|
|
|
*/ |
30
|
|
|
private readonly bool $isLike; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array<int,string|int|float|\Stringable> |
34
|
|
|
*/ |
35
|
|
|
private array $values = []; |
36
|
|
|
|
37
|
71 |
|
public function __construct(string $snakeCaseExprMethod, string $tableAlias, string $columnName) |
38
|
|
|
{ |
39
|
71 |
|
Assert::oneOf($snakeCaseExprMethod, Builder::SUPPORTED_EXPRESSIONS); |
40
|
71 |
|
$this->isLike = \in_array($snakeCaseExprMethod, ['like', 'not_like', 'ilike'], true); |
|
|
|
|
41
|
71 |
|
$exprMethod = lcfirst(str_replace('_', '', ucwords($snakeCaseExprMethod, '_'))); |
42
|
71 |
|
Assert::methodExists(Builder::class, $exprMethod, sprintf('method "%s" not allowed', $exprMethod)); |
43
|
71 |
|
$this->exprMethod = $exprMethod; |
|
|
|
|
44
|
71 |
|
$this->tableAlias = $tableAlias; |
|
|
|
|
45
|
71 |
|
$this->columnName = $columnName; |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
71 |
|
public function getExprMethod(): string |
49
|
|
|
{ |
50
|
71 |
|
return $this->exprMethod; |
51
|
|
|
} |
52
|
|
|
|
53
|
64 |
|
public function getTableAlias(): string |
54
|
|
|
{ |
55
|
64 |
|
return $this->tableAlias; |
56
|
|
|
} |
57
|
|
|
|
58
|
64 |
|
public function getColumnName(): string |
59
|
|
|
{ |
60
|
64 |
|
return $this->columnName; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return array<int,string|int|float|\Stringable> |
65
|
|
|
*/ |
66
|
58 |
|
public function getValues(): array |
67
|
|
|
{ |
68
|
58 |
|
return $this->values; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array<int,string|int|float|\Stringable> $values |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
71 |
|
public function setValues(array $values): self |
77
|
|
|
{ |
78
|
71 |
|
$this->values = $values; |
79
|
|
|
|
80
|
71 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
26 |
|
public function countValues(): int |
84
|
|
|
{ |
85
|
26 |
|
return \count($this->values); |
86
|
|
|
} |
87
|
|
|
|
88
|
57 |
|
public function generateParameter(string | int $i): string |
89
|
|
|
{ |
90
|
57 |
|
return sprintf(':%s_%s_%s', $this->getTableAlias(), $this->getColumnName(), $i); |
91
|
|
|
} |
92
|
|
|
|
93
|
64 |
|
public function getPropertyPath(): string |
94
|
|
|
{ |
95
|
64 |
|
return sprintf('%s.%s', $this->getTableAlias(), $this->getColumnName()); |
96
|
|
|
} |
97
|
|
|
|
98
|
32 |
|
public function isLike(): bool |
99
|
|
|
{ |
100
|
32 |
|
return $this->isLike; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|