|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Atlance\HttpDoctrineOrmFilter\Query; |
|
6
|
|
|
|
|
7
|
|
|
use Webmozart\Assert\Assert; |
|
8
|
|
|
|
|
9
|
|
|
final class Field |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* DQL expression. |
|
13
|
|
|
*/ |
|
14
|
|
|
private string $exprMethod; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Snake case DQL expression. |
|
18
|
|
|
*/ |
|
19
|
|
|
private string $snakeCaseExprMethod; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The name of the Entity class. |
|
23
|
|
|
*/ |
|
24
|
|
|
private string $class; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Table alias in current instance ORM\QueryBuilder. |
|
28
|
|
|
*/ |
|
29
|
|
|
private string $tableAlias; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The name of the field in the Entity. |
|
33
|
|
|
*/ |
|
34
|
|
|
private string $fieldName; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The column name. Optional. Defaults to the field name. |
|
38
|
|
|
*/ |
|
39
|
|
|
private string $columnName; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The type name of the mapped field. Can be one of Doctrine's mapping types or a custom mapping type. |
|
43
|
|
|
*/ |
|
44
|
|
|
private string $type; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The database length of the column. Optional. Default value taken from the type. |
|
48
|
|
|
*/ |
|
49
|
|
|
private ?int $length; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Marks the field as the primary key of the entity. Multiple fields of an entity can have the id attribute, |
|
53
|
|
|
* forming a composite key. |
|
54
|
|
|
*/ |
|
55
|
|
|
private ?bool $id; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Whether the column is nullable. Defaults to FALSE. |
|
59
|
|
|
*/ |
|
60
|
|
|
private ?bool $nullable; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* The SQL fragment that is used when generating the DDL for the column. |
|
64
|
|
|
*/ |
|
65
|
|
|
private ?string $columnDefinition; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* The precision of a decimal column. Only valid if the column type is decimal. |
|
69
|
|
|
*/ |
|
70
|
|
|
private ?int $precision; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* The scale of a decimal column. Only valid if the column type is decimal. |
|
74
|
|
|
*/ |
|
75
|
|
|
private ?int $scale; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Whether a unique constraint should be generated for the column. |
|
79
|
|
|
*/ |
|
80
|
|
|
private ?bool $unique; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Is LIKE operator? |
|
84
|
|
|
*/ |
|
85
|
|
|
private bool $isLike; |
|
86
|
|
|
|
|
87
|
|
|
private array $values = []; |
|
88
|
|
|
|
|
89
|
74 |
|
public function __construct(string $snakeCaseExprMethod, string $class, string $tableAlias) |
|
90
|
|
|
{ |
|
91
|
74 |
|
Assert::oneOf($snakeCaseExprMethod, Builder::SUPPORTED_EXPRESSIONS); |
|
92
|
74 |
|
$this->snakeCaseExprMethod = $snakeCaseExprMethod; |
|
93
|
74 |
|
$this->isLike = \in_array($snakeCaseExprMethod, ['like', 'not_like', 'ilike'], true); |
|
94
|
74 |
|
$exprMethod = lcfirst(str_replace('_', '', ucwords($snakeCaseExprMethod, '_'))); |
|
95
|
74 |
|
Assert::methodExists(Builder::class, $exprMethod, sprintf('method "%s" not allowed', $exprMethod)); |
|
96
|
74 |
|
$this->exprMethod = $exprMethod; |
|
97
|
74 |
|
$this->class = $class; |
|
98
|
74 |
|
$this->tableAlias = $tableAlias; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
74 |
|
public function getExprMethod(): string |
|
102
|
|
|
{ |
|
103
|
74 |
|
return $this->exprMethod; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
74 |
|
public function getSnakeCaseExprMethod(): string |
|
107
|
|
|
{ |
|
108
|
74 |
|
return $this->snakeCaseExprMethod; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
42 |
|
public function getClass(): string |
|
112
|
|
|
{ |
|
113
|
42 |
|
return $this->class; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
73 |
|
public function getTableAlias(): string |
|
117
|
|
|
{ |
|
118
|
73 |
|
return $this->tableAlias; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
73 |
|
public function getFieldName(): string |
|
122
|
|
|
{ |
|
123
|
73 |
|
return $this->fieldName; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
66 |
|
public function getColumnName(): string |
|
127
|
|
|
{ |
|
128
|
66 |
|
return $this->columnName; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
58 |
|
public function getValues(): array |
|
132
|
|
|
{ |
|
133
|
58 |
|
return $this->values; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
71 |
|
public function setValues(array $values): self |
|
137
|
|
|
{ |
|
138
|
71 |
|
$this->values = $values; |
|
139
|
|
|
|
|
140
|
71 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
26 |
|
public function countValues(): int |
|
144
|
|
|
{ |
|
145
|
26 |
|
return \count($this->values); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
66 |
|
public function generateParameter(null | string | int $i = null): string |
|
149
|
|
|
{ |
|
150
|
66 |
|
return null === $i |
|
151
|
42 |
|
? sprintf(':%s_%s', $this->getTableAlias(), $this->getColumnName()) |
|
152
|
66 |
|
: sprintf(':%s_%s_%s', $this->getTableAlias(), $this->getColumnName(), $i); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
64 |
|
public function getPropertyPath(bool $isOrm = true): string |
|
156
|
|
|
{ |
|
157
|
64 |
|
return $isOrm |
|
158
|
64 |
|
? sprintf('%s.%s', $this->getTableAlias(), $this->getFieldName()) |
|
159
|
64 |
|
: sprintf('%s.%s', $this->getTableAlias(), $this->getColumnName()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
32 |
|
public function isLike(): bool |
|
163
|
|
|
{ |
|
164
|
32 |
|
return $this->isLike; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
74 |
|
public function initProperties(array $properties): self |
|
168
|
|
|
{ |
|
169
|
|
|
/** |
|
170
|
|
|
* @var string $property |
|
171
|
|
|
* @var mixed $value |
|
172
|
|
|
*/ |
|
173
|
74 |
|
foreach ($properties as $property => $value) { |
|
174
|
74 |
|
if (property_exists($this, $property)) { |
|
175
|
74 |
|
$this->{$property} = $value; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
74 |
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|