Skip to content

Commit

Permalink
Merge 39ee809 into a12ac48
Browse files Browse the repository at this point in the history
  • Loading branch information
amenophis committed Nov 16, 2019
2 parents a12ac48 + 39ee809 commit 7bbb05c
Show file tree
Hide file tree
Showing 11 changed files with 554 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/Bridge/Doctrine/Orm/Filter/WhereFilter.php
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}

0 comments on commit 7bbb05c

Please sign in to comment.