-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathIndexDependency.php
More file actions
85 lines (71 loc) · 1.74 KB
/
IndexDependency.php
File metadata and controls
85 lines (71 loc) · 1.74 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
<?php
namespace Utopia\Database\Validator;
use Utopia\Database\Document;
use Utopia\Validator;
class IndexDependency extends Validator
{
protected string $message = "Attribute can't be deleted or renamed because it is used in an index";
protected bool $castIndexSupport;
/**
* @var array<Document>
*/
protected array $indexes;
/**
* @param array<Document> $indexes
* @param bool $castIndexSupport
*/
public function __construct(array $indexes, bool $castIndexSupport)
{
$this->castIndexSupport = $castIndexSupport;
$this->indexes = $indexes;
}
/**
* Returns validator description
*/
public function getDescription(): string
{
return $this->message;
}
/**
* Is valid.
*
* @param Document $value
*/
public function isValid($value): bool
{
if (! $this->castIndexSupport) {
return true;
}
if (! $value->getAttribute('array', false)) {
return true;
}
$key = \strtolower($value->getAttribute('key', $value->getAttribute('$id')));
foreach ($this->indexes as $index) {
$attributes = $index->getAttribute('attributes', []);
foreach ($attributes as $attribute) {
if ($key === \strtolower($attribute)) {
return false;
}
}
}
return true;
}
/**
* Is array
*
* Function will return true if object is array.
*/
public function isArray(): bool
{
return false;
}
/**
* Get Type
*
* Returns validator type.
*/
public function getType(): string
{
return self::TYPE_OBJECT;
}
}