appwrite/src/Appwrite/Utopia/Database/Validator/Queries/Base.php

74 lines
2.2 KiB
PHP
Raw Normal View History

2022-08-22 14:14:40 +00:00
<?php
namespace Appwrite\Utopia\Database\Validator\Queries;
2022-08-24 18:23:34 +00:00
use Appwrite\Utopia\Database\Validator\Queries;
2022-08-22 19:12:30 +00:00
use Appwrite\Utopia\Database\Validator\Query\Limit;
use Appwrite\Utopia\Database\Validator\Query\Offset;
use Appwrite\Utopia\Database\Validator\Query\Cursor;
use Appwrite\Utopia\Database\Validator\Query\Filter;
use Appwrite\Utopia\Database\Validator\Query\Order;
2022-08-22 14:14:40 +00:00
use Utopia\Config\Config;
use Utopia\Database\Database;
use Utopia\Database\Document;
2022-08-24 18:23:34 +00:00
class Base extends Queries
2022-08-22 14:14:40 +00:00
{
/**
* Expression constructor
*
* @param string $collection
* @param string[] $allowedAttributes
*/
public function __construct(string $collection, array $allowedAttributes)
{
$config = Config::getParam('collections', []);
$collections = array_merge($config['console'], $config['projects'], $config['buckets'], $config['databases']);
2022-08-22 14:14:40 +00:00
// array for constant lookup time
$allowedAttributesLookup = [];
foreach ($allowedAttributes as $attribute) {
$allowedAttributesLookup[$attribute] = true;
}
$attributes = [];
foreach ($collection['attributes'] as $attribute) {
$key = $attribute['$id'];
if (!isset($allowedAttributesLookup[$key])) {
continue;
}
$attributes[] = new Document([
'key' => $key,
'type' => $attribute['type'],
'array' => $attribute['array'],
]);
}
2022-08-22 19:12:30 +00:00
$attributes[] = new Document([
'key' => '$id',
'type' => Database::VAR_STRING,
'array' => false,
]);
$attributes[] = new Document([
2022-08-23 15:01:57 +00:00
'key' => '$createdAt',
2022-08-22 19:12:30 +00:00
'type' => Database::VAR_DATETIME,
'array' => false,
]);
$attributes[] = new Document([
2022-08-23 15:01:57 +00:00
'key' => '$updatedAt',
2022-08-22 19:12:30 +00:00
'type' => Database::VAR_DATETIME,
'array' => false,
]);
$validators = [
new Limit(),
new Offset(),
new Cursor(),
new Filter($attributes),
new Order($attributes),
];
2022-08-24 18:23:34 +00:00
parent::__construct(...$validators);
2022-08-22 14:14:40 +00:00
}
2022-08-22 19:12:30 +00:00
}