-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and use behavior
ExpressionInjector
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Icinga\Module\X509\Model\Behavior; | ||
|
||
use ipl\Orm\Contract\QueryAwareBehavior; | ||
use ipl\Orm\Contract\RewriteFilterBehavior; | ||
use ipl\Orm\Query; | ||
use ipl\Stdlib\Filter; | ||
|
||
class ExpressionInjector implements RewriteFilterBehavior, QueryAwareBehavior | ||
{ | ||
/** @var array */ | ||
protected $columns; | ||
|
||
/** @var Query */ | ||
protected $query; | ||
|
||
public function __construct(...$columns) | ||
{ | ||
$this->columns = $columns; | ||
} | ||
|
||
public function setQuery(Query $query) | ||
{ | ||
$this->query = $query; | ||
|
||
return $this; | ||
} | ||
|
||
public function rewriteCondition(Filter\Condition $condition, $relation = null) | ||
{ | ||
$columnName = $condition->metaData()->get('columnName'); | ||
if (in_array($columnName, $this->columns, true)) { | ||
$relationPath = $condition->metaData()->get('relationPath'); | ||
if ($relationPath && $relationPath !== $this->query->getModel()->getTableAlias()) { | ||
$subject = $this->query->getResolver()->resolveRelation($relationPath)->getTarget(); | ||
} else { | ||
$subject = $this->query->getModel(); | ||
} | ||
|
||
$expression = clone $subject->getColumns()[$columnName]; | ||
$expression->setColumns($this->query->getResolver()->qualifyColumns( | ||
$this->query->getResolver()->requireAndResolveColumns( | ||
$expression->getColumns(), | ||
$subject | ||
), | ||
$subject | ||
)); | ||
|
||
$condition->setColumn($this->query->getDb()->getQueryBuilder()->buildExpression($expression)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters