Skip to content

Commit

Permalink
Added prune variable support
Browse files Browse the repository at this point in the history
  • Loading branch information
LaravelFreelancerNL committed May 15, 2022
1 parent 3fe8feb commit 051b6e1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/AQL/HasGraphClauses.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,15 @@ public function prune(
object|array|string|int|float|bool|null $leftOperand,
string|QueryBuilder|Expression $comparisonOperator = null,
object|array|string|int|float|bool|null $rightOperand = null,
string|QueryBuilder|Expression $logicalOperator = null
string|QueryBuilder|Expression $logicalOperator = null,
string $pruneVariable = null
): self {
$predicates = $leftOperand;
if (! is_array($predicates)) {
$predicates = [[$leftOperand, $comparisonOperator, $rightOperand, $logicalOperator]];
}

$this->addCommand(new PruneClause($predicates));
$this->addCommand(new PruneClause($predicates, $pruneVariable));

return $this;
}
Expand Down
27 changes: 26 additions & 1 deletion src/Clauses/PruneClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,41 @@

namespace LaravelFreelancerNL\FluentAQL\Clauses;

use LaravelFreelancerNL\FluentAQL\Expressions\PredicateExpression;
use LaravelFreelancerNL\FluentAQL\QueryBuilder;

class PruneClause extends FilterClause
{
protected ?string $pruneVariable = null;

/**
* Filter statement.
*
* @param array<mixed> $predicates
*/
public function __construct(
array|PredicateExpression $predicates,
string $pruneVariable = null
) {
parent::__construct($predicates);

$this->pruneVariable = $pruneVariable;
}


public function compile(QueryBuilder $queryBuilder): string
{
$aql = 'PRUNE ';

if (isset($this->pruneVariable)) {
$pruneVariable = $queryBuilder->normalizeArgument($this->pruneVariable, 'Variable');
$aql .= $pruneVariable->compile($queryBuilder) . ' = ';
}

$this->predicates = $queryBuilder->normalizePredicates($this->predicates);

$compiledPredicates = $queryBuilder->compilePredicates($this->predicates);

return 'PRUNE ' . rtrim($compiledPredicates);
return $aql . rtrim($compiledPredicates);
}
}
24 changes: 24 additions & 0 deletions tests/Unit/AQL/GraphClausesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,28 @@ public function testPruneClauseWithMultiplePredicates()
->get();
self::assertEquals('FOR u IN Users PRUNE u.active == true AND u.age > 18', $result->query);
}

/**
* @covers \LaravelFreelancerNL\FluentAQL\Clauses\PruneClause
*/
public function testPruneClauseWithVariable()
{
$result = (new QueryBuilder())
->for('u', 'users')
->prune('u.active', '==', 'true', null, 'pruneCondition')
->get();
self::assertEquals('FOR u IN users PRUNE pruneCondition = u.active == true', $result->query);

$result = (new QueryBuilder())
->for('u', 'Users')
->prune('u.active', '==', 'true', 'OR', 'pruneCondition')
->get();
self::assertEquals('FOR u IN Users PRUNE pruneCondition = u.active == true', $result->query);

$result = (new QueryBuilder())
->for('u', 'Users')
->prune(['u.active', '==', 'true'], pruneVariable: 'pruneCondition')
->get();
self::assertEquals('FOR u IN Users PRUNE pruneCondition = u.active == true', $result->query);
}
}

0 comments on commit 051b6e1

Please sign in to comment.