Skip to content

Commit d9eaa5a

Browse files
committed
Merge branch 'primary-attribute' of github.com:utopia-php/database into primary-attribute-asint
# Conflicts: # src/Database/Validator/Sequence.php
2 parents d63e3c4 + e10670a commit d9eaa5a

12 files changed

Lines changed: 65 additions & 78 deletions

File tree

src/Database/Adapter/SQL.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,6 @@ public function getAttributeWidth(Document $collection): int
10031003
}
10041004

10051005
switch ($attribute['type']) {
1006-
case Database::VAR_SEQUENCE:
10071006
case Database::VAR_ID:
10081007
$total += 8; // BIGINT 8 bytes
10091008
break;
@@ -1587,7 +1586,7 @@ public function getMaxVarcharLength(): int
15871586
*/
15881587
public function getIdAttributeType(): string
15891588
{
1590-
return 'int';
1589+
return Database::VAR_ID_INT;
15911590
}
15921591

15931592
/**

src/Database/Database.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ class Database
4141
public const VAR_BOOLEAN = 'boolean';
4242
public const VAR_DATETIME = 'datetime';
4343
public const VAR_ID = 'id';
44-
public const VAR_SEQUENCE = 'sequence';
44+
45+
// VAR_ID types
46+
public const VAR_ID_INT = '_integer';
47+
public const VAR_ID_MONGO = '_mongo';
4548

4649
public const INT_MAX = 2147483647;
4750
public const BIG_INT_MAX = PHP_INT_MAX;
@@ -168,7 +171,7 @@ class Database
168171
],
169172
[
170173
'$id' => '$sequence',
171-
'type' => self::VAR_SEQUENCE,
174+
'type' => self::VAR_ID,
172175
'size' => 0,
173176
'required' => true,
174177
'signed' => true,

src/Database/Validator/Queries/Documents.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(
4040
$attributes[] = new Document([
4141
'$id' => '$sequence',
4242
'key' => '$sequence',
43-
'type' => Database::VAR_SEQUENCE,
43+
'type' => Database::VAR_ID,
4444
'array' => false,
4545
]);
4646
$attributes[] = new Document([

src/Database/Validator/Query/Filter.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,8 @@ protected function isValidAttributeAndValues(string $attribute, array $values, s
108108
$validator = null;
109109

110110
switch ($attributeType) {
111-
case Database::VAR_SEQUENCE:
112-
$validator = new Sequence($this->idAttributeType, true);
113-
break;
114-
115111
case Database::VAR_ID:
116-
$validator = new Sequence($this->idAttributeType, false);
112+
$validator = new Sequence($this->idAttributeType, $attribute === '$sequence');
117113
break;
118114

119115
case Database::VAR_STRING:

src/Database/Validator/Sequence.php

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ public function isArray(): bool
3333

3434
public function getType(): string
3535
{
36-
if ($this->idAttributeType === 'string') {
37-
return self::TYPE_STRING;
38-
}
39-
40-
return self::TYPE_INTEGER;
36+
return self::TYPE_STRING;
4137
}
4238

4339
public function isValid($value): bool
@@ -46,29 +42,26 @@ public function isValid($value): bool
4642
return false;
4743
}
4844

49-
if ($this->idAttributeType === 'string') {
50-
return preg_match('/^[a-f0-9]{24}$/i', $value) === 1;
51-
} elseif ($this->idAttributeType === 'int') {
45+
switch ($this->idAttributeType) {
46+
case Database::VAR_ID_MONGO:
47+
return preg_match('/^[a-f0-9]{24}$/i', $value) === 1;
5248

53-
if (gettype($value) !== 'integer') {
54-
return false;
55-
}
49+
case Database::VAR_ID_INT:
50+
if (gettype($value) !== 'integer') {
51+
return false;
52+
}
5653

57-
$validator = new Integer();
58-
if (!$validator->isValid($value)) {
59-
return false;
60-
}
54+
$validator = new Integer(loose: true);
55+
if (!$validator->isValid($value)) {
56+
return false;
57+
}
6158

62-
$start = ($this->primary) ? 1 : 0;
59+
$start = ($this->primary) ? 1 : 0;
60+
$validator = new Range($start, Database::BIG_INT_MAX, Database::VAR_INTEGER);
61+
return $validator->isValid($value);
6362

64-
$validator = new Range($start, Database::BIG_INT_MAX, Database::VAR_INTEGER);
65-
if (!$validator->isValid($value)) {
63+
default:
6664
return false;
67-
}
68-
69-
return true;
7065
}
71-
72-
return false;
7366
}
7467
}

src/Database/Validator/Structure.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Structure extends Validator
3232
],
3333
[
3434
'$id' => '$sequence',
35-
'type' => Database::VAR_SEQUENCE,
35+
'type' => Database::VAR_ID,
3636
'size' => 0,
3737
'required' => false,
3838
'signed' => true,
@@ -316,12 +316,8 @@ protected function checkForInvalidAttributeValues(array $structure, array $keys)
316316
$validators = [];
317317

318318
switch ($type) {
319-
case Database::VAR_SEQUENCE:
320-
$validators[] = new Sequence($this->idAttributeType, true);
321-
break;
322-
323319
case Database::VAR_ID:
324-
$validators[] = new Sequence($this->idAttributeType, false);
320+
$validators[] = new Sequence($this->idAttributeType, $attribute['$id'] === '$sequence');
325321
break;
326322

327323
case Database::VAR_STRING:

tests/unit/Validator/DocumentsQueriesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function testValidQueries(): void
126126
$validator = new Documents(
127127
$this->collection['attributes'],
128128
$this->collection['indexes'],
129-
'int'
129+
Database::VAR_ID_INT
130130
);
131131

132132
$queries = [
@@ -164,7 +164,7 @@ public function testInvalidQueries(): void
164164
$validator = new Documents(
165165
$this->collection['attributes'],
166166
$this->collection['indexes'],
167-
'int'
167+
Database::VAR_ID_INT
168168
);
169169

170170
$queries = ['{"method":"notEqual","attribute":"title","values":["Iron Man","Ant Man"]}'];

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, 'int'),
83+
new Filter($attributes, Database::VAR_ID_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, 'int'),
146+
new Filter($attributes, Database::VAR_ID_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, 'int'),
199+
new Filter($attributes, Database::VAR_ID_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, 'int'),
66+
new Filter($attributes, Database::VAR_ID_INT),
6767
new Limit(),
6868
new Offset(),
6969
new Order($attributes)

tests/unit/Validator/Query/FilterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function setUp(): void
4545
]),
4646
];
4747

48-
$this->validator = new Filter($attributes, 'int');
48+
$this->validator = new Filter($attributes, Database::VAR_ID_INT);
4949
}
5050

5151
public function testSuccess(): void

0 commit comments

Comments
 (0)