diff --git a/docs/api/query-clauses.md b/docs/api/query-clauses.md index d08b336..8d594cd 100644 --- a/docs/api/query-clauses.md +++ b/docs/api/query-clauses.md @@ -294,6 +294,31 @@ FOR user IN users [ArangoDB AGGREGATE documentation](https://www.arangodb.com/docs/stable/aql/operations-collect.html#aggregation) +## WINDOW +``` +window( + array|object $offsets, + string|QueryBuilder|Expression $rangeValue = null + ) +``` +Aggregate adjacent documents or value ranges with a sliding window +to calculate running totals, rolling averages, and other statistical properties + +**Example 1:** +```php +$qb->window(['preceding' => 5, 'following' => 10]) +``` +Resulting AQL: `... WINDOW { "preceding": 5, "following": 10 } ...` + +**Example 2: using range-based aggregation** +```php +$qb->window(['preceding' => 5, 'following' => 10], 't.time') +``` +Resulting AQL: `... WINDOW t.time WITH { "preceding": 5, "following": 10 } ...` + +[ArangoDB WINDOW documentation](https://www.arangodb.com/docs/stable/aql/operations-window.html) + + ## Other clauses ### OPTIONS diff --git a/src/AQL/HasDocumentFunctions.php b/src/AQL/HasDocumentFunctions.php index 40358a1..6b521e5 100644 --- a/src/AQL/HasDocumentFunctions.php +++ b/src/AQL/HasDocumentFunctions.php @@ -89,10 +89,10 @@ public function matches( * * @link https://www.arangodb.com/docs/stable/aql/functions-document.html#merge * - * @param array $documents + * @param array|string|Expression $documents * @return FunctionExpression */ - public function merge(array ...$documents): FunctionExpression + public function merge(string|array|Expression ...$documents): FunctionExpression { return new FunctionExpression('MERGE', $documents); } diff --git a/tests/Unit/AQL/DocumentFunctionsTest.php b/tests/Unit/AQL/DocumentFunctionsTest.php index 9f9985e..3518b5a 100644 --- a/tests/Unit/AQL/DocumentFunctionsTest.php +++ b/tests/Unit/AQL/DocumentFunctionsTest.php @@ -69,6 +69,29 @@ public function testMerge() self::assertEquals('RETURN MERGE({"user1":{"name":"Janet"}}, {"user2":{"name":"Tom"}})', $qb->get()->query); } + public function testMergeVariable() + { + $qb = new QueryBuilder(); + + $qb->for('u', 'users') + ->return( + $qb->merge( + 'u', + [ + 'user1' => ['name' => 'Janet'] + ], + [ + 'user2' => ['name' => 'Tom'] + ] + ) + ); + + self::assertEquals( + 'FOR u IN users RETURN MERGE(u, {"user1":{"name":"Janet"}}, {"user2":{"name":"Tom"}})', + $qb->get()->query + ); + } + public function testMergeWithArrayOfDocuments() { $qb = new QueryBuilder();