Completed
Push — master ( 29b346...1faa7f )
by Amrouche
19s
created

src/Bridge/Doctrine/Orm/Filter/BooleanFilter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Orm\Util\QueryNameGeneratorInterface;
17
use ApiPlatform\Core\Exception\InvalidArgumentException;
18
use Doctrine\DBAL\Types\Type as DBALType;
19
use Doctrine\ORM\QueryBuilder;
20
21
/**
22
 * Filters the collection by boolean values.
23
 *
24
 * Filters collection on equality of boolean properties. The value is specified
25
 * as one of ( "true" | "false" | "1" | "0" ) in the query.
26
 *
27
 * For each property passed, if the resource does not have such property or if
28
 * the value is not one of ( "true" | "false" | "1" | "0" ) the property is ignored.
29
 *
30
 * @author Amrouche Hamza <[email protected]>
31
 * @author Teoh Han Hui <[email protected]>
32
 */
33
class BooleanFilter extends AbstractFilter
34
{
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getDescription(string $resourceClass): array
39
    {
40
        $description = [];
41
42
        $properties = $this->properties;
43
        if (null === $properties) {
44
            $properties = array_fill_keys($this->getClassMetadata($resourceClass)->getFieldNames(), null);
45
        }
46
47
        foreach ($properties as $property => $unused) {
48
            if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isBooleanField($property, $resourceClass)) {
49
                continue;
50
            }
51
52
            $description[$property] = [
53
                'property' => $property,
54
                'type' => 'bool',
55
                'required' => false,
56
            ];
57
        }
58
59
        return $description;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
66
    {
67 View Code Duplication
        if (
68
            !$this->isPropertyEnabled($property, $resourceClass) ||
69
            !$this->isPropertyMapped($property, $resourceClass) ||
70
            !$this->isBooleanField($property, $resourceClass)
71
        ) {
72
            return;
73
        }
74
75
        if (in_array($value, ['true', '1'], true)) {
76
            $value = true;
77
        } elseif (in_array($value, ['false', '0'], true)) {
78
            $value = false;
79 View Code Duplication
        } else {
80
            $this->logger->notice('Invalid filter ignored', [
81
                'exception' => new InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $property, implode('" | "', [
82
                    'true',
83
                    'false',
84
                    '1',
85
                    '0',
86
                ]))),
87
            ]);
88
89
            return;
90
        }
91
92
        $alias = 'o';
93
        $field = $property;
94
95 View Code Duplication
        if ($this->isPropertyNested($property, $resourceClass)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            list($alias, $field) = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass);
97
        }
98
99
        $valueParameter = $queryNameGenerator->generateParameterName($field);
100
101
        $queryBuilder
102
            ->andWhere(sprintf('%s.%s = :%s', $alias, $field, $valueParameter))
103
            ->setParameter($valueParameter, $value);
104
    }
105
106
    /**
107
     * Determines whether the given property refers to a boolean field.
108
     *
109
     * @param string $property
110
     * @param string $resourceClass
111
     *
112
     * @return bool
113
     */
114 View Code Duplication
    protected function isBooleanField(string $property, string $resourceClass): bool
115
    {
116
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
117
        $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
118
119
        return DBALType::BOOLEAN === $metadata->getTypeOfField($propertyParts['field']);
120
    }
121
}
122