Skip to content

Commit ce9ae74

Browse files
Remanufacture Statement clauses
Bugfixes Code cleanup
1 parent 7976e50 commit ce9ae74

25 files changed

+188
-161
lines changed

src/AQL/HasDateFunctions.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public function dateNow()
3535
*/
3636
public function dateIso8601()
3737
{
38-
if (empty($arguments = func_get_args())) {
38+
$arguments = func_get_args();
39+
if (empty($arguments)) {
3940
$arguments[] = time();
4041
}
4142

@@ -52,7 +53,8 @@ public function dateIso8601()
5253
*/
5354
public function dateTimestamp()
5455
{
55-
if (empty($arguments = func_get_args())) {
56+
$arguments = func_get_args();
57+
if (empty($arguments)) {
5658
$arguments[] = time();
5759
}
5860

src/AQL/HasQueryClauses.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ public function aggregate($variableName, $aggregateExpression): QueryBuilder
219219
*
220220
* @return QueryBuilder
221221
*/
222-
public function sort($sortBy = null, $direction = null): QueryBuilder
222+
public function sort(): QueryBuilder
223223
{
224-
$this->addClause(new SortClause($sortBy, $direction));
224+
$this->addClause(new SortClause(func_get_args()));
225225

226226
return $this;
227227
}

src/AQL/HasStatementClauses.php

-25
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ public function let($variableName, $expression)
4545
*/
4646
public function insert($document, string $collection): QueryBuilder
4747
{
48-
$document = $this->normalizeArgument($document, ['RegisteredVariable', 'Object', 'Bind']);
49-
50-
$this->registerCollections($collection);
51-
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);
52-
5348
$this->addClause(new InsertClause($document, $collection));
5449

5550
return $this;
@@ -68,11 +63,6 @@ public function insert($document, string $collection): QueryBuilder
6863
*/
6964
public function update($document, $with, $collection): QueryBuilder
7065
{
71-
$document = $this->normalizeArgument($document, ['RegisteredVariable', 'Key', 'Object', 'Bind']);
72-
$with = $this->normalizeArgument($with, ['Object', 'Bind']);
73-
$this->registerCollections($collection);
74-
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);
75-
7666
$this->addClause(new UpdateClause($document, $with, $collection));
7767

7868
return $this;
@@ -91,11 +81,6 @@ public function update($document, $with, $collection): QueryBuilder
9181
*/
9282
public function replace($document, $with, string $collection): QueryBuilder
9383
{
94-
$document = $this->normalizeArgument($document, ['RegisteredVariable', 'Key', 'Object', 'Bind']);
95-
$with = $this->normalizeArgument($with, ['Object', 'Bind']);
96-
$this->registerCollections($collection);
97-
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);
98-
9984
$this->addClause(new ReplaceClause($document, $with, $collection));
10085

10186
return $this;
@@ -118,12 +103,6 @@ public function replace($document, $with, string $collection): QueryBuilder
118103
*/
119104
public function upsert($search, $insert, $with, string $collection, bool $replace = false): QueryBuilder
120105
{
121-
$search = $this->normalizeArgument($search, ['RegisteredVariable', 'Key', 'Bind']);
122-
$insert = $this->normalizeArgument($insert, ['RegisteredVariable', 'Key', 'Bind']);
123-
$with = $this->normalizeArgument($with, ['Object', 'Bind']);
124-
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);
125-
$this->registerCollections($collection);
126-
127106
$this->addClause(new UpsertClause($search, $insert, $with, $collection, $replace));
128107

129108
return $this;
@@ -141,10 +120,6 @@ public function upsert($search, $insert, $with, string $collection, bool $replac
141120
*/
142121
public function remove($document, string $collection): QueryBuilder
143122
{
144-
$document = $this->normalizeArgument($document, ['RegisteredVariable', 'Key', 'Object', 'Bind']);
145-
$this->registerCollections($collection);
146-
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);
147-
148123
$this->addClause(new RemoveClause($document, $collection));
149124

150125
return $this;

src/Clauses/AggregateClause.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function compile(QueryBuilder $queryBuilder): string
2626
['Reference', 'Function', 'Query', 'Bind']
2727
);
2828

29-
return "AGGREGATE {$this->variableName->compile($queryBuilder)}
30-
= {$this->aggregateExpression->compile($queryBuilder)}";
29+
return "AGGREGATE {$this->variableName->compile($queryBuilder)} " .
30+
"= {$this->aggregateExpression->compile($queryBuilder)}";
3131
}
3232
}

src/Clauses/EdgeCollectionsClause.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ public function __construct(array $edgeCollections)
1717
$this->edgeCollections = $edgeCollections;
1818
}
1919

20+
/**
21+
*
22+
* @SuppressWarnings(PHPMD.UndefinedVariable)
23+
*
24+
* @param QueryBuilder $queryBuilder
25+
* @return string
26+
*/
2027
public function compile(QueryBuilder $queryBuilder): string
2128
{
2229
$this->edgeCollections = array_map(function ($edgeCollection) use ($queryBuilder) {
@@ -25,18 +32,21 @@ public function compile(QueryBuilder $queryBuilder): string
2532
}
2633

2734
$edgeCollection[0] = $queryBuilder->normalizeArgument($edgeCollection[0], 'Collection');
28-
if (isset($edgeCollections[1]) && !$this->grammar->isDirection($edgeCollections[1])) {
35+
if (isset($edgeCollections[1]) && !$queryBuilder->grammar->isGraphDirection($edgeCollections[1])) {
2936
unset($edgeCollections[1]);
3037
}
3138
return $edgeCollection;
3239
}, $this->edgeCollections);
3340

3441
$output = array_map(function ($edgeCollection) use ($queryBuilder) {
3542
if ($edgeCollection instanceof LiteralExpression) {
36-
return $edgeCollection->compile($queryBuilder) . ' ';
43+
return $edgeCollection->compile($queryBuilder);
3744
}
3845

3946
$edgeCollectionOutput = '';
47+
if (isset($edgeCollection[1])) {
48+
$edgeCollectionOutput = $edgeCollection[1] . ' ';
49+
}
4050

4151
$edgeCollectionOutput .= $edgeCollection[0]->compile($queryBuilder);
4252
return $edgeCollectionOutput;

src/Clauses/ForClause.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function compile(QueryBuilder $queryBuilder): string
3636
return $variable->compile($queryBuilder);
3737
}, $this->variables);
3838

39-
$variableExpression = implode(', ',$variableExpression);
39+
$variableExpression = implode(', ', $variableExpression);
4040

4141
if ($this->in !== null) {
4242
$this->in = $queryBuilder

src/Clauses/InsertClause.php

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

33
namespace LaravelFreelancerNL\FluentAQL\Clauses;
44

5+
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
6+
57
class InsertClause extends Clause
68
{
79
protected $document;
@@ -13,11 +15,18 @@ public function __construct($document, $collection)
1315
parent::__construct();
1416

1517
$this->document = $document;
18+
1619
$this->collection = $collection;
1720
}
1821

19-
public function compile(): string
22+
public function compile(QueryBuilder $queryBuilder): string
2023
{
21-
return "INSERT {$this->document} IN {$this->collection}";
24+
$this->document = $queryBuilder->normalizeArgument($this->document, ['RegisteredVariable', 'Object', 'Bind']);
25+
26+
$queryBuilder->registerCollections($this->collection);
27+
$this->collection = $queryBuilder->normalizeArgument($this->collection, ['Collection', 'Bind']);
28+
29+
30+
return "INSERT {$this->document->compile($queryBuilder)} IN {$this->collection->compile($queryBuilder)}";
2231
}
2332
}

src/Clauses/LetClause.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct($variableName, $expression)
2121
public function compile(QueryBuilder $queryBuilder): string
2222
{
2323
$this->variableName = $queryBuilder->normalizeArgument($this->variableName, 'Variable');
24-
$queryBuilder->registerVariable($this->variableName);
24+
$queryBuilder->registerVariable($this->variableName->compile($queryBuilder));
2525

2626
$this->expression = $queryBuilder->normalizeArgument(
2727
$this->expression,

src/Clauses/RemoveClause.php

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

33
namespace LaravelFreelancerNL\FluentAQL\Clauses;
44

5+
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
6+
57
class RemoveClause extends Clause
68
{
79
protected $document;
@@ -16,8 +18,15 @@ public function __construct($document, $collection)
1618
$this->collection = $collection;
1719
}
1820

19-
public function compile(): string
21+
public function compile(QueryBuilder $queryBuilder): string
2022
{
21-
return "REMOVE {$this->document} IN {$this->collection}";
23+
$this->document = $queryBuilder->normalizeArgument(
24+
$this->document,
25+
['RegisteredVariable', 'Key', 'Object', 'Bind']
26+
);
27+
$queryBuilder->registerCollections($this->collection);
28+
$this->collection = $queryBuilder->normalizeArgument($this->collection, ['Collection', 'Bind']);
29+
30+
return "REMOVE {$this->document->compile($queryBuilder)} IN {$this->collection->compile($queryBuilder)}";
2231
}
2332
}

src/Clauses/ReplaceClause.php

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

33
namespace LaravelFreelancerNL\FluentAQL\Clauses;
44

5+
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
6+
57
class ReplaceClause extends Clause
68
{
79
protected $document;
@@ -19,8 +21,21 @@ public function __construct($document, $with, $collection)
1921
$this->collection = $collection;
2022
}
2123

22-
public function compile(): string
24+
public function compile(QueryBuilder $queryBuilder): string
2325
{
24-
return "REPLACE {$this->document} WITH {$this->with} IN {$this->collection}";
26+
$this->document = $queryBuilder->normalizeArgument(
27+
$this->document,
28+
['RegisteredVariable', 'Key', 'Object', 'Bind']
29+
);
30+
31+
$this->with = $queryBuilder->normalizeArgument($this->with, ['Object', 'Bind']);
32+
33+
$this->collection = $queryBuilder->normalizeArgument($this->collection, ['Collection', 'Bind']);
34+
$queryBuilder->registerCollections($this->collection->compile($queryBuilder));
35+
36+
37+
return "REPLACE {$this->document->compile($queryBuilder)} " .
38+
"WITH {$this->with->compile($queryBuilder)} " .
39+
"IN {$this->collection->compile($queryBuilder)}";
2540
}
2641
}

src/Clauses/SortClause.php

+42-20
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,64 @@
22

33
namespace LaravelFreelancerNL\FluentAQL\Clauses;
44

5+
use LaravelFreelancerNL\FluentAQL\Expressions\ExpressionInterface;
56
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
67

78
class SortClause extends Clause
89
{
9-
protected $by;
10+
protected $attributes;
1011

11-
protected $direction;
12-
13-
public function __construct($sortBy = null, $direction = null)
12+
public function __construct($attributes)
1413
{
1514
parent::__construct();
1615

17-
$this->by = $sortBy;
18-
$this->direction = $direction;
16+
$this->attributes = $attributes;
1917
}
2018

2119
public function compile(QueryBuilder $queryBuilder): string
2220
{
23-
$sortExpressions = [];
24-
25-
//normalize string|null $by and $direction
26-
if (is_string($this->by) || $this->by == null) {
27-
$sortExpressions[] = $queryBuilder->normalizeSortExpression($this->by, $this->direction);
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]]];
2828
}
2929

30-
if (is_array($this->by)) {
31-
//Wandel door de array
32-
$sortExpressions = array_map(function ($expression) use ($queryBuilder) {
33-
return $queryBuilder->normalizeSortExpression($expression);
34-
}, $this->by);
30+
if (empty($this->attributes)) {
31+
$this->attributes = [null];
3532
}
3633

37-
$sortExpressions = implode(', ', array_map(function ($expression) {
38-
return implode(' ', $expression);
39-
}, $sortExpressions));
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];
45+
}
46+
return $output;
47+
}, $this->attributes);
4048

41-
return 'SORT ' . $sortExpressions;
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']);
57+
}
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]);
61+
}
62+
return $sortBy;
63+
}, $this->attributes);
4264
}
4365
}

src/Clauses/TraverseClause.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(
3535
public function compile(QueryBuilder $queryBuilder): string
3636
{
3737
$this->startVertex = $queryBuilder->normalizeArgument($this->startVertex, 'Id');
38-
$this->direction = $queryBuilder->normalizeArgument($this->direction, 'Direction');
38+
$this->direction = $queryBuilder->normalizeArgument($this->direction, 'GraphDirection');
3939

4040
if ($this->toVertex !== null) {
4141
$this->toVertex = $queryBuilder->normalizeArgument($this->toVertex, 'Id');

src/Clauses/UpdateClause.php

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

33
namespace LaravelFreelancerNL\FluentAQL\Clauses;
44

5+
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
6+
57
class UpdateClause extends Clause
68
{
79
protected $document;
@@ -19,8 +21,18 @@ public function __construct($document, $with, $collection)
1921
$this->collection = $collection;
2022
}
2123

22-
public function compile(): string
24+
public function compile(QueryBuilder $queryBuilder): string
2325
{
24-
return "UPDATE {$this->document} WITH {$this->with} IN {$this->collection}";
26+
$this->document = $queryBuilder->normalizeArgument(
27+
$this->document,
28+
['RegisteredVariable', 'Key', 'Object', 'Bind']
29+
);
30+
$this->with = $queryBuilder->normalizeArgument($this->with, ['Object', 'Bind']);
31+
$this->collection = $queryBuilder->normalizeArgument($this->collection, ['Collection', 'Bind']);
32+
$queryBuilder->registerCollections($this->collection->compile($queryBuilder));
33+
34+
return "UPDATE {$this->document->compile($queryBuilder)} " .
35+
"WITH {$this->with->compile($queryBuilder)} " .
36+
"IN {$this->collection->compile($queryBuilder)}";
2537
}
2638
}

0 commit comments

Comments
 (0)