From 297980ff37405afbc276e35a4b7b618035a8f06e Mon Sep 17 00:00:00 2001 From: Bas de Groot Date: Sat, 5 Dec 2020 19:38:57 +0100 Subject: [PATCH] - Fixed empty strings represented as nothing. - Added concat AQL function support - Added date_format AQL function support --- docs/api/functions.md | 6 ++++++ src/AQL/HasDateFunctions.php | 14 +++++++++++++ src/AQL/HasFunctions.php | 1 + src/AQL/HasStringFunctions.php | 26 ++++++++++++++++++++++++ src/Traits/NormalizesDateFunctions.php | 13 ++++++++++++ src/Traits/NormalizesFunctions.php | 3 ++- src/Traits/NormalizesStringFunctions.php | 20 ++++++++++++++++++ src/Traits/ValidatesExpressions.php | 2 +- tests/Unit/AQL/DateFunctionsTest.php | 8 ++++++++ tests/Unit/AQL/QueryClausesTest.php | 12 +++++++++++ tests/Unit/AQL/StringFunctionsTest.php | 20 ++++++++++++++++++ 11 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 src/AQL/HasStringFunctions.php create mode 100644 src/Traits/NormalizesStringFunctions.php create mode 100644 tests/Unit/AQL/StringFunctionsTest.php diff --git a/docs/api/functions.md b/docs/api/functions.md index 43457ed..cf83bac 100644 --- a/docs/api/functions.md +++ b/docs/api/functions.md @@ -40,6 +40,7 @@ $qb->for('i', '1..100') | dateMinute($date) | [DATE_MINUTE(date)](https://www.arangodb.com/docs/stable/aql/functions-date.html#date_minute) | | dateSecond($date) | [DATE_SECOND(date)](https://www.arangodb.com/docs/stable/aql/functions-date.html#date_second) | | dateMillisecond($date) | [DATE_MILLISECOND(date)](https://www.arangodb.com/docs/stable/aql/functions-date.html#date_millisecond) | +| dateFormat($date, $format) | [DATE_FORMAT(date, format)](https://www.arangodb.com/docs/stable/aql/functions-date.html#date_format) | ## GEO functions | Description | AQL Function | @@ -60,3 +61,8 @@ $qb->for('i', '1..100') | min($value) | [MIN(anyArray)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#min) | | rand() | [RAND()](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#rand) | | sum($value) | [SUM(numArray)](https://www.arangodb.com/docs/stable/aql/functions-numeric.html#sum) | + +## String functions +| Description | AQL Function | +| :-------------------- | :-------------------------------------------------------------------------------------------- | +| concat(...$arguments) | [CONCAT(value1, value2, ... valueN)](https://www.arangodb.com/docs/stable/aql/functions-string.html#concat) | diff --git a/src/AQL/HasDateFunctions.php b/src/AQL/HasDateFunctions.php index ced5828..6c738fd 100644 --- a/src/AQL/HasDateFunctions.php +++ b/src/AQL/HasDateFunctions.php @@ -158,4 +158,18 @@ public function dateMillisecond($date) { return new FunctionExpression('DATE_MILLISECOND', $date); } + + /** + * Get the custom formatted representation of the date. + * + * @Link https://www.arangodb.com/docs/stable/aql/functions-date.html#date_format + * + * @param $date + * @param $format + * @return FunctionExpression + */ + public function dateFormat($date, $format): FunctionExpression + { + return new FunctionExpression('DATE_FORMAT', [$date, $format]); + } } diff --git a/src/AQL/HasFunctions.php b/src/AQL/HasFunctions.php index ebc34ad..9009a6a 100644 --- a/src/AQL/HasFunctions.php +++ b/src/AQL/HasFunctions.php @@ -17,6 +17,7 @@ trait HasFunctions use HasGeoFunctions; use HasMiscellaneousFunctions; use HasNumericFunctions; + use HasStringFunctions; protected function function($functionName, ...$parameters) { diff --git a/src/AQL/HasStringFunctions.php b/src/AQL/HasStringFunctions.php new file mode 100644 index 0000000..7b25fe9 --- /dev/null +++ b/src/AQL/HasStringFunctions.php @@ -0,0 +1,26 @@ +parameters[0] = $queryBuilder->normalizeArgument( + $this->parameters[0], + ['Number', 'Function', 'Query', 'Reference', 'Bind'] + ); + + $this->parameters[1] = $queryBuilder->normalizeArgument( + $this->parameters[1], + ['Function', 'Query', 'Reference', 'Bind'] + ); + } } diff --git a/src/Traits/NormalizesFunctions.php b/src/Traits/NormalizesFunctions.php index 7b6e4f7..19fd507 100644 --- a/src/Traits/NormalizesFunctions.php +++ b/src/Traits/NormalizesFunctions.php @@ -17,6 +17,7 @@ trait NormalizesFunctions use NormalizesGeoFunctions; use NormalizesMiscellaneousFunctions; use NormalizesNumericFunctions; + use NormalizesStringFunctions; protected function normalizeArrays(QueryBuilder $queryBuilder) { @@ -54,7 +55,7 @@ protected function normalizeStrings(QueryBuilder $queryBuilder) foreach ($this->parameters as $key => $parameter) { $this->parameters[$key] = $queryBuilder->normalizeArgument( $parameter, - ['Query', 'Variable', 'Reference', 'Bind'] + ['Query', 'Reference', 'Bind'] ); } } diff --git a/src/Traits/NormalizesStringFunctions.php b/src/Traits/NormalizesStringFunctions.php new file mode 100644 index 0000000..07d3174 --- /dev/null +++ b/src/Traits/NormalizesStringFunctions.php @@ -0,0 +1,20 @@ +normalizeStrings($queryBuilder); + } +} diff --git a/src/Traits/ValidatesExpressions.php b/src/Traits/ValidatesExpressions.php index 46185c8..f0e6327 100644 --- a/src/Traits/ValidatesExpressions.php +++ b/src/Traits/ValidatesExpressions.php @@ -232,7 +232,7 @@ public function isReference($value, $registeredVariables = []): bool $variables = implode('|', $registeredVariables); } - if (! is_string($value)) { + if (! is_string($value) || empty($value)) { return false; } diff --git a/tests/Unit/AQL/DateFunctionsTest.php b/tests/Unit/AQL/DateFunctionsTest.php index 8873d36..3b660b1 100644 --- a/tests/Unit/AQL/DateFunctionsTest.php +++ b/tests/Unit/AQL/DateFunctionsTest.php @@ -109,4 +109,12 @@ public function testDateMillisecond() $qb->return($qb->dateMillisecond(1399472349522)); self::assertEquals('RETURN DATE_MILLISECOND(1399472349522)', $qb->get()->query); } + + public function testDateFormat() + { + $qb = new QueryBuilder(); + $qb->return($qb->dateFormat(1399472349522, "%q/%yyyy")); + self::assertEquals('RETURN DATE_FORMAT(1399472349522, @' . $qb->getQueryId() . '_1)', $qb->get()->query); + } + } diff --git a/tests/Unit/AQL/QueryClausesTest.php b/tests/Unit/AQL/QueryClausesTest.php index bd6c58a..80050bf 100644 --- a/tests/Unit/AQL/QueryClausesTest.php +++ b/tests/Unit/AQL/QueryClausesTest.php @@ -175,6 +175,18 @@ public function testFiltersWithRecursiveFiltersStartingArray() ); } + public function testFiltersWithEmptyStringValue() + { + $result = (new QueryBuilder()) + ->for('doc', 'documents') + ->filter('doc.test', '==', '') + ->get(); + self::assertEquals( + 'FOR doc IN documents FILTER doc.test == @' . $result->getQueryId() . '_1', + $result->query + ); + } + public function testSearchClause() { $result = (new QueryBuilder()) diff --git a/tests/Unit/AQL/StringFunctionsTest.php b/tests/Unit/AQL/StringFunctionsTest.php new file mode 100644 index 0000000..3a1ee7f --- /dev/null +++ b/tests/Unit/AQL/StringFunctionsTest.php @@ -0,0 +1,20 @@ +return($qb->concat('string', 'this', 'together')); + self::assertEquals('RETURN CONCAT(@' . $qb->getQueryId() . '_1, @' . $qb->getQueryId() . '_2, @' . $qb->getQueryId() . '_3)', $qb->get()->query); + } +}