Completed
Push — master ( ac8ec4...1a57ac )
by Kévin
04:37 queued 11s
created

Doctrine/Common/Filter/ExistsFilterTrait.php (1 issue)

Labels
Severity
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\Common\Filter;
15
16
use ApiPlatform\Core\Bridge\Doctrine\Common\PropertyHelperTrait;
17
use ApiPlatform\Core\Exception\InvalidArgumentException;
18
use Psr\Log\LoggerInterface;
19
20
/**
21
 * Trait for filtering the collection by whether a property value exists or not.
22
 *
23
 * @author Teoh Han Hui <[email protected]>
24
 * @author Alan Poulain <[email protected]>
25
 */
26
trait ExistsFilterTrait
27
{
28
    use PropertyHelperTrait;
29
30
    /**
31
     * @var string Keyword used to retrieve the value
32
     */
33
    private $existsParameterName;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getDescription(string $resourceClass): array
39
    {
40
        $description = [];
41
42
        $properties = $this->getProperties();
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, true) || !$this->isNullableField($property, $resourceClass)) {
49
                continue;
50
            }
51
            $propertyName = $this->normalizePropertyName($property);
52
            $description[sprintf('%s[%s]', $this->existsParameterName, $propertyName)] = [
53
                'property' => $propertyName,
54
                'type' => 'bool',
55
                'required' => false,
56
            ];
57
        }
58
59
        return $description;
60
    }
61
62
    /**
63
     * Determines whether the given property refers to a nullable field.
64
     */
65
    abstract protected function isNullableField(string $property, string $resourceClass): bool;
66
67
    abstract protected function getProperties(): ?array;
68
69
    abstract protected function getLogger(): LoggerInterface;
70
71
    abstract protected function normalizePropertyName($property);
72
73
    private function normalizeValue($value, string $property): ?bool
74
    {
75
        if (\is_array($value) && isset($value[self::QUERY_PARAMETER_KEY])) {
0 ignored issues
show
The constant ApiPlatform\Core\Bridge\...it::QUERY_PARAMETER_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
76
            @trigger_error(
77
                sprintf('The ExistsFilter syntax "%s[exists]=true/false" is deprecated since 2.5. Use the syntax "%s[%s]=true/false" instead.', $property, $this->existsParameterName, $property),
78
                E_USER_DEPRECATED
79
            );
80
            $value = $value[self::QUERY_PARAMETER_KEY];
81
        }
82
83
        if (\in_array($value, [true, 'true', '1', '', null], true)) {
84
            return true;
85
        }
86
87
        if (\in_array($value, [false, 'false', '0'], true)) {
88
            return false;
89
        }
90
91
        $this->getLogger()->notice('Invalid filter ignored', [
92
            'exception' => new InvalidArgumentException(sprintf('Invalid value for "%s[%s]", expected one of ( "%s" )', $this->existsParameterName, $property, implode('" | "', [
93
                'true',
94
                'false',
95
                '1',
96
                '0',
97
            ]))),
98
        ]);
99
100
        return null;
101
    }
102
}
103