diff --git a/composer.lock b/composer.lock index eb5849095e..b9176fe53e 100644 --- a/composer.lock +++ b/composer.lock @@ -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" } diff --git a/src/Appwrite/Utopia/Database/Validator/Queries.php b/src/Appwrite/Utopia/Database/Validator/Queries.php index cc675429e4..826f783c4d 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries.php @@ -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; } diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Collection.php b/src/Appwrite/Utopia/Database/Validator/Queries/Collection.php index 0404f299af..dffaabee6d 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Collection.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Collection.php @@ -55,4 +55,4 @@ class Collection extends QueriesValidator parent::__construct(new QueryValidator($attributes), $attributes, $indexes, true); } -} +} \ No newline at end of file diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/LimitOffsetCursorQuery.php b/src/Appwrite/Utopia/Database/Validator/Queries/LimitOffsetCursorQuery.php deleted file mode 100644 index f74d41474c..0000000000 --- a/src/Appwrite/Utopia/Database/Validator/Queries/LimitOffsetCursorQuery.php +++ /dev/null @@ -1,64 +0,0 @@ -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); - } -} diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Users.php b/src/Appwrite/Utopia/Database/Validator/Queries/Users.php index 59373c2cab..b61ec9c086 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Users.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Users.php @@ -28,4 +28,4 @@ class Users extends Collection { parent::__construct('users', self::ALLOWED_ATTRIBUTES); } -} +} \ No newline at end of file diff --git a/src/Appwrite/Utopia/Database/Validator/Query.php b/src/Appwrite/Utopia/Database/Validator/Query.php deleted file mode 100644 index eeb5b51a76..0000000000 --- a/src/Appwrite/Utopia/Database/Validator/Query.php +++ /dev/null @@ -1,21 +0,0 @@ -isValid($cursor)) { - return true; - } - - $this->message = 'Invalid cursor: ' . $validator->getDescription(); - return false; - } -} diff --git a/src/Appwrite/Utopia/Database/Validator/Query/Cursor.php b/src/Appwrite/Utopia/Database/Validator/Query/Cursor.php new file mode 100644 index 0000000000..0fb77ee0b6 --- /dev/null +++ b/src/Appwrite/Utopia/Database/Validator/Query/Cursor.php @@ -0,0 +1,44 @@ +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; + } +} diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/LimitOffsetCursorFilterQuery.php b/src/Appwrite/Utopia/Database/Validator/Query/Filter.php similarity index 65% rename from src/Appwrite/Utopia/Database/Validator/Queries/LimitOffsetCursorFilterQuery.php rename to src/Appwrite/Utopia/Database/Validator/Query/Filter.php index 71da608709..e32b3aab66 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/LimitOffsetCursorFilterQuery.php +++ b/src/Appwrite/Utopia/Database/Validator/Query/Filter.php @@ -1,12 +1,12 @@ 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; } } } diff --git a/src/Appwrite/Utopia/Database/Validator/Query/Limit.php b/src/Appwrite/Utopia/Database/Validator/Query/Limit.php new file mode 100644 index 0000000000..63f681f7d9 --- /dev/null +++ b/src/Appwrite/Utopia/Database/Validator/Query/Limit.php @@ -0,0 +1,97 @@ +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; + } +} diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/LimitOffsetQuery.php b/src/Appwrite/Utopia/Database/Validator/Query/Offset.php similarity index 56% rename from src/Appwrite/Utopia/Database/Validator/Queries/LimitOffsetQuery.php rename to src/Appwrite/Utopia/Database/Validator/Query/Offset.php index e11590757e..a319c91502 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/LimitOffsetQuery.php +++ b/src/Appwrite/Utopia/Database/Validator/Query/Offset.php @@ -1,31 +1,27 @@ 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); } /** diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/LimitOffsetCursorFilterOrderQuery.php b/src/Appwrite/Utopia/Database/Validator/Query/Order.php similarity index 52% rename from src/Appwrite/Utopia/Database/Validator/Queries/LimitOffsetCursorFilterOrderQuery.php rename to src/Appwrite/Utopia/Database/Validator/Query/Order.php index 3a8459fa47..9ee40fa117 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/LimitOffsetCursorFilterOrderQuery.php +++ b/src/Appwrite/Utopia/Database/Validator/Query/Order.php @@ -1,11 +1,11 @@ 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 *