-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathKey.php
More file actions
107 lines (90 loc) · 2.27 KB
/
Copy pathKey.php
File metadata and controls
107 lines (90 loc) · 2.27 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace Utopia\Database\Validator;
use Utopia\Database\Database;
use Utopia\Validator;
class Key extends Validator
{
protected string $message;
/**
* Get Description.
*
* Returns validator description
*
* @return string
*/
public function getDescription(): string
{
return $this->message;
}
/**
* Expression constructor
*/
public function __construct(
protected readonly bool $allowInternal = false,
protected readonly int $maxLength = Database::MAX_UID_DEFAULT_LENGTH,
) {
$this->message = 'Parameter must contain at most ' . $this->maxLength . ' chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char';
}
/**
* Is valid.
*
* Returns true if valid or false if not.
*
* @param $value
* @return bool
*/
public function isValid($value): bool
{
if (!\is_string($value)) {
return false;
}
if ($value === '') {
return false;
}
// No leading special characters
$leading = \mb_substr($value, 0, 1);
if ($leading === '_' || $leading === '.' || $leading === '-') {
return false;
}
$isInternal = $leading === '$';
if ($isInternal && !$this->allowInternal) {
return false;
}
if ($isInternal) {
$allowList = [ '$id', '$createdAt', '$updatedAt' ];
// If exact match, no need for any further checks
return \in_array($value, $allowList);
}
// Valid chars: A-Z, a-z, 0-9, underscore, hyphen, period
if (\preg_match('/[^A-Za-z0-9\_\-\.]/', $value)) {
return false;
}
// At most maxLength chars
if (\mb_strlen($value) > $this->maxLength) {
return false;
}
return true;
}
/**
* Is array
*
* Function will return true if object is array.
*
* @return bool
*/
public function isArray(): bool
{
return false;
}
/**
* Get Type
*
* Returns validator type.
*
* @return string
*/
public function getType(): string
{
return self::TYPE_STRING;
}
}