Skip to content

Commit 4a2a7fc

Browse files
* added credits to documentation
* simplified sort API * simplified edgeCollections API * split bind method in bind and bindCollection * code cleanup
1 parent 89acfcc commit 4a2a7fc

23 files changed

+181
-136
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Fluent PHP query builder for [ArangoDB’s](https://www.arangodb.com) Query Lang
88
[![Code Coverage](https://scrutinizer-ci.com/g/LaravelFreelancerNL/fluentaql/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/LaravelFreelancerNL/fluentaql/?branch=master)
99
[![License](https://poser.pugx.org/laravel-freelancer-nl/fluentaql/license)](//packagist.org/packages/laravel-freelancer-nl/fluentaql)
1010

11-
**1.0.0-alpha release: API may change in future releases**
11+
**1.0.0-alpha release: API may change before a stable version is released**
1212

1313
## Table of contents
1414
1. [Use Cases](#purpose)
@@ -122,3 +122,6 @@ $qb->bind('your data', 'your-bind-id')
122122
### ArangoDB PHP clients
123123
- [Official ArangoDB PHP driver](https://github.com/arangodb/arangodb-php)
124124
- [Community PHP driver](https://github.com/sandrokeil/arangodb-php-client)
125+
126+
## Credits
127+
- Pluma@arangodb

docs/api/graph-clauses.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,12 @@ Resulting AQL: `WITH users, cities FOR v, e, p ANY "users/1" GRAPH "citizens"`
150150

151151
## EDGE COLLECTIONS (unnamed graph)
152152
```
153-
edgeCollections(...$edgeCollections)
153+
edgeCollections(...$edgeCollections)
154154
```
155155
Execute the previously set traversal on the given set of edge collections.
156+
To specify a direction add it before the accompanying edge collection.
156157

157-
**Example:**
158+
**Example 1: multiple edge collections**
158159
```
159160
$qb = new QueryBuilder();
160161
$qb->with('users', 'cities')
@@ -164,6 +165,17 @@ Execute the previously set traversal on the given set of edge collections.
164165
```
165166
Resulting AQL: `WITH users, cities FOR v, e, p ANY "users/1" edge1, edge2, edge3`
166167

168+
**Example 2: with directions**
169+
```
170+
$qb = new QueryBuilder();
171+
$qb->with('users', 'cities')
172+
->for(['v', 'e', 'p'])
173+
->traverse('users/1', 'ANY')
174+
->edgeCollections('edge1', 'inbound', edge2', 'edge3')
175+
```
176+
Resulting AQL: `WITH users, cities FOR v, e, p ANY "users/1" edge1, INBOUND edge2, edge3`
177+
178+
167179
[ArangoDB UNNAMED GRAPH documentation](https://www.arangodb.com/docs/stable/aql/graphs-traversals.html#working-with-collection-sets)
168180

169181

docs/api/query-clauses.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Resulting AQL: `... SEARCH u.age < 18 OR u.age > 65 ...`
104104
```
105105
sort($reference, $direction)
106106
```
107-
Return the result of a (sub)query.
107+
Return the result of a (sub)query. To set a direction add the appropriate string attribute to the method.
108108

109109
**Example 1: default direction (asc)**
110110
```
@@ -120,7 +120,7 @@ Resulting AQL: `... SORT user.name DESC ...`
120120

121121
**Example 3: sort by multiple attributes**
122122
```
123-
$qb->sort(['user.name', 'desc'], 'user.email');
123+
$qb->sort('user.name', 'desc', 'user.email');
124124
```
125125
Resulting AQL: `... SORT user.name DESC, user.email ...`
126126

docs/core-concepts/data-binding.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Data binding
22

3+
## Bind collection names

src/AQL/HasGraphClauses.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
*/
1818
trait HasGraphClauses
1919
{
20+
21+
abstract public function addCommand($command);
22+
2023
/**
2124
* Start a query with 'WITH' to prevent graph traversal deadlocks.
2225
* This is required in clusters.
@@ -113,11 +116,12 @@ public function graph(string $graphName): self
113116
*
114117
* @link https://www.arangodb.com/docs/stable/aql/graphs-traversals.html
115118
*
119+
* @param array $edgeCollections
116120
* @return QueryBuilder
117121
*/
118-
public function edgeCollections(): self
122+
public function edgeCollections(...$edgeCollections): self
119123
{
120-
$this->addCommand(new EdgeCollectionsClause(func_get_args()));
124+
$this->addCommand(new EdgeCollectionsClause($edgeCollections));
121125

122126
return $this;
123127
}

src/AQL/HasNumericFunctions.php

+5-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ trait HasNumericFunctions
1818
*
1919
* @link https://www.arangodb.com/docs/stable/aql/functions-numeric.html#average
2020
*
21-
* @param string|array $value
21+
* @param mixed $value
2222
*
2323
* @return FunctionExpression
2424
*/
@@ -37,7 +37,7 @@ public function avg($value)
3737
*
3838
* @link https://www.arangodb.com/docs/stable/aql/functions-numeric.html#max
3939
*
40-
* @param string|array $value
40+
* @param mixed $value
4141
*
4242
* @return FunctionExpression
4343
*/
@@ -51,7 +51,7 @@ public function max($value)
5151
*
5252
* @link https://www.arangodb.com/docs/stable/aql/functions-numeric.html#min
5353
*
54-
* @param string|array $value
54+
* @param mixed $value
5555
*
5656
* @return FunctionExpression
5757
*/
@@ -76,16 +76,12 @@ public function rand()
7676
*
7777
* @link https://www.arangodb.com/docs/stable/aql/functions-numeric.html#sum
7878
*
79-
* @param string|array $value
79+
* @param mixed $value
8080
*
8181
* @return FunctionExpression
8282
*/
8383
public function sum($value)
8484
{
85-
$arguments = [];
86-
87-
$arguments['value'] = $this->normalizeArgument($value, ['List', 'Variable', 'Reference']);
88-
89-
return new FunctionExpression('SUM', $arguments);
85+
return new FunctionExpression('SUM', [$value]);
9086
}
9187
}

src/AQL/HasQueryClauses.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
trait HasQueryClauses
2626
{
27+
28+
abstract public function addCommand($command);
29+
2730
/**
2831
* Create a for clause.
2932
*
@@ -195,11 +198,12 @@ public function aggregate($variableName, $aggregateExpression): self
195198
*
196199
* @link https://www.arangodb.com/docs/stable/aql/operations-sort.html
197200
*
201+
* @param mixed ...$references
198202
* @return QueryBuilder
199203
*/
200-
public function sort(): self
204+
public function sort(...$references): self
201205
{
202-
$this->addCommand(new SortClause(func_get_args()));
206+
$this->addCommand(new SortClause($references));
203207

204208
return $this;
205209
}

src/AQL/HasStatementClauses.php

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
trait HasStatementClauses
1818
{
19+
20+
abstract public function addCommand($command);
21+
1922
/**
2023
* Assign a value to a variable.
2124
*

src/AQL/HasSupportCommands.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace LaravelFreelancerNL\FluentAQL\AQL;
44

55
use LaravelFreelancerNL\FluentAQL\Clauses\RawClause;
6-
use LaravelFreelancerNL\FluentAQL\Expressions\FunctionExpression;
76
use LaravelFreelancerNL\FluentAQL\Expressions\LiteralExpression;
8-
use LaravelFreelancerNL\FluentAQL\Expressions\RawExpression;
97
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
108

119
/**
@@ -15,13 +13,18 @@
1513
*/
1614
trait HasSupportCommands
1715
{
16+
17+
abstract public function addCommand($command);
18+
abstract public function bind($data, $to = null);
19+
abstract public function registerCollections($collections, $mode = 'write');
20+
1821
/**
1922
* @param string $aql
2023
* @param array $binds
2124
* @param array $collections
2225
* @return $this|QueryBuilder
2326
*/
24-
public function raw(string $aql, $binds = [], $collections = []): QueryBuilder
27+
public function raw(string $aql, $binds = [], $collections = []): self
2528
{
2629
foreach ($binds as $key => $value) {
2730
$this->bind($value, $key);

src/Clauses/EdgeCollectionsClause.php

+12-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace LaravelFreelancerNL\FluentAQL\Clauses;
44

5+
use LaravelFreelancerNL\FluentAQL\Expressions\Expression;
6+
use LaravelFreelancerNL\FluentAQL\Expressions\ExpressionInterface;
57
use LaravelFreelancerNL\FluentAQL\Expressions\LiteralExpression;
68
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
79

@@ -26,30 +28,22 @@ public function __construct(array $edgeCollections)
2628
public function compile(QueryBuilder $queryBuilder): string
2729
{
2830
$this->edgeCollections = array_map(function ($edgeCollection) use ($queryBuilder) {
29-
if (is_string($edgeCollection)) {
30-
return $queryBuilder->normalizeArgument($edgeCollection, 'Collection');
31-
}
32-
33-
$edgeCollection[0] = $queryBuilder->normalizeArgument($edgeCollection[0], 'Collection');
34-
if (isset($edgeCollection[1]) && !$queryBuilder->grammar->isGraphDirection($edgeCollection[1])) {
35-
unset($edgeCollection[1]);
31+
if (!$queryBuilder->grammar->isGraphDirection($edgeCollection)) {
32+
return $queryBuilder->normalizeArgument($edgeCollection, ['Collection', 'Query', 'Bind']);
3633
}
3734
return $edgeCollection;
3835
}, $this->edgeCollections);
3936

40-
$output = array_map(function ($edgeCollection) use ($queryBuilder) {
41-
if ($edgeCollection instanceof LiteralExpression) {
42-
return $edgeCollection->compile($queryBuilder);
37+
$output = '';
38+
foreach ($this->edgeCollections as $value) {
39+
if ($value instanceof ExpressionInterface) {
40+
$output .= $value->compile($queryBuilder) . ', ';
4341
}
44-
45-
$edgeCollectionOutput = '';
46-
if (isset($edgeCollection[1])) {
47-
$edgeCollectionOutput = $edgeCollection[1] . ' ';
42+
if (is_string($value)) {
43+
$output .= $value . ' ';
4844
}
45+
}
4946

50-
return $edgeCollectionOutput . $edgeCollection[0]->compile($queryBuilder);
51-
}, $this->edgeCollections);
52-
53-
return implode(', ', $output);
47+
return rtrim($output, ', ');
5448
}
5549
}

src/Clauses/ForClause.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function compile(QueryBuilder $queryBuilder): string
4040

4141
if ($this->in !== null) {
4242
$this->in = $queryBuilder
43-
->normalizeArgument($this->in, ['Collection', 'Range', 'List', 'Reference', 'Query', 'Bind'])
43+
->normalizeArgument($this->in, ['Collection', 'Range', 'List', 'Reference', 'Query', 'CollectionBind'])
4444
->compile($queryBuilder);
4545
}
4646
$inExpression = (string) $this->in;

src/Clauses/SortClause.php

+22-40
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,41 @@
77

88
class SortClause extends Clause
99
{
10-
protected $attributes;
10+
/**
11+
* @var array $references
12+
*/
13+
protected $references;
1114

12-
public function __construct($attributes)
15+
public function __construct($references)
1316
{
1417
parent::__construct();
1518

16-
$this->attributes = $attributes;
19+
$this->references = $references;
1720
}
1821

1922
public function compile(QueryBuilder $queryBuilder): string
2023
{
21-
//Structure input of reference + direction to the regular sort Expressions
22-
if (
23-
count($this->attributes) == 2
24-
&& is_string($this->attributes[1])
25-
&& $queryBuilder->grammar->isSortDirection($this->attributes[1])
26-
) {
27-
$this->attributes = [[$this->attributes[0], $this->attributes[1]]];
24+
if (empty($this->references[0])) {
25+
return 'SORT null';
2826
}
2927

30-
if (empty($this->attributes)) {
31-
$this->attributes = [null];
32-
}
33-
34-
$this->attributes = $this->normalizeSortExpressions($queryBuilder, $this->attributes);
35-
36-
//Generate query output
37-
$sortExpressionOutput = array_map(function ($sortBy) use ($queryBuilder) {
38-
if ($sortBy instanceof ExpressionInterface) {
39-
return $sortBy->compile($queryBuilder);
40-
}
41-
42-
$output = $sortBy[0]->compile($queryBuilder);
43-
if (isset($sortBy[1])) {
44-
$output .= ' ' . $sortBy[1];
28+
$this->references = array_map(function ($reference) use ($queryBuilder) {
29+
if (!$queryBuilder->grammar->isSortDirection($reference)) {
30+
return $queryBuilder->normalizeArgument($reference, ['Reference', 'Null', 'Query', 'Bind']);
4531
}
46-
return $output;
47-
}, $this->attributes);
32+
return $reference;
33+
}, $this->references);
4834

49-
return 'SORT ' . implode(', ', $sortExpressionOutput);
50-
}
51-
52-
protected function normalizeSortExpressions(QueryBuilder $queryBuilder, array $attributes)
53-
{
54-
return array_map(function ($sortBy) use ($queryBuilder) {
55-
if (is_string($sortBy) || $sortBy === null) {
56-
return $queryBuilder->normalizeArgument($sortBy, ['Null', 'Reference', 'Function', 'Bind']);
35+
$output = '';
36+
foreach ($this->references as $value) {
37+
if ($value instanceof ExpressionInterface) {
38+
$output .= ', ' . $value->compile($queryBuilder);
5739
}
58-
$sortBy[0] = $queryBuilder->normalizeArgument($sortBy[0], ['Null', 'Reference', 'Function', 'Bind']);
59-
if (isset($sortBy[1]) && ! $queryBuilder->grammar->isSortDirection($sortBy[1])) {
60-
unset($sortBy[1]);
40+
if (is_string($value)) {
41+
$output .= ' ' . $value;
6142
}
62-
return $sortBy;
63-
}, $this->attributes);
43+
}
44+
45+
return 'SORT ' . ltrim($output, ', ');
6446
}
6547
}

src/Grammar.php

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class Grammar
8787
'AssociativeArray' => 'Object',
8888
'Attribute' => 'Literal',
8989
'Bind' => 'Bind',
90+
'CollectionBind' => 'CollectionBind',
9091
'Boolean' => 'Boolean',
9192
'Collection' => 'Literal',
9293
'Constant' => 'Constant',

0 commit comments

Comments
 (0)