Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Executor/Eloquent/FilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract protected function execute($target, array $operators, array $parameters
public function applyFilter($target, array $parameters, array $operators, ExecutionContext $context)
{
/** @var QueryBuilder $query */
$query = !$target instanceof QueryBuilder ? $target->getQuery() : $target;
$query = !$target instanceof QueryBuilder && !$this->allowEloquentBuilderAsQuery ? $target->getQuery() : $target;
$sql = $this->execute($target, $operators, $parameters);

$query->whereRaw($sql, $parameters);
Expand Down
28 changes: 28 additions & 0 deletions src/Target/Eloquent/Eloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,33 @@

namespace RulerZ\Target\Eloquent;

use RulerZ\Compiler\Context;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;

use RulerZ\Target\AbstractSqlTarget;

class Eloquent extends AbstractSqlTarget
{
/**
* Allow eloquent builder as query.
*
* @var bool
*/
protected $allowEloquentBuilderAsQuery = false;

/**
* Constructor.
*
* @param bool $allowEloquentBuilderAsQuery Whether to allow the execution target to be eloquent builder instead of query builder.
*/
public function __construct($allowEloquentBuilderAsQuery = false)
{
parent::__construct();

$this->allowEloquentBuilderAsQuery = (bool) $allowEloquentBuilderAsQuery;
}

/**
* @inheritDoc
*/
Expand All @@ -27,4 +47,12 @@ protected function getExecutorTraits()
'\RulerZ\Executor\Polyfill\FilterBasedSatisfaction',
];
}

/**
* @inheritDoc
*/
protected function createVisitor(Context $context)
{
return new EloquentVisitor($context, $this->getOperators(), $this->allowStarOperator, $this->allowEloquentBuilderAsQuery);
}
}
40 changes: 40 additions & 0 deletions src/Target/Eloquent/EloquentVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace RulerZ\Target\Eloquent;

use Hoa\Ruler\Model as AST;

use RulerZ\Compiler\Context;
use RulerZ\Target\GenericSqlVisitor;
use RulerZ\Target\Operators\Definitions as OperatorsDefinitions;

class EloquentVisitor extends GenericSqlVisitor
{
/**
* Allow eloquent builder as query.
*
* @var bool
*/
protected $allowEloquentBuilderAsQuery = false;

public function __construct(
Context $context,
OperatorsDefinitions $operators,
$allowStarOperator = true,
$allowEloquentBuilderAsQuery = false
) {
parent::__construct($context, $operators, $allowStarOperator);

$this->allowEloquentBuilderAsQuery = (bool) $allowEloquentBuilderAsQuery;
}

/**
* @inheritDoc
*/
public function getCompilationData()
{
return [
'allowEloquentBuilderAsQuery ' => $this->allowEloquentBuilderAsQuery,
];
}
}
1 change: 1 addition & 0 deletions tests/stub/Executor/EloquentExecutorStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class EloquentExecutorStub
{
public static $executeReturn;
protected $allowEloquentBuilderAsQuery = false;

use FilterTrait;

Expand Down