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
11 changed files
with
554 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,89 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Filter; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition\AndCondition; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition\EqCondition; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition\GtCondition; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition\GteCondition; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition\LtCondition; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition\LteCondition; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition\NeqCondition; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition\OrCondition; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Where; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
final class WhereFilter implements ContextAwareFilterInterface | ||
{ | ||
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = []) | ||
{ | ||
if (null === $filters = $context['filters']) { | ||
return; | ||
} | ||
|
||
if (null === $whereData = $filters['where']) { | ||
return; | ||
} | ||
|
||
$rootAlias = $queryBuilder->getRootAliases()[0]; | ||
$where = $this->buildWhereFromArray($rootAlias, $whereData); | ||
|
||
$where->apply($queryBuilder); | ||
} | ||
|
||
public function getDescription(string $resourceClass): array | ||
{ | ||
return []; // TODO | ||
} | ||
|
||
public function buildWhereFromArray(string $alias, array $whereArray): Where | ||
{ | ||
$where = new Where(); | ||
foreach ($whereArray as $conditionType => $conditionArray) { | ||
$type = array_keys($conditionArray)[0]; | ||
|
||
switch ($type) { | ||
case AndCondition::TYPE: | ||
$conditions = $this->buildWhereFromArray($alias, $conditionArray)->getConditions(); | ||
$where->addCondition(new AndCondition($conditions)); | ||
break; | ||
case OrCondition::TYPE: | ||
$conditions = $this->buildWhereFromArray($alias, $conditionArray)->getConditions(); | ||
$where->addCondition(new OrCondition($conditions)); | ||
break; | ||
case EqCondition::TYPE: | ||
$where->addCondition(new EqCondition($alias.'.'.$conditionType, (array) $conditionArray[$type])); | ||
break; | ||
case NeqCondition::TYPE: | ||
$where->addCondition(new NeqCondition($alias.'.'.$conditionType, (array) $conditionArray[$type])); | ||
break; | ||
case GtCondition::TYPE: | ||
$where->addCondition(new GtCondition($alias.'.'.$conditionType, (array) $conditionArray[$type])); | ||
break; | ||
case GteCondition::TYPE: | ||
$where->addCondition(new GteCondition($alias.'.'.$conditionType, (array) $conditionArray[$type])); | ||
break; | ||
case LtCondition::TYPE: | ||
$where->addCondition(new LtCondition($alias.'.'.$conditionType, (array) $conditionArray[$type])); | ||
break; | ||
case LteCondition::TYPE: | ||
$where->addCondition(new LteCondition($alias.'.'.$conditionType, (array) $conditionArray[$type])); | ||
break; | ||
} | ||
} | ||
|
||
return $where; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Bridge/Doctrine/Orm/Filter/WhereFilter/Condition/AndCondition.php
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,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition; | ||
|
||
use Doctrine\ORM\Query\Expr; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
final class AndCondition implements ConditionInterface | ||
{ | ||
public const TYPE = 'and'; | ||
|
||
/** | ||
* @var ConditionInterface[] | ||
*/ | ||
private $conditions; | ||
|
||
public function __construct(array $conditions) | ||
{ | ||
$this->conditions = $conditions; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return self::TYPE; | ||
} | ||
|
||
public function apply(QueryBuilder $queryBuilder): Expr\Composite | ||
{ | ||
$and = $queryBuilder->expr()->andX(); | ||
|
||
foreach ($this->conditions as $condition) { | ||
$and->add($condition->apply($queryBuilder)); | ||
} | ||
|
||
return $and; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Bridge/Doctrine/Orm/Filter/WhereFilter/Condition/ConditionInterface.php
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,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition; | ||
|
||
use Doctrine\ORM\Query\Expr\Composite; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
interface ConditionInterface | ||
{ | ||
public function getType(): string; | ||
|
||
public function apply(QueryBuilder $queryBuilder): Composite; | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Bridge/Doctrine/Orm/Filter/WhereFilter/Condition/EqCondition.php
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,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition; | ||
|
||
use Doctrine\ORM\Query\Expr; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
final class EqCondition implements ConditionInterface | ||
{ | ||
public const TYPE = 'eq'; | ||
|
||
private $key; | ||
|
||
private $values; | ||
|
||
public function __construct(string $key, array $values) | ||
{ | ||
$this->key = $key; | ||
$this->values = $values; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return self::TYPE; | ||
} | ||
|
||
public function apply(QueryBuilder $queryBuilder): Expr\Composite | ||
{ | ||
$or = $queryBuilder->expr()->orX(); | ||
foreach ($this->values as $value) { | ||
$or->add( | ||
$queryBuilder->expr()->eq($this->key, $queryBuilder->expr()->literal($value)) | ||
); | ||
} | ||
|
||
return $or; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Bridge/Doctrine/Orm/Filter/WhereFilter/Condition/GtCondition.php
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,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition; | ||
|
||
use Doctrine\ORM\Query\Expr; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
final class GtCondition implements ConditionInterface | ||
{ | ||
public const TYPE = 'gt'; | ||
|
||
private $key; | ||
|
||
private $values; | ||
|
||
public function __construct(string $key, array $values) | ||
{ | ||
$this->key = $key; | ||
$this->values = $values; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return self::TYPE; | ||
} | ||
|
||
public function apply(QueryBuilder $queryBuilder): Expr\Composite | ||
{ | ||
$or = $queryBuilder->expr()->orX(); | ||
foreach ($this->values as $value) { | ||
$or->add( | ||
$queryBuilder->expr()->gt($this->key, $queryBuilder->expr()->literal($value)) | ||
); | ||
} | ||
|
||
return $or; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Bridge/Doctrine/Orm/Filter/WhereFilter/Condition/GteCondition.php
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,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition; | ||
|
||
use Doctrine\ORM\Query\Expr; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
final class GteCondition implements ConditionInterface | ||
{ | ||
public const TYPE = 'gte'; | ||
|
||
private $key; | ||
|
||
private $values; | ||
|
||
public function __construct(string $key, array $values) | ||
{ | ||
$this->key = $key; | ||
$this->values = $values; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return self::TYPE; | ||
} | ||
|
||
public function apply(QueryBuilder $queryBuilder): Expr\Composite | ||
{ | ||
$or = $queryBuilder->expr()->orX(); | ||
foreach ($this->values as $value) { | ||
$or->add( | ||
$queryBuilder->expr()->gte($this->key, $queryBuilder->expr()->literal($value)) | ||
); | ||
} | ||
|
||
return $or; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Bridge/Doctrine/Orm/Filter/WhereFilter/Condition/LtCondition.php
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,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\WhereFilter\Condition; | ||
|
||
use Doctrine\ORM\Query\Expr; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
final class LtCondition implements ConditionInterface | ||
{ | ||
public const TYPE = 'lt'; | ||
|
||
private $key; | ||
|
||
private $values; | ||
|
||
public function __construct(string $key, array $values) | ||
{ | ||
$this->key = $key; | ||
$this->values = $values; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return self::TYPE; | ||
} | ||
|
||
public function apply(QueryBuilder $queryBuilder): Expr\Composite | ||
{ | ||
$or = $queryBuilder->expr()->orX(); | ||
foreach ($this->values as $value) { | ||
$or->add( | ||
$queryBuilder->expr()->lt($this->key, $queryBuilder->expr()->literal($value)) | ||
); | ||
} | ||
|
||
return $or; | ||
} | ||
} |
Oops, something went wrong.