Merge pull request #3708 from appwrite/feat-update-naming-conventions
feat: update naming convention and folder structure
This commit is contained in:
commit
9881c4cc73
11 changed files with 206 additions and 180 deletions
16
composer.lock
generated
16
composer.lock
generated
|
@ -3526,23 +3526,23 @@
|
|||
},
|
||||
{
|
||||
"name": "phpunit/php-code-coverage",
|
||||
"version": "9.2.15",
|
||||
"version": "9.2.16",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/sebastianbergmann/php-code-coverage.git",
|
||||
"reference": "2e9da11878c4202f97915c1cb4bb1ca318a63f5f"
|
||||
"reference": "2593003befdcc10db5e213f9f28814f5aa8ac073"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/2e9da11878c4202f97915c1cb4bb1ca318a63f5f",
|
||||
"reference": "2e9da11878c4202f97915c1cb4bb1ca318a63f5f",
|
||||
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/2593003befdcc10db5e213f9f28814f5aa8ac073",
|
||||
"reference": "2593003befdcc10db5e213f9f28814f5aa8ac073",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-dom": "*",
|
||||
"ext-libxml": "*",
|
||||
"ext-xmlwriter": "*",
|
||||
"nikic/php-parser": "^4.13.0",
|
||||
"nikic/php-parser": "^4.14",
|
||||
"php": ">=7.3",
|
||||
"phpunit/php-file-iterator": "^3.0.3",
|
||||
"phpunit/php-text-template": "^2.0.2",
|
||||
|
@ -3591,7 +3591,7 @@
|
|||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/sebastianbergmann/php-code-coverage/issues",
|
||||
"source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.15"
|
||||
"source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.16"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
|
@ -3599,7 +3599,7 @@
|
|||
"type": "github"
|
||||
}
|
||||
],
|
||||
"time": "2022-03-07T09:28:20+00:00"
|
||||
"time": "2022-08-20T05:26:47+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpunit/php-file-iterator",
|
||||
|
@ -5383,5 +5383,5 @@
|
|||
"platform-overrides": {
|
||||
"php": "8.0"
|
||||
},
|
||||
"plugin-api-version": "2.2.0"
|
||||
"plugin-api-version": "2.3.0"
|
||||
}
|
||||
|
|
|
@ -22,14 +22,15 @@ class Queries extends Validator
|
|||
/**
|
||||
* Queries constructor
|
||||
*
|
||||
* @param Validator $validator used to validate each query
|
||||
* @param Document[] $attributes allowed attributes to be queried
|
||||
* @param Document[] $indexes available for strict query matching
|
||||
* @param bool $strict
|
||||
* @param $validators - a list of validators
|
||||
*/
|
||||
public function __construct(Validator $validator)
|
||||
public function __construct(Limit $limit = null, Offset $offset = null, Order $order = null, Cursor $cursor = null, Filter $filter = null)
|
||||
{
|
||||
$this->validator = $validator;
|
||||
$this->limit = $limit;
|
||||
$this->offset = $offset;
|
||||
$this->order = $order;
|
||||
$this->filter = $filter;
|
||||
$this->cursor = $cursor;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -68,7 +69,19 @@ class Queries extends Validator
|
|||
}
|
||||
}
|
||||
|
||||
if (!$this->validator->isValid($query)) {
|
||||
$method = $query->getMethod();
|
||||
switch ($method) {
|
||||
case Query::TYPE_LIMIT:
|
||||
$validator = $this->limit;
|
||||
case Query::TYPE_OFFSET:
|
||||
$validator = $this->offset;
|
||||
case Query::TYPE_ORDER:
|
||||
$validator = $this->order;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($validator && !$validator->isValid($query)) {
|
||||
$this->message = 'Query not valid: ' . $this->validator->getDescription();
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -55,4 +55,4 @@ class Collection extends QueriesValidator
|
|||
|
||||
parent::__construct(new QueryValidator($attributes), $attributes, $indexes, true);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Utopia\Database\Validator\Queries;
|
||||
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Database\Validator\UID;
|
||||
|
||||
class LimitOffsetCursorQuery extends LimitOffsetQuery
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $message = 'Invalid query';
|
||||
|
||||
/**
|
||||
* Query constructor
|
||||
*
|
||||
* @param int $maxLimit
|
||||
* @param int $maxOffset
|
||||
* @param int $maxValuesCount
|
||||
*/
|
||||
public function __construct(int $maxLimit = 100, int $maxOffset = 5000)
|
||||
{
|
||||
parent::__construct($maxLimit, $maxOffset);
|
||||
}
|
||||
|
||||
protected function isValidCursor($cursor): bool
|
||||
{
|
||||
$validator = new UID();
|
||||
|
||||
if ($validator->isValid($cursor)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->message = 'Invalid cursor: ' . $validator->getDescription();
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is valid.
|
||||
*
|
||||
* Returns true if:
|
||||
* 1. method is limit or offset and values are within range
|
||||
* 2. method is cursorBefore or cursorAfter and value is not null
|
||||
*
|
||||
* Otherwise, returns false
|
||||
*
|
||||
* @param Query $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid($query): bool
|
||||
{
|
||||
// Validate method
|
||||
$method = $query->getMethod();
|
||||
|
||||
if ($method === Query::TYPE_CURSORAFTER || $method === Query::TYPE_CURSORBEFORE) {
|
||||
$cursor = $query->getValue();
|
||||
return $this->isValidCursor($cursor);
|
||||
}
|
||||
|
||||
return parent::isValid($query);
|
||||
}
|
||||
}
|
|
@ -28,4 +28,4 @@ class Users extends Collection
|
|||
{
|
||||
parent::__construct('users', self::ALLOWED_ATTRIBUTES);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Utopia\Database\Validator;
|
||||
|
||||
use Utopia\Database\Validator\UID;
|
||||
use Utopia\Database\Validator\Query as QueryValidator;
|
||||
|
||||
class Query extends QueryValidator
|
||||
{
|
||||
protected function isValidCursor($cursor): bool
|
||||
{
|
||||
$validator = new UID();
|
||||
|
||||
if ($validator->isValid($cursor)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->message = 'Invalid cursor: ' . $validator->getDescription();
|
||||
return false;
|
||||
}
|
||||
}
|
44
src/Appwrite/Utopia/Database/Validator/Query/Cursor.php
Normal file
44
src/Appwrite/Utopia/Database/Validator/Query/Cursor.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Utopia\Database\Validator\Query;
|
||||
|
||||
use Utopia\Validator;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Database\Validator\UID;
|
||||
|
||||
class Cursor extends Validator
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $message = 'Invalid query';
|
||||
|
||||
/**
|
||||
* Is valid.
|
||||
*
|
||||
* Returns true if method is cursorBefore or cursorAfter and value is not null
|
||||
*
|
||||
* Otherwise, returns false
|
||||
*
|
||||
* @param Query $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid($query): bool
|
||||
{
|
||||
// Validate method
|
||||
$method = $query->getMethod();
|
||||
|
||||
if ($method === Query::TYPE_CURSORAFTER || $method === Query::TYPE_CURSORBEFORE) {
|
||||
$cursor = $query->getValue();
|
||||
$validator = new UID();
|
||||
if ($validator->isValid($cursor)) {
|
||||
return true;
|
||||
}
|
||||
$this->message = 'Invalid cursor: ' . $validator->getDescription();
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Utopia\Database\Validator\Queries;
|
||||
namespace Appwrite\Utopia\Database\Validator\Query;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Queries\LimitOffsetCursorQuery;
|
||||
use Utopia\Validator;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Query;
|
||||
|
||||
class LimitOffsetCursorFilterQuery extends LimitOffsetCursorQuery
|
||||
class Filter extends Validator
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
|
@ -21,19 +21,15 @@ class LimitOffsetCursorFilterQuery extends LimitOffsetCursorQuery
|
|||
/**
|
||||
* Query constructor
|
||||
*
|
||||
* @param int $maxLimit
|
||||
* @param int $maxOffset
|
||||
* @param int $maxValuesCount
|
||||
*/
|
||||
public function __construct(int $maxLimit = 100, int $maxOffset = 5000, array $attributes = [], int $maxValuesCount = 100)
|
||||
public function __construct(array $attributes = [], int $maxValuesCount = 100)
|
||||
{
|
||||
foreach ($attributes as $attribute) {
|
||||
$this->schema[$attribute->getAttribute('key')] = $attribute->getArrayCopy();
|
||||
}
|
||||
|
||||
$this->maxValuesCount = $maxValuesCount;
|
||||
|
||||
parent::__construct($maxLimit, $maxOffset);
|
||||
}
|
||||
|
||||
protected function isValidAttribute($attribute): bool
|
||||
|
@ -78,30 +74,10 @@ class LimitOffsetCursorFilterQuery extends LimitOffsetCursorQuery
|
|||
return true;
|
||||
}
|
||||
|
||||
protected function isValidContains(string $attribute, array $values): bool
|
||||
{
|
||||
if (!$this->isValidAttributeAndValues($attribute, $values)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$attributeSchema = $this->schema[$attribute];
|
||||
|
||||
// Contains method only supports array attributes
|
||||
if (!$attributeSchema['array']) {
|
||||
$this->message = 'Query method only supported on array attributes: ' . Query::TYPE_CONTAINS;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is valid.
|
||||
*
|
||||
* Returns true if:
|
||||
* 1. method is limit or offset and values are within range
|
||||
* 2. method is cursorBefore or cursorAfter and value is not null
|
||||
* 3. method is a filter method, attribute exists, and value matches attribute type
|
||||
* Returns true if method is a filter method, attribute exists, and value matches attribute type
|
||||
*
|
||||
* Otherwise, returns false
|
||||
*
|
||||
|
@ -116,9 +92,10 @@ class LimitOffsetCursorFilterQuery extends LimitOffsetCursorQuery
|
|||
$attribute = $query->getAttribute();
|
||||
|
||||
switch ($method) {
|
||||
case Query::TYPE_CONTAINS:
|
||||
$values = $query->getValues();
|
||||
return $this->isValidContains($attribute, $values);
|
||||
// Do we support contains ?
|
||||
// case Query::TYPE_CONTAINS:
|
||||
// $values = $query->getValues();
|
||||
// return $this->isValidContains($attribute, $values);
|
||||
|
||||
case Query::TYPE_EQUAL:
|
||||
case Query::TYPE_NOTEQUAL:
|
||||
|
@ -131,7 +108,7 @@ class LimitOffsetCursorFilterQuery extends LimitOffsetCursorQuery
|
|||
return $this->isValidAttributeAndValues($attribute, $values);
|
||||
|
||||
default:
|
||||
return parent::isValid($query);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
97
src/Appwrite/Utopia/Database/Validator/Query/Limit.php
Normal file
97
src/Appwrite/Utopia/Database/Validator/Query/Limit.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Utopia\Database\Validator\Query;
|
||||
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Validator\Range;
|
||||
use Utopia\Validator;
|
||||
|
||||
class Limit extends Validator
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $message = 'Invalid query';
|
||||
|
||||
protected int $maxLimit;
|
||||
|
||||
/**
|
||||
* Query constructor
|
||||
*
|
||||
* @param int $maxLimit
|
||||
*/
|
||||
public function __construct(int $maxLimit = 100)
|
||||
{
|
||||
$this->maxLimit = $maxLimit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Description.
|
||||
*
|
||||
* Returns validator description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription(): string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
protected function isValidLimit($limit): bool
|
||||
{
|
||||
$validator = new Range(0, $this->maxLimit);
|
||||
if ($validator->isValid($limit)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->message = 'Invalid limit: ' . $validator->getDescription();
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is valid.
|
||||
*
|
||||
* Returns true if method is limit values are within range.
|
||||
*
|
||||
* @param Query $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid($query): bool
|
||||
{
|
||||
// Validate method
|
||||
$method = $query->getMethod();
|
||||
|
||||
if ($method !== Query::LIMIT) {
|
||||
$this->message = 'Query method invalid: ' . $method;
|
||||
return false;
|
||||
}
|
||||
|
||||
$limit = $query->getValue();
|
||||
return $this->isValidLimit($limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
|
@ -1,31 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Utopia\Database\Validator\Queries;
|
||||
namespace Appwrite\Utopia\Database\Validator\Query;
|
||||
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Validator\Range;
|
||||
use Utopia\Validator;
|
||||
|
||||
class LimitOffsetQuery extends Validator
|
||||
class Offset extends Validator
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $message = 'Invalid query';
|
||||
|
||||
protected int $maxLimit;
|
||||
protected int $maxOffset;
|
||||
|
||||
/**
|
||||
* Query constructor
|
||||
*
|
||||
* @param int $maxLimit
|
||||
* @param int $maxOffset
|
||||
* @param int $maxValuesCount
|
||||
*/
|
||||
public function __construct(int $maxLimit = 100, int $maxOffset = 5000)
|
||||
public function __construct(int $maxOffset = 5000)
|
||||
{
|
||||
$this->maxLimit = $maxLimit;
|
||||
$this->maxOffset = $maxOffset;
|
||||
}
|
||||
|
||||
|
@ -41,17 +37,6 @@ class LimitOffsetQuery extends Validator
|
|||
return $this->message;
|
||||
}
|
||||
|
||||
protected function isValidLimit($limit): bool
|
||||
{
|
||||
$validator = new Range(0, $this->maxLimit);
|
||||
if ($validator->isValid($limit)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->message = 'Invalid limit: ' . $validator->getDescription();
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function isValidOffset($offset): bool
|
||||
{
|
||||
$validator = new Range(0, $this->maxOffset);
|
||||
|
@ -66,7 +51,7 @@ class LimitOffsetQuery extends Validator
|
|||
/**
|
||||
* Is valid.
|
||||
*
|
||||
* Returns true if method is limit or offset and values are within range.
|
||||
* Returns true if method is offset and values are within range.
|
||||
*
|
||||
* @param Query $value
|
||||
*
|
||||
|
@ -76,19 +61,14 @@ class LimitOffsetQuery extends Validator
|
|||
{
|
||||
// Validate method
|
||||
$method = $query->getMethod();
|
||||
switch ($method) {
|
||||
case Query::TYPE_LIMIT:
|
||||
$limit = $query->getValue();
|
||||
return $this->isValidLimit($limit);
|
||||
|
||||
case Query::TYPE_OFFSET:
|
||||
$offset = $query->getValue();
|
||||
return $this->isValidOffset($offset);
|
||||
|
||||
default:
|
||||
$this->message = 'Query method invalid: ' . $method;
|
||||
return false;
|
||||
|
||||
if ($method !== Query::TYPE_OFFSET) {
|
||||
$this->message = 'Query method invalid: ' . $method;
|
||||
return false;
|
||||
}
|
||||
|
||||
$offset = $query->getValue();
|
||||
return $this->isValidOffset($offset);
|
||||
}
|
||||
|
||||
/**
|
|
@ -1,11 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Utopia\Database\Validator\Queries;
|
||||
namespace Appwrite\Utopia\Database\Validator\Query;
|
||||
|
||||
use Appwrite\Utopia\Database\Validator\Queries\LimitOffsetCursorFilterQuery;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Validator;
|
||||
|
||||
class LimitOffsetCursorFilterOrderQuery extends LimitOffsetCursorFilterQuery
|
||||
class Order extends Validator
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
|
@ -20,29 +20,29 @@ class LimitOffsetCursorFilterOrderQuery extends LimitOffsetCursorFilterQuery
|
|||
/**
|
||||
* Query constructor
|
||||
*
|
||||
* @param int $maxLimit
|
||||
* @param int $maxOffset
|
||||
* @param int $maxValuesCount
|
||||
*/
|
||||
public function __construct(int $maxLimit = 100, int $maxOffset = 5000, array $attributes = [], int $maxValuesCount = 100)
|
||||
public function __construct(array $attributes = [])
|
||||
{
|
||||
foreach ($attributes as $attribute) {
|
||||
$this->schema[$attribute->getAttribute('key')] = $attribute->getArrayCopy();
|
||||
}
|
||||
}
|
||||
|
||||
$this->maxValuesCount = $maxValuesCount;
|
||||
protected function isValidAttribute($attribute): bool
|
||||
{
|
||||
// Search for attribute in schema
|
||||
if (!isset($this->schema[$attribute])) {
|
||||
$this->message = 'Attribute not found in schema: ' . $attribute;
|
||||
return false;
|
||||
}
|
||||
|
||||
parent::__construct($maxLimit, $maxOffset);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is valid.
|
||||
*
|
||||
* Returns true if:
|
||||
* 1. method is limit or offset and values are within range
|
||||
* 2. method is cursorBefore or cursorAfter and value is not null
|
||||
* 3. method is a filter method, attribute exists, and value matches attribute type
|
||||
* 4. method is orderAsc or orderDesc and attribute exists or is empty string
|
||||
* Returns true if method is ORDER_ASC or ORDER_DESC and attributes are valid
|
||||
*
|
||||
* Otherwise, returns false
|
||||
*
|
Loading…
Reference in a new issue