-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathPartialStructure.php
More file actions
63 lines (54 loc) · 1.83 KB
/
Copy pathPartialStructure.php
File metadata and controls
63 lines (54 loc) · 1.83 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
<?php
namespace Utopia\Database\Validator;
use Utopia\Database\Database;
use Utopia\Database\Document;
class PartialStructure extends Structure
{
/**
* Is valid.
*
* Returns true if valid or false if not.
*
* @param mixed $document
* @param array<int, string> $requiredAttributes optional list of required attributes to check
*
* @return bool
*/
public function isValid($document, array $requiredAttributes = []): bool
{
if (!$document instanceof Document) {
$this->message = 'Value must be an instance of Document';
return false;
}
if (empty($this->collection->getId()) || Database::METADATA !== $this->collection->getCollection()) {
$this->message = 'Collection not found';
return false;
}
$keys = [];
$structure = $document->getArrayCopy();
$attributes = \array_merge($this->attributes, $this->collection->getAttribute('attributes', []));
foreach ($attributes as $attribute) {
$name = $attribute['$id'] ?? '';
$keys[$name] = $attribute;
}
/**
* @var array<mixed,mixed> $requiredAttributesMap
*/
$requiredAttributesMap = [];
foreach ($this->attributes as $attribute) {
if ($attribute['required'] === true && in_array($attribute['$id'], $requiredAttributes)) {
$requiredAttributesMap[] = $attribute;
}
}
if (!$this->checkForAllRequiredValues($structure, $requiredAttributesMap, $keys)) {
return false;
}
if (!$this->checkForUnknownAttributes($structure, $keys)) {
return false;
}
if (!$this->checkForInvalidAttributeValues($structure, $keys)) {
return false;
}
return true;
}
}