Skip to content

Commit 54e4fb1

Browse files
author
bas
committed
- Abstracted several expression to their base
- Arrays and object values are now recursively normalized allowing for the use of attribute pointers and functions within an object or List - Improved performance of several syntax checking methods especially concerning operators.
1 parent 4acc2d7 commit 54e4fb1

33 files changed

+608
-428
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"phpstan/phpstan": "^0.12.0@dev",
3333
"phpunit/phpunit": "8.*",
3434
"sebastian/phpcpd": "^4.1",
35-
"triagens/arangodb": "3.5.*"
35+
"triagens/arangodb": "^3.5"
3636
},
3737
"autoload": {
3838
"psr-4": {

src/API/hasGraphClauses.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function with() : QueryBuilder
2929
$collections = func_get_args();
3030
foreach ($collections as $key => $collection) {
3131
$this->registerCollections($collection, 'read');
32-
$collections[$key] = $this->normalizeArgument($collection, 'collection');
32+
$collections[$key] = $this->normalizeArgument($collection, 'Collection');
3333
}
3434

3535
$this->addCommand(new WithClause($collections));
@@ -50,11 +50,11 @@ public function with() : QueryBuilder
5050
*/
5151
public function traverse($fromVertex, $inDirection = 'outbound', $toVertex = null, $kShortestPaths = false) : QueryBuilder
5252
{
53-
$fromVertex = $this->normalizeArgument($fromVertex, 'id');
54-
$inDirection = $this->normalizeArgument($inDirection, 'direction');
53+
$fromVertex = $this->normalizeArgument($fromVertex, 'Id');
54+
$inDirection = $this->normalizeArgument($inDirection, 'Direction');
5555

5656
if ($toVertex !== null) {
57-
$toVertex = $this->normalizeArgument($toVertex, 'id');
57+
$toVertex = $this->normalizeArgument($toVertex, 'Id');
5858
}
5959

6060

@@ -105,7 +105,7 @@ public function kShortestPaths($fromVertex, $inDirection, $toVertex) : QueryBuil
105105
*/
106106
public function graph(string $graphName) : QueryBuilder
107107
{
108-
$graphName = $this->normalizeArgument($graphName, 'graph');
108+
$graphName = $this->normalizeArgument($graphName, 'Graph');
109109

110110
$this->addCommand(new GraphClause($graphName));
111111

src/API/hasMiscellaneousFunctions.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
namespace LaravelFreelancerNL\FluentAQL\API;
33

44
use LaravelFreelancerNL\FluentAQL\Expressions\FunctionExpression;
5-
use LaravelFreelancerNL\FluentAQL\Expressions\KeyExpression;
65
use LaravelFreelancerNL\FluentAQL\Expressions\ListExpression;
7-
use LaravelFreelancerNL\FluentAQL\Expressions\LiteralExpression;
86

97
/**
108
* Trait hasFunctions
@@ -21,7 +19,7 @@ trait hasMiscellaneousFunctions
2119
* @link https://www.arangodb.com/docs/stable/aql/functions-miscellaneous.html#document
2220
*
2321
* @param $collection
24-
* @param null|string|array|KeyExpression|ListExpression $id
22+
* @param null|string|array|ListExpression $id
2523
* @return FunctionExpression
2624
*/
2725
public function document($collection, $id = null)
@@ -34,9 +32,9 @@ public function document($collection, $id = null)
3432
}
3533

3634
if ($collection !== null) {
37-
$arguments['collection'] = $this->normalizeArgument($collection, ['collection', 'id']);
35+
$arguments['collection'] = $this->normalizeArgument($collection, ['Collection', 'Id']);
3836
}
39-
$arguments['id'] = $this->normalizeArgument($id, ['query', 'list', 'key', 'id']);
37+
$arguments['id'] = $this->normalizeArgument($id, ['Id', 'Key', 'Query', 'List']);
4038

4139
return new FunctionExpression('DOCUMENT', $arguments);
4240
}

src/API/hasQueryClauses.php

+40-24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use LaravelFreelancerNL\FluentAQL\Clauses\CollectClause;
66
use LaravelFreelancerNL\FluentAQL\Clauses\FilterClause;
77
use LaravelFreelancerNL\FluentAQL\Clauses\GroupClause;
8-
use LaravelFreelancerNL\FluentAQL\Clauses\InClause;
98
use LaravelFreelancerNL\FluentAQL\Clauses\KeepClause;
109
use LaravelFreelancerNL\FluentAQL\Clauses\LimitClause;
1110
use LaravelFreelancerNL\FluentAQL\Clauses\OptionsClause;
@@ -14,9 +13,7 @@
1413
use LaravelFreelancerNL\FluentAQL\Clauses\ReturnClause;
1514
use LaravelFreelancerNL\FluentAQL\Clauses\SearchClause;
1615
use LaravelFreelancerNL\FluentAQL\Clauses\SortClause;
17-
use LaravelFreelancerNL\FluentAQL\Clauses\WithClause;
1816
use LaravelFreelancerNL\FluentAQL\Clauses\WithCountClause;
19-
use LaravelFreelancerNL\FluentAQL\Expressions\NullExpression;
2017
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
2118

2219
/**
@@ -33,20 +30,31 @@ trait hasQueryClauses
3330
* You HAVE TO prepare user input yourself or be open to injection attacks.
3431
*
3532
* @param string $aql
36-
* @param null $bindings
37-
* @param null $collections
38-
* @return $this
33+
* @param null $binds
34+
* @param array|null $collections
35+
* @return QueryBuilder
3936
*/
40-
public function raw(string $aql, $bindings = [], $collections = []) : QueryBuilder
37+
public function raw(string $aql, $binds = null, $collections = null) : QueryBuilder
4138
{
39+
if (is_array($binds)) {
40+
foreach ($binds as $key => $value) {
41+
$this->bind($value, $key);
42+
}
43+
}
44+
if (is_array($binds)) {
45+
foreach ($collections as $mode => $modeCollections) {
46+
$this->registerCollections($modeCollections, $mode);
47+
}
48+
}
49+
4250
$this->addCommand(new RawClause($aql));
4351

4452
return $this;
4553
}
4654

4755
public function options($options) : QueryBuilder
4856
{
49-
$options = $this->normalizeArgument($options, 'document');
57+
$options = $this->normalizeArgument($options, 'Object');
5058

5159
$this->addCommand(new OptionsClause($options));
5260

@@ -56,7 +64,7 @@ public function options($options) : QueryBuilder
5664

5765
/**
5866
* Create a for clause
59-
* @link https://www.arangodb.com/docs/3.4/aql/operations-for.html
67+
* @link https://www.arangodb.com/docs/stable/aql/operations-for.html
6068
*
6169
* @param string|array $variableName
6270
* @param mixed $in
@@ -69,11 +77,12 @@ public function for($variableName, $in = null) : QueryBuilder
6977
}
7078

7179
foreach ($variableName as $key => $value) {
72-
$variableName[$key] = $this->normalizeArgument($value, 'variable');
80+
$variableName[$key] = $this->normalizeArgument($value, 'Variable');
81+
$this->registerVariable($variableName[$key]);
7382
}
7483

7584
if ($in !== null) {
76-
$in = $this->normalizeArgument($in, ['collection', 'range', 'list', 'query']);
85+
$in = $this->normalizeArgument($in, ['Collection', 'Range', 'List', 'Query']);
7786
}
7887

7988
$this->addCommand(new ForClause($variableName, $in));
@@ -142,10 +151,10 @@ public function search($attribute, $comparisonOperator = '==', $value = null, $l
142151
public function collect($variableName = null, $expression = null) : QueryBuilder
143152
{
144153
if (isset($variableName)) {
145-
$variableName = $this->normalizeArgument($variableName, 'variable');
154+
$variableName = $this->normalizeArgument($variableName, 'Variable');
146155
}
147156
if (isset($expression)) {
148-
$expression = $this->normalizeArgument($expression, ['attribute', 'function', 'query', 'bind']);
157+
$expression = $this->normalizeArgument($expression, ['VariableAttribute', 'Function', 'Query', 'Bind']);
149158
}
150159

151160
$this->addCommand(new CollectClause($variableName, $expression));
@@ -164,9 +173,11 @@ public function collect($variableName = null, $expression = null) : QueryBuilder
164173
*/
165174
public function group($groupsVariable, $projectionExpression = null) : QueryBuilder
166175
{
167-
$groupsVariable = $this->normalizeArgument($groupsVariable, 'variable');
176+
$groupsVariable = $this->normalizeArgument($groupsVariable, 'Variable');
177+
$this->registerVariable($groupsVariable);
178+
168179
if (isset($projectionExpression)) {
169-
$projectionExpression = $this->normalizeArgument($projectionExpression, ['attribute', 'document', 'function', 'query', 'bind']);
180+
$projectionExpression = $this->normalizeArgument($projectionExpression, ['VariableAttribute', 'Object', 'Function', 'Query', 'Bind']);
170181
}
171182

172183
$this->addCommand(new GroupClause($groupsVariable, $projectionExpression));
@@ -184,7 +195,8 @@ public function group($groupsVariable, $projectionExpression = null) : QueryBuil
184195
*/
185196
public function keep($keepVariable) : QueryBuilder
186197
{
187-
$keepVariable = $this->normalizeArgument($keepVariable, 'variable');
198+
$keepVariable = $this->normalizeArgument($keepVariable, 'Variable');
199+
$this->registerVariable($keepVariable);
188200

189201
$this->addCommand(new KeepClause($keepVariable));
190202

@@ -203,7 +215,8 @@ public function keep($keepVariable) : QueryBuilder
203215
*/
204216
public function withCount($countVariableName) : QueryBuilder
205217
{
206-
$countVariableName = $this->normalizeArgument($countVariableName, 'variable');
218+
$countVariableName = $this->normalizeArgument($countVariableName, 'Variable');
219+
$this->registerVariable($countVariableName);
207220

208221
$this->addCommand(new WithCountClause($countVariableName));
209222

@@ -221,8 +234,10 @@ public function withCount($countVariableName) : QueryBuilder
221234
*/
222235
public function aggregate($variableName, $aggregateExpression) : QueryBuilder
223236
{
224-
$variableName = $this->normalizeArgument($variableName, 'variable');
225-
$aggregateExpression = $this->normalizeArgument($aggregateExpression, 'attribute', 'function', 'query', 'bind');
237+
$variableName = $this->normalizeArgument($variableName, 'Variable');
238+
$this->registerVariable($variableName);
239+
240+
$aggregateExpression = $this->normalizeArgument($aggregateExpression, ['VariableAttribute', 'Function', 'Query', 'Bind']);
226241

227242
$this->addCommand(new AggregateClause($variableName, $aggregateExpression));
228243

@@ -261,15 +276,14 @@ public function sort($sortBy = null, $direction = null) : QueryBuilder
261276
* Limit results
262277
* @link https://www.arangodb.com/docs/stable/aql/operations-limit.html
263278
*
264-
* @param $offsetOrCount
265-
* @param null $count
279+
* @param int $offsetOrCount
280+
* @param int $count
266281
* @return $this
267282
*/
268-
public function limit($offsetOrCount, $count = null)
283+
public function limit(int $offsetOrCount, int $count = null)
269284
{
270-
$offsetOrCount = $this->normalizeArgument($offsetOrCount, 'numeric');
271285
if ($count !== null) {
272-
$count = $this->normalizeArgument($count, 'numeric');
286+
$count = (int) $count;
273287
}
274288

275289
$this->addCommand(new LimitClause($offsetOrCount, $count));
@@ -287,6 +301,8 @@ public function limit($offsetOrCount, $count = null)
287301
*/
288302
public function return($expression, $distinct = false) : QueryBuilder
289303
{
304+
$expression = $this->normalizeArgument($expression, ['Variable', 'VariableAttribute', 'Object', 'Function', 'Query', 'Bind']);
305+
290306
$this->addCommand(new ReturnClause($expression, $distinct));
291307

292308
return $this;

src/API/hasStatementClauses.php

+24-22
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@ trait hasStatementClauses
1919
{
2020
/**
2121
* Assign a value to a variable.
22-
* @link https://www.arangodb.com/docs/3.4/aql/operations-let.html
22+
* @link https://www.arangodb.com/docs/stable/aql/operations-let.html
2323
*
2424
* @param $variableName
2525
* @param $expression
2626
* @return $this
2727
*/
2828
public function let($variableName, $expression)
2929
{
30-
$variableName = $this->normalizeArgument($variableName, 'variable');
31-
$expression = $this->normalizeArgument($expression, ['query', 'list', 'range', 'numeric', 'bind']);
30+
$variableName = $this->normalizeArgument($variableName, 'Variable');
31+
$this->registerVariable($variableName);
32+
33+
$expression = $this->normalizeArgument($expression, ['List', 'Object', 'Query', 'Range', 'Number', 'Bind']);
3234

3335
$this->addCommand(new LetClause($variableName, $expression));
3436

@@ -37,18 +39,18 @@ public function let($variableName, $expression)
3739

3840
/**
3941
* Insert a document in a collection
40-
* @link https://www.arangodb.com/docs/3.4/aql/operations-insert.html
42+
* @link https://www.arangodb.com/docs/stable/aql/operations-insert.html
4143
*
4244
* @param $document
4345
* @param string $collection
4446
* @return QueryBuilder
4547
*/
4648
public function insert($document, string $collection) : QueryBuilder
4749
{
48-
$document = $this->normalizeArgument($document, ['key', 'variable', 'bind']);
50+
$document = $this->normalizeArgument($document, ['Key', 'Variable', 'Bind']);
4951

5052
$this->registerCollections($collection);
51-
$collection = $this->normalizeArgument($collection, ['collection', 'bind']);
53+
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);
5254

5355
$this->addCommand(new InsertClause($document, $collection));
5456

@@ -57,7 +59,7 @@ public function insert($document, string $collection) : QueryBuilder
5759

5860
/**
5961
* Update a document in a collection with the supplied data
60-
* @link https://www.arangodb.com/docs/3.4/aql/operations-update.html
62+
* @link https://www.arangodb.com/docs/stable/aql/operations-update.html
6163
*
6264
* @param $document
6365
* @param $with
@@ -66,10 +68,10 @@ public function insert($document, string $collection) : QueryBuilder
6668
*/
6769
public function update($document, $with, $collection) : QueryBuilder
6870
{
69-
$document = $this->normalizeArgument($document, ['key', 'variable', 'bind']);
70-
$with = $this->normalizeArgument($with, ['numeric', 'bind']);
71+
$document = $this->normalizeArgument($document, ['Key', 'Variable', 'Bind']);
72+
$with = $this->normalizeArgument($with, ['Number', 'Bind']);
7173
$this->registerCollections($collection);
72-
$collection = $this->normalizeArgument($collection, ['collection', 'bind']);
74+
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);
7375

7476
$this->addCommand(new UpdateClause($document, $with, $collection));
7577

@@ -78,7 +80,7 @@ public function update($document, $with, $collection) : QueryBuilder
7880

7981
/**
8082
* Replace a document in a collection with the supplied data
81-
* @link https://www.arangodb.com/docs/3.4/aql/operations-replace.html
83+
* @link https://www.arangodb.com/docs/stable/aql/operations-replace.html
8284
*
8385
* @param $document
8486
* @param $with
@@ -87,10 +89,10 @@ public function update($document, $with, $collection) : QueryBuilder
8789
*/
8890
public function replace($document, $with, string $collection) : QueryBuilder
8991
{
90-
$document = $this->normalizeArgument($document, ['key', 'variable', 'bind']);
91-
$with = $this->normalizeArgument($with, ['numeric', 'bind']);
92+
$document = $this->normalizeArgument($document, ['Key', 'Variable', 'Bind']);
93+
$with = $this->normalizeArgument($with, ['Number', 'Bind']);
9294
$this->registerCollections($collection);
93-
$collection = $this->normalizeArgument($collection, ['collection', 'bind']);
95+
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);
9496

9597
$this->addCommand(new ReplaceClause($document, $with, $collection));
9698

@@ -99,7 +101,7 @@ public function replace($document, $with, string $collection) : QueryBuilder
99101

100102
/**
101103
* Update, replace or insert documents in a collection with the supplied data.
102-
* @link https://www.arangodb.com/docs/3.4/aql/operations-upsert.html
104+
* @link https://www.arangodb.com/docs/stable/aql/operations-upsert.html
103105
*
104106
* @param mixed $search
105107
* @param mixed $insert
@@ -110,11 +112,11 @@ public function replace($document, $with, string $collection) : QueryBuilder
110112
*/
111113
public function upsert($search, $insert, $with, string $collection, bool $replace = false) : QueryBuilder
112114
{
113-
$search = $this->normalizeArgument($search, ['key', 'variable', 'bind']);
114-
$insert = $this->normalizeArgument($insert, ['key', 'variable', 'bind']);
115-
$with = $this->normalizeArgument($with, ['numeric', 'bind']);
115+
$search = $this->normalizeArgument($search, ['Key', 'Variable', 'Bind']);
116+
$insert = $this->normalizeArgument($insert, ['Key', 'Variable', 'Bind']);
117+
$with = $this->normalizeArgument($with, ['Number', 'Bind']);
118+
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);
116119
$this->registerCollections($collection);
117-
$collection = $this->normalizeArgument($collection, ['collection', 'bind']);
118120

119121
$this->addCommand(new UpsertClause($search, $insert, $with, $collection, $replace));
120122

@@ -123,17 +125,17 @@ public function upsert($search, $insert, $with, string $collection, bool $replac
123125

124126
/**
125127
* Remove a document from a collection
126-
* @link https://www.arangodb.com/docs/3.4/aql/operations-remove.html
128+
* @link https://www.arangodb.com/docs/stable/aql/operations-remove.html
127129
*
128130
* @param mixed $document
129131
* @param string $collection
130132
* @return QueryBuilder
131133
*/
132134
public function remove($document, string $collection) : QueryBuilder
133135
{
134-
$document = $this->normalizeArgument($document, ['key', 'variable', 'bind']);
136+
$document = $this->normalizeArgument($document, ['Key', 'Variable', 'Bind']);
135137
$this->registerCollections($collection);
136-
$collection = $this->normalizeArgument($collection, ['collection', 'bind']);
138+
$collection = $this->normalizeArgument($collection, ['Collection', 'Bind']);
137139

138140
$this->addCommand(new RemoveClause($document, $collection));
139141

src/Clauses/AggregateClause.php

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<?php
22
namespace LaravelFreelancerNL\FluentAQL\Clauses;
33

4-
use LaravelFreelancerNL\FluentAQL\Expressions\ExpressionInterface;
5-
use LaravelFreelancerNL\FluentAQL\Expressions\VariableExpression;
6-
74
class AggregateClause extends Clause
85
{
96
protected $variableName;

src/Clauses/CollectClause.php

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
namespace LaravelFreelancerNL\FluentAQL\Clauses;
33

44
use LaravelFreelancerNL\FluentAQL\Expressions\ExpressionInterface;
5-
use LaravelFreelancerNL\FluentAQL\Expressions\VariableExpression;
65

76
class CollectClause extends Clause
87
{

0 commit comments

Comments
 (0)