-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathSequence.php
More file actions
67 lines (54 loc) · 1.55 KB
/
Sequence.php
File metadata and controls
67 lines (54 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Utopia\Database\Validator;
use Utopia\Database\Database;
use Utopia\Validator;
use Utopia\Validator\Integer;
use Utopia\Validator\Range;
class Sequence extends Validator
{
private string $idAttributeType;
private bool $primary;
public function getDescription(): string
{
return 'Invalid sequence value';
}
/**
* Expression constructor
*/
public function __construct(string $idAttributeType, bool $primary)
{
$this->primary = $primary;
$this->idAttributeType = $idAttributeType;
}
public function isArray(): bool
{
return false;
}
public function getType(): string
{
return self::TYPE_STRING;
}
public function isValid($value): bool
{
if ($this->primary && empty($value)) {
return false;
}
switch ($this->idAttributeType) {
case Database::VAR_ID_MONGO:
return preg_match('/^[a-f0-9]{24}$/i', $value) === 1;
case Database::VAR_ID_INT:
if (gettype($value) !== 'integer') {
return false;
}
$validator = new Integer(loose: true);
if (!$validator->isValid($value)) {
return false;
}
$start = ($this->primary) ? 1 : 0;
$validator = new Range($start, Database::BIG_INT_MAX, Database::VAR_INTEGER);
return $validator->isValid($value);
default:
return false;
}
}
}