diff --git a/src/Database/Database.php b/src/Database/Database.php index f9b176b9c..c087a6f93 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -13,7 +13,7 @@ class Database // Simple Types const VAR_STRING = 'string'; const VAR_INTEGER = 'integer'; - const VAR_FLOAT = 'float'; + const VAR_FLOAT = 'double'; const VAR_BOOLEAN = 'boolean'; // Relationships Types diff --git a/src/Database/Validator/QueryValidator.php b/src/Database/Validator/QueryValidator.php new file mode 100644 index 000000000..e3246a1b1 --- /dev/null +++ b/src/Database/Validator/QueryValidator.php @@ -0,0 +1,120 @@ +schema = $schema; + } + + /** + * Get Description. + * + * Returns validator description + * + * @return string + */ + public function getDescription() + { + return $this->message; + } + + /** + * Is valid. + * + * Returns true if query typed according to schema. + * + * @param $query + * + * @return bool + */ + public function isValid($query) + { + // Validate operator + if (!in_array($query->getOperator(), $this->operators)) { + $this->message = 'Query operator invalid: ' . $query->getOperator(); + return false; + } + + // Search for attribute in schema + $attributeIndex = array_search($query->getAttribute(), array_column($this->schema, '$id')); + + if ($attributeIndex === false) { + $this->message = 'Attribute not found in schema: ' . $query->getAttribute(); + return false; + } + + // Extract the type of desired attribute from collection $schema + $attributeType = $this->schema[array_search($query->getAttribute(), array_column($this->schema, '$id'))]['type']; + + foreach ($query->getValues() as $value) { + if (gettype($value) !== $attributeType) { + $this->message = 'Query type does not match expected: ' . $attributeType; + return false; + } + } + + return true; + } + /** + * 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/tests/Database/Validator/QueryValidatorTest.php b/tests/Database/Validator/QueryValidatorTest.php new file mode 100644 index 000000000..4ad863158 --- /dev/null +++ b/tests/Database/Validator/QueryValidatorTest.php @@ -0,0 +1,120 @@ + 'title', + 'type' => Database::VAR_STRING, + 'size' => 256, + 'required' => true, + 'signed' => true, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'description', + 'type' => Database::VAR_STRING, + 'size' => 1000000, + 'required' => true, + 'signed' => true, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'rating', + 'type' => Database::VAR_INTEGER, + 'size' => 5, + 'required' => true, + 'signed' => true, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'price', + 'type' => Database::VAR_FLOAT, + 'size' => 5, + 'required' => true, + 'signed' => true, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'published', + 'type' => Database::VAR_BOOLEAN, + 'size' => 5, + 'required' => true, + 'signed' => true, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => 'tags', + 'type' => Database::VAR_STRING, + 'size' => 55, + 'required' => true, + 'signed' => true, + 'array' => true, + 'filters' => [], + ], + ]; + + public function setUp(): void + { + } + + public function tearDown(): void + { + } + + public function testQuery() + { + $validator = new QueryValidator($this->schema); + + $this->assertEquals(true, $validator->isValid(Query::parse('title.notEqual("Iron Man", "Ant Man")'))); + $this->assertEquals(true, $validator->isValid(Query::parse('description.equal("Best movie ever")'))); + $this->assertEquals(true, $validator->isValid(Query::parse('rating.greater(4)'))); + + $this->assertEquals(true, $validator->isValid(Query::parse('price.lesserEqual(6.50)'))); + } + + public function testInvalidOperator() + { + $validator = new QueryValidator($this->schema); + + $response = $validator->isValid(Query::parse('title.eqqual("Iron Man")')); + + $this->assertEquals(false, $response); + $this->assertEquals('Query operator invalid: eqqual', $validator->getDescription()); + } + + public function testAttributeNotFound() + { + $validator = new QueryValidator($this->schema); + + $response = $validator->isValid(Query::parse('name.equal("Iron Man")')); + + $this->assertEquals(false, $response); + $this->assertEquals('Attribute not found in schema: name', $validator->getDescription()); + } + + public function testAttributeWrongType() + { + $validator = new QueryValidator($this->schema); + + $response = $validator->isValid(Query::parse('title.equal(1776)')); + + $this->assertEquals(false, $response); + $this->assertEquals('Query type does not match expected: string', $validator->getDescription()); + } +}