-
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.5 KB
/
Sequence.php
File metadata and controls
67 lines (54 loc) · 1.5 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\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;
}
if ($value === null) {
return true;
}
if (!\is_string($value) && !\is_int($value)) {
return false;
}
if (!$this->primary) {
return true;
}
switch ($this->idAttributeType) {
case Database::VAR_UUID7:
return \is_string($value) && preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-7[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i', $value) === 1;
case Database::VAR_INTEGER:
$validator = new Range(1, Database::MAX_BIG_INT, Database::VAR_INTEGER);
return $validator->isValid($value);
default:
return false;
}
}
}