-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathOrder.php
More file actions
74 lines (62 loc) · 1.7 KB
/
Order.php
File metadata and controls
74 lines (62 loc) · 1.7 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
<?php
namespace Utopia\Database\Validator\Query;
use Utopia\Database\Document;
use Utopia\Database\Query;
class Order extends Base
{
/**
* @var array<int|string, mixed>
*/
protected array $schema = [];
/**
* @param array<Document> $attributes
*/
public function __construct(array $attributes = [])
{
foreach ($attributes as $attribute) {
$this->schema[$attribute->getAttribute('key', $attribute->getAttribute('$id'))] = $attribute->getArrayCopy();
}
}
/**
* @param string $attribute
* @return bool
*/
protected function isValidAttribute(string $attribute): bool
{
// Search for attribute in schema
if (!isset($this->schema[$attribute])) {
$this->message = 'Attribute not found in schema: ' . $attribute;
return false;
}
return true;
}
/**
* Is valid.
*
* Returns true if method is ORDER_ASC or ORDER_DESC and attributes are valid
*
* Otherwise, returns false
*
* @param Query $value
* @return bool
*/
public function isValid($value): bool
{
if (!$value instanceof Query) {
return false;
}
$method = $value->getMethod();
$attribute = $value->getAttribute();
if ($method === Query::TYPE_ORDER_ASC || $method === Query::TYPE_ORDER_DESC) {
return $this->isValidAttribute($attribute);
}
if ($method === Query::TYPE_ORDER_RANDOM) {
return true; // orderRandom doesn't need an attribute
}
return false;
}
public function getMethodType(): string
{
return self::METHOD_TYPE_ORDER;
}
}