Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
104 additions
and
124 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,101 @@ | ||
<?php | ||
|
||
namespace Pushword\Core\Repository; | ||
|
||
use Doctrine\ORM\Query\Expr\Andx; | ||
use Doctrine\ORM\Query\Expr\Orx; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
/** | ||
* Eg: | ||
* ['title', 'LIKE' '%this%'] => works | ||
* [['title', 'LIKE' '%this%']] => works | ||
* [['title', 'LIKE' '%this%'], 'OR', ['title', 'LIKE' '%that%']] => works | ||
* [[['title', 'LIKE' '%this%'], ['title', 'LIKE' '%this%']], 'OR', ['title', 'LIKE' '%that%']] => works | ||
*/ | ||
class FilterWhereParser | ||
{ | ||
/** | ||
* @param array<mixed> $where | ||
*/ | ||
public function __construct( | ||
private QueryBuilder $queryBuilder, | ||
private array $where | ||
) { | ||
} | ||
|
||
public function parseAndAdd(): QueryBuilder | ||
{ | ||
if ([] === $this->where) { | ||
return $this->queryBuilder; | ||
} | ||
|
||
// Normalize array ['] | ||
if (! isset($this->where[0]) || ! \is_array($this->where[0])) { // eg : ['key' => 'test'...] or ['test', ...] | ||
$this->where = [$this->where]; | ||
} | ||
|
||
return $this->queryBuilder->andWhere($this->retrieveFrom($this->where)); | ||
} | ||
|
||
/** | ||
* @param array<mixed> $where | ||
*/ | ||
private function containsSubQuery(array $where): bool | ||
{ | ||
return \is_array(array_values($where)[0] ?? throw new \Exception()); | ||
} | ||
|
||
/** | ||
* @param array<mixed> $where | ||
*/ | ||
private function retrieveFrom(array $where): Andx|Orx | ||
{ | ||
$compose = \in_array('OR', $where, true) ? $this->queryBuilder->expr()->orX() : $this->queryBuilder->expr()->andX(); | ||
foreach ($where as $singleWhere) { | ||
if ('OR' === $singleWhere) { | ||
continue; | ||
} | ||
|
||
if (! \is_array($singleWhere)) { | ||
throw new \Exception('malformated where params'); | ||
} | ||
|
||
if ($this->containsSubQuery($singleWhere)) { | ||
$compose->add($this->retrieveFrom($singleWhere)); | ||
|
||
continue; | ||
} | ||
|
||
$compose->add($this->retrieveExpressionFrom($singleWhere)); | ||
} | ||
|
||
return $compose; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $whereRow | ||
*/ | ||
private function retrieveExpressionFrom(array $whereRow): string | ||
{ | ||
$paramKey = 'm'.md5('a'.random_int(0, mt_getrandmax())); | ||
|
||
$prefix = $whereRow['key_prefix'] ?? $whereRow[4] ?? 'p.'; | ||
$key = $whereRow['key'] ?? $whereRow[0] ?? throw new \Exception('key was forgotten'); | ||
$operator = $whereRow['operator'] ?? $whereRow[1] ?? throw new \Exception('operator was forgotten'); | ||
$sqlValue = 'IN' === $operator ? '( :'.$paramKey.')' : ' :'.$paramKey; | ||
$value = $whereRow['value'] ?? $whereRow[2]; | ||
|
||
if (null === $value) { | ||
if (! \in_array($operator, ['IS', 'IS NOT'], true)) { | ||
throw new \Exception('operator `'.$operator.'` forbidden for null value'); | ||
} | ||
|
||
return $prefix.$key.' '.$operator.' NULL'; | ||
} | ||
|
||
$this->queryBuilder->setParameter($paramKey, $value); | ||
|
||
return $prefix.$key.' '.$operator.' '.$sqlValue; | ||
} | ||
} |
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
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
Binary file not shown.