Completed
Push — master ( 906aaa...a12ac4 )
by Han Hui
26s queued 13s
created

src/Bridge/Doctrine/Orm/Filter/NumericFilter.php (2 issues)

1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Filter;
15
16
use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\NumericFilterTrait;
17
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
18
use Doctrine\DBAL\Types\Type as DBALType;
19
use Doctrine\ORM\QueryBuilder;
20
21
/**
22
 * Filters the collection by numeric values.
23
 *
24
 * Filters collection by equality of numeric properties.
25
 *
26
 * For each property passed, if the resource does not have such property or if
27
 * the value is not numeric, the property is ignored.
28
 *
29
 * @author Amrouche Hamza <[email protected]>
30
 * @author Teoh Han Hui <[email protected]>
31
 */
32
class NumericFilter extends AbstractContextAwareFilter
33
{
34
    use NumericFilterTrait;
35
36
    /**
37
     * Type of numeric in Doctrine.
38
     *
39
     * @see http://doctrine-orm.readthedocs.org/projects/doctrine-dbal/en/latest/reference/types.html
40
     */
41
    public const DOCTRINE_NUMERIC_TYPES = [
42
        DBALType::BIGINT => true,
43
        DBALType::DECIMAL => true,
44
        DBALType::FLOAT => true,
45
        DBALType::INTEGER => true,
46
        DBALType::SMALLINT => true,
47
    ];
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
53
    {
54
        if (
55
            !$this->isPropertyEnabled($property, $resourceClass) ||
56
            !$this->isPropertyMapped($property, $resourceClass) ||
57
            !$this->isNumericField($property, $resourceClass)
58
        ) {
59
            return;
60
        }
61
62
        $values = $this->normalizeValues($value, $property);
63
        if (null === $values) {
64
            return;
65
        }
66
67
        $alias = $queryBuilder->getRootAliases()[0];
68
        $field = $property;
69
70
        if ($this->isPropertyNested($property, $resourceClass)) {
71
            [$alias, $field] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass);
72
        }
73
74
        $valueParameter = $queryNameGenerator->generateParameterName($field);
75
76
        if (1 === \count($values)) {
77
            $queryBuilder
78
                ->andWhere(sprintf('%s.%s = :%s', $alias, $field, $valueParameter))
79
                ->setParameter($valueParameter, $values[0], (string) $this->getDoctrineFieldType($property, $resourceClass));
80
        } else {
81
            $queryBuilder
82
                ->andWhere(sprintf('%s.%s IN (:%s)', $alias, $field, $valueParameter))
83
                ->setParameter($valueParameter, $values);
84
        }
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    protected function getType(string $doctrineType = null): string
91
    {
92
        if (null === $doctrineType || DBALType::DECIMAL === $doctrineType) {
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::DECIMAL has been deprecated: Use {@see DefaultTypes::DECIMAL} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

92
        if (null === $doctrineType || /** @scrutinizer ignore-deprecated */ DBALType::DECIMAL === $doctrineType) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
93
            return 'string';
94
        }
95
96
        if (DBALType::FLOAT === $doctrineType) {
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\DBAL\Types\Type::FLOAT has been deprecated: Use {@see DefaultTypes::FLOAT} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

96
        if (/** @scrutinizer ignore-deprecated */ DBALType::FLOAT === $doctrineType) {

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
97
            return 'float';
98
        }
99
100
        return 'int';
101
    }
102
}
103