-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathIndex.php
More file actions
218 lines (191 loc) · 5.94 KB
/
Index.php
File metadata and controls
218 lines (191 loc) · 5.94 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
namespace Utopia\Database\Validator;
use Utopia\Database\Database;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Validator;
use Utopia\Database\Document;
class Index extends Validator
{
protected string $message = 'Invalid index';
protected int $maxLength;
/**
* @var array<Document> $attributes
*/
protected array $attributes;
/**
* @param array<Document> $attributes
* @param int $maxLength
*/
public function __construct(array $attributes, int $maxLength)
{
$this->maxLength = $maxLength;
foreach ($attributes as $attribute) {
$key = \strtolower($attribute->getAttribute('key', $attribute->getAttribute('$id')));
$this->attributes[$key] = $attribute;
}
foreach (Database::getInternalAttributes() as $attribute) {
$key = \strtolower($attribute->getAttribute('$id'));
$this->attributes[$key] = $attribute;
}
}
/**
* Returns validator description
* @return string
*/
public function getDescription(): string
{
return $this->message;
}
/**
* @param Document $index
* @return bool
*/
public function checkAttributesNotFound(Document $index): bool
{
foreach ($index->getAttribute('attributes', []) as $attribute) {
if (!isset($this->attributes[\strtolower($attribute)])) {
$this->message = 'Invalid index attribute "' . $attribute . '" not found';
return false;
}
}
return true;
}
/**
* @param Document $index
* @return bool
*/
public function checkEmptyIndexAttributes(Document $index): bool
{
if (empty($index->getAttribute('attributes', []))) {
$this->message = 'No attributes provided for index';
return false;
}
return true;
}
/**
* @param Document $index
* @return bool
*/
public function checkDuplicatedAttributes(Document $index): bool
{
$attributes = $index->getAttribute('attributes', []);
$orders = $index->getAttribute('orders', []);
$stack = [];
foreach ($attributes as $key => $attribute) {
$direction = $orders[$key] ?? 'ASC';
$value = \strtolower($attribute . '|' . $direction);
if (\in_array($value, $stack)) {
$this->message = 'Duplicate attributes provided';
return false;
}
$stack[] = $value;
}
return true;
}
/**
* @param Document $index
* @return bool
* @throws DatabaseException
*/
public function checkFulltextIndexNonString(Document $index): bool
{
if ($index->getAttribute('type') === Database::INDEX_FULLTEXT) {
foreach ($index->getAttribute('attributes', []) as $attribute) {
$attribute = $this->attributes[\strtolower($attribute)] ?? new Document();
if ($attribute->getAttribute('type', '') !== Database::VAR_STRING) {
$this->message = 'Attribute "' . $attribute->getAttribute('key', $attribute->getAttribute('$id')) . '" cannot be part of a FULLTEXT index, must be of type string';
return false;
}
}
}
return true;
}
/**
* @param Document $index
* @return bool
*/
public function checkIndexLength(Document $index): bool
{
if ($index->getAttribute('type') === Database::INDEX_FULLTEXT) {
return true;
}
$total = 0;
$lengths = $index->getAttribute('lengths', []);
foreach ($index->getAttribute('attributes', []) as $attributePosition => $attributeName) {
$attribute = $this->attributes[\strtolower($attributeName)];
switch ($attribute->getAttribute('type')) {
case Database::VAR_STRING:
$attributeSize = $attribute->getAttribute('size', 0);
$indexLength = $lengths[$attributePosition] ?? $attributeSize;
break;
case Database::VAR_FLOAT:
$attributeSize = 2; // 8 bytes / 4 mb4
$indexLength = 2;
break;
default:
$attributeSize = 1; // 4 bytes / 4 mb4
$indexLength = 1;
break;
}
if ($indexLength > $attributeSize) {
$this->message = 'Index length ' . $indexLength . ' is larger than the size for ' . $attributeName . ': ' . $attributeSize . '"';
return false;
}
$total += $indexLength;
}
if ($total > $this->maxLength && $this->maxLength > 0) {
$this->message = 'Index length is longer than the maximum: ' . $this->maxLength;
return false;
}
return true;
}
/**
* Is valid.
*
* Returns true index if valid.
* @param Document $value
* @return bool
* @throws DatabaseException
*/
public function isValid($value): bool
{
if (!$this->checkAttributesNotFound($value)) {
return false;
}
if (!$this->checkEmptyIndexAttributes($value)) {
return false;
}
if (!$this->checkDuplicatedAttributes($value)) {
return false;
}
if (!$this->checkFulltextIndexNonString($value)) {
return false;
}
if (!$this->checkIndexLength($value)) {
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_OBJECT;
}
}