Skip to content

Commit 41e6f76

Browse files
committed
Add $idAttributeType
1 parent c2e3679 commit 41e6f76

8 files changed

Lines changed: 77 additions & 47 deletions

File tree

src/Database/Database.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4354,6 +4354,7 @@ public function updateDocuments(
43544354
$validator = new DocumentsValidator(
43554355
$attributes,
43564356
$indexes,
4357+
$this->adapter->getIdAttributeType(),
43574358
$this->maxQueryValues,
43584359
$this->adapter->getMinDateTime(),
43594360
$this->adapter->getMaxDateTime(),
@@ -5804,6 +5805,7 @@ public function deleteDocuments(
58045805
$validator = new DocumentsValidator(
58055806
$attributes,
58065807
$indexes,
5808+
$this->adapter->getIdAttributeType(),
58075809
$this->maxQueryValues,
58085810
$this->adapter->getMinDateTime(),
58095811
$this->adapter->getMaxDateTime()
@@ -5995,6 +5997,7 @@ public function find(string $collection, array $queries = [], string $forPermiss
59955997
$validator = new DocumentsValidator(
59965998
$attributes,
59975999
$indexes,
6000+
$this->adapter->getIdAttributeType(),
59986001
$this->maxQueryValues,
59996002
$this->adapter->getMinDateTime(),
60006003
$this->adapter->getMaxDateTime(),
@@ -6248,6 +6251,7 @@ public function count(string $collection, array $queries = [], ?int $max = null)
62486251
$validator = new DocumentsValidator(
62496252
$attributes,
62506253
$indexes,
6254+
$this->adapter->getIdAttributeType(),
62516255
$this->maxQueryValues,
62526256
$this->adapter->getMinDateTime(),
62536257
$this->adapter->getMaxDateTime(),
@@ -6298,6 +6302,7 @@ public function sum(string $collection, string $attribute, array $queries = [],
62986302
$validator = new DocumentsValidator(
62996303
$attributes,
63006304
$indexes,
6305+
$this->adapter->getIdAttributeType(),
63016306
$this->maxQueryValues,
63026307
$this->adapter->getMinDateTime(),
63036308
$this->adapter->getMaxDateTime(),

src/Database/Validator/Queries/Documents.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ class Documents extends IndexedQueries
1919
* Expression constructor
2020
*
2121
* @param array<mixed> $attributes
22+
* @param string $idAttributeType
2223
* @param array<mixed> $indexes
2324
* @throws Exception
2425
*/
2526
public function __construct(
2627
array $attributes,
2728
array $indexes,
29+
string $idAttributeType,
2830
int $maxValuesCount = 100,
2931
\DateTime $minAllowedDate = new \DateTime('0000-01-01'),
3032
\DateTime $maxAllowedDate = new \DateTime('9999-12-31'),
@@ -60,6 +62,7 @@ public function __construct(
6062
new Cursor(),
6163
new Filter(
6264
$attributes,
65+
$idAttributeType,
6366
$maxValuesCount,
6467
$minAllowedDate,
6568
$maxAllowedDate,

src/Database/Validator/Query/Filter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class Filter extends Base
2626
* @param \DateTime $maxAllowedDate
2727
*/
2828
public function __construct(
29-
array $attributes = [],
29+
array $attributes,
30+
private readonly string $idAttributeType,
3031
private readonly int $maxValuesCount = 100,
3132
private readonly \DateTime $minAllowedDate = new \DateTime('0000-01-01'),
3233
private readonly \DateTime $maxAllowedDate = new \DateTime('9999-12-31'),
@@ -108,11 +109,11 @@ protected function isValidAttributeAndValues(string $attribute, array $values, s
108109

109110
switch ($attributeType) {
110111
case Database::VAR_SEQUENCE:
111-
$validator = new Sequence('int', true);
112+
$validator = new Sequence($this->idAttributeType, true);
112113
break;
113114

114115
case Database::VAR_ID:
115-
$validator = new Sequence('int', false);
116+
$validator = new Sequence($this->idAttributeType, false);
116117
break;
117118

118119
case Database::VAR_STRING:

tests/unit/Validator/DocumentsQueriesTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ public function setUp(): void
7676
'signed' => false,
7777
'array' => false,
7878
'filters' => [],
79+
]),
80+
new Document([
81+
'$id' => 'id',
82+
'key' => 'id',
83+
'type' => Database::VAR_ID,
84+
'size' => 0,
85+
'required' => false,
86+
'signed' => false,
87+
'array' => false,
88+
'filters' => [],
7989
])
8090
],
8191
'indexes' => [
@@ -113,9 +123,14 @@ public function tearDown(): void
113123
*/
114124
public function testValidQueries(): void
115125
{
116-
$validator = new Documents($this->collection['attributes'], $this->collection['indexes']);
126+
$validator = new Documents(
127+
$this->collection['attributes'],
128+
$this->collection['indexes'],
129+
'int'
130+
);
117131

118132
$queries = [
133+
Query::notEqual('id', '1000000'),
119134
Query::equal('description', ['Best movie ever']),
120135
Query::equal('description', ['']),
121136
Query::equal('is_bool', [false]),
@@ -146,7 +161,11 @@ public function testValidQueries(): void
146161
*/
147162
public function testInvalidQueries(): void
148163
{
149-
$validator = new Documents($this->collection['attributes'], $this->collection['indexes']);
164+
$validator = new Documents(
165+
$this->collection['attributes'],
166+
$this->collection['indexes'],
167+
'int'
168+
);
150169

151170
$queries = ['{"method":"notEqual","attribute":"title","values":["Iron Man","Ant Man"]}'];
152171
$this->assertEquals(false, $validator->isValid($queries));
@@ -167,5 +186,7 @@ public function testInvalidQueries(): void
167186
$queries = [Query::equal('title', [])]; // empty array
168187
$this->assertEquals(false, $validator->isValid($queries));
169188
$this->assertEquals('Invalid query: Equal queries require at least one value.', $validator->getDescription());
189+
190+
170191
}
171192
}

tests/unit/Validator/IndexedQueriesTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function testValid(): void
8080
$indexes,
8181
[
8282
new Cursor(),
83-
new Filter($attributes),
83+
new Filter($attributes, 'int'),
8484
new Limit(),
8585
new Offset(),
8686
new Order($attributes)
@@ -143,7 +143,7 @@ public function testMissingIndex(): void
143143
$indexes,
144144
[
145145
new Cursor(),
146-
new Filter($attributes),
146+
new Filter($attributes, 'int'),
147147
new Limit(),
148148
new Offset(),
149149
new Order($attributes)
@@ -196,7 +196,7 @@ public function testTwoAttributesFulltext(): void
196196
$indexes,
197197
[
198198
new Cursor(),
199-
new Filter($attributes),
199+
new Filter($attributes, 'int'),
200200
new Limit(),
201201
new Offset(),
202202
new Order($attributes)

tests/unit/Validator/QueriesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testValid(): void
6363
$validator = new Queries(
6464
[
6565
new Cursor(),
66-
new Filter($attributes),
66+
new Filter($attributes, 'int'),
6767
new Limit(),
6868
new Offset(),
6969
new Order($attributes)

tests/unit/Validator/Query/FilterTest.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,34 @@ class FilterTest extends TestCase
1818
*/
1919
public function setUp(): void
2020
{
21-
$this->validator = new Filter(
22-
attributes: [
23-
new Document([
24-
'$id' => 'string',
25-
'key' => 'string',
26-
'type' => Database::VAR_STRING,
27-
'array' => false,
28-
]),
29-
new Document([
30-
'$id' => 'string_array',
31-
'key' => 'string_array',
32-
'type' => Database::VAR_STRING,
33-
'array' => true,
34-
]),
35-
new Document([
36-
'$id' => 'integer_array',
37-
'key' => 'integer_array',
38-
'type' => Database::VAR_INTEGER,
39-
'array' => true,
40-
]),
41-
new Document([
42-
'$id' => 'integer',
43-
'key' => 'integer',
44-
'type' => Database::VAR_INTEGER,
45-
'array' => false,
46-
]),
47-
],
48-
);
21+
$attributes = [
22+
new Document([
23+
'$id' => 'string',
24+
'key' => 'string',
25+
'type' => Database::VAR_STRING,
26+
'array' => false,
27+
]),
28+
new Document([
29+
'$id' => 'string_array',
30+
'key' => 'string_array',
31+
'type' => Database::VAR_STRING,
32+
'array' => true,
33+
]),
34+
new Document([
35+
'$id' => 'integer_array',
36+
'key' => 'integer_array',
37+
'type' => Database::VAR_INTEGER,
38+
'array' => true,
39+
]),
40+
new Document([
41+
'$id' => 'integer',
42+
'key' => 'integer',
43+
'type' => Database::VAR_INTEGER,
44+
'array' => false,
45+
]),
46+
];
47+
48+
$this->validator = new Filter($attributes, 'int');
4949
}
5050

5151
public function testSuccess(): void

tests/unit/Validator/QueryTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function tearDown(): void
108108
*/
109109
public function testQuery(): void
110110
{
111-
$validator = new Documents($this->attributes, []);
111+
$validator = new Documents($this->attributes, [], 'int');
112112

113113
$this->assertEquals(true, $validator->isValid([Query::equal('$id', ['Iron Man', 'Ant Man'])]));
114114
$this->assertEquals(true, $validator->isValid([Query::equal('$id', ['Iron Man'])]));
@@ -138,7 +138,7 @@ public function testQuery(): void
138138
*/
139139
public function testAttributeNotFound(): void
140140
{
141-
$validator = new Documents($this->attributes, []);
141+
$validator = new Documents($this->attributes, [], 'int');
142142

143143
$response = $validator->isValid([Query::equal('name', ['Iron Man'])]);
144144
$this->assertEquals(false, $response);
@@ -154,7 +154,7 @@ public function testAttributeNotFound(): void
154154
*/
155155
public function testAttributeWrongType(): void
156156
{
157-
$validator = new Documents($this->attributes, []);
157+
$validator = new Documents($this->attributes, [], 'int');
158158

159159
$response = $validator->isValid([Query::equal('title', [1776])]);
160160
$this->assertEquals(false, $response);
@@ -166,7 +166,7 @@ public function testAttributeWrongType(): void
166166
*/
167167
public function testQueryDate(): void
168168
{
169-
$validator = new Documents($this->attributes, []);
169+
$validator = new Documents($this->attributes, [], 'int');
170170

171171
$response = $validator->isValid([Query::greaterThan('birthDay', '1960-01-01 10:10:10')]);
172172
$this->assertEquals(true, $response);
@@ -177,7 +177,7 @@ public function testQueryDate(): void
177177
*/
178178
public function testQueryLimit(): void
179179
{
180-
$validator = new Documents($this->attributes, []);
180+
$validator = new Documents($this->attributes, [], 'int');
181181

182182
$response = $validator->isValid([Query::limit(25)]);
183183
$this->assertEquals(true, $response);
@@ -191,7 +191,7 @@ public function testQueryLimit(): void
191191
*/
192192
public function testQueryOffset(): void
193193
{
194-
$validator = new Documents($this->attributes, []);
194+
$validator = new Documents($this->attributes, [], 'int');
195195

196196
$response = $validator->isValid([Query::offset(25)]);
197197
$this->assertEquals(true, $response);
@@ -205,7 +205,7 @@ public function testQueryOffset(): void
205205
*/
206206
public function testQueryOrder(): void
207207
{
208-
$validator = new Documents($this->attributes, []);
208+
$validator = new Documents($this->attributes, [], 'int');
209209

210210
$response = $validator->isValid([Query::orderAsc('title')]);
211211
$this->assertEquals(true, $response);
@@ -225,7 +225,7 @@ public function testQueryOrder(): void
225225
*/
226226
public function testQueryCursor(): void
227227
{
228-
$validator = new Documents($this->attributes, []);
228+
$validator = new Documents($this->attributes, [], 'int');
229229

230230
$response = $validator->isValid([Query::cursorAfter(new Document(['$id' => 'asdf']))]);
231231
$this->assertEquals(true, $response);
@@ -255,7 +255,7 @@ public function testQueryGetByType(): void
255255
*/
256256
public function testQueryEmpty(): void
257257
{
258-
$validator = new Documents($this->attributes, []);
258+
$validator = new Documents($this->attributes, [], 'int');
259259

260260
$response = $validator->isValid([Query::equal('title', [''])]);
261261
$this->assertEquals(true, $response);
@@ -284,7 +284,7 @@ public function testQueryEmpty(): void
284284
*/
285285
public function testOrQuery(): void
286286
{
287-
$validator = new Documents($this->attributes, []);
287+
$validator = new Documents($this->attributes, [], 'int');
288288

289289
$this->assertFalse($validator->isValid(
290290
[Query::or(

0 commit comments

Comments
 (0)