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

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

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\PropertyHelperTrait;
17
use ApiPlatform\Core\Bridge\Doctrine\Orm\PropertyHelperTrait as OrmPropertyHelperTrait;
18
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
19
use ApiPlatform\Core\Util\RequestParser;
20
use Doctrine\Common\Persistence\ManagerRegistry;
21
use Doctrine\ORM\QueryBuilder;
22
use Psr\Log\LoggerInterface;
23
use Psr\Log\NullLogger;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\RequestStack;
26
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
27
28
/**
29
 * {@inheritdoc}
30
 *
31
 * Abstract class with helpers for easing the implementation of a filter.
32
 *
33
 * @author Kévin Dunglas <[email protected]>
34
 * @author Théo FIDRY <[email protected]>
35
 */
36
abstract class AbstractFilter implements FilterInterface
37
{
38
    use PropertyHelperTrait;
39
    use OrmPropertyHelperTrait;
40
41
    protected $managerRegistry;
42
    protected $requestStack;
43
    protected $logger;
44
    protected $properties;
45
    protected $nameConverter;
46
47
    public function __construct(ManagerRegistry $managerRegistry, ?RequestStack $requestStack = null, LoggerInterface $logger = null, array $properties = null, NameConverterInterface $nameConverter = null)
48
    {
49
        if (null !== $requestStack) {
50
            @trigger_error(sprintf('Passing an instance of "%s" is deprecated since 2.2. Use "filters" context key instead.', RequestStack::class), E_USER_DEPRECATED);
51
        }
52
53
        $this->managerRegistry = $managerRegistry;
54
        $this->requestStack = $requestStack;
55
        $this->logger = $logger ?? new NullLogger();
56
        $this->properties = $properties;
57
        $this->nameConverter = $nameConverter;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null/*, array $context = []*/)
64
    {
65
        @trigger_error(sprintf('Using "%s::apply()" is deprecated since 2.2. Use "%s::apply()" with the "filters" context key instead.', __CLASS__, AbstractContextAwareFilter::class), E_USER_DEPRECATED);
66
67
        if (null === $this->requestStack || null === $request = $this->requestStack->getCurrentRequest()) {
68
            return;
69
        }
70
71
        foreach ($this->extractProperties($request, $resourceClass) as $property => $value) {
72
            $this->filterProperty($property, $value, $queryBuilder, $queryNameGenerator, $resourceClass, $operationName);
73
        }
74
    }
75
76
    /**
77
     * Passes a property through the filter.
78
     */
79
    abstract protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null/*, array $context = []*/);
80
81
    protected function getManagerRegistry(): ManagerRegistry
82
    {
83
        return $this->managerRegistry;
84
    }
85
86
    protected function getProperties(): ?array
87
    {
88
        return $this->properties;
89
    }
90
91
    protected function getLogger(): LoggerInterface
92
    {
93
        return $this->logger;
94
    }
95
96
    /**
97
     * Determines whether the given property is enabled.
98
     */
99
    protected function isPropertyEnabled(string $property/*, string $resourceClass*/): bool
100
    {
101
        if (\func_num_args() > 1) {
102
            $resourceClass = func_get_arg(1);
103
        } else {
104
            if (__CLASS__ !== \get_class($this)) {
105
                $r = new \ReflectionMethod($this, __FUNCTION__);
106
                if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
107
                    @trigger_error(sprintf('Method %s() will have a second `$resourceClass` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.1.', __FUNCTION__), E_USER_DEPRECATED);
108
                }
109
            }
110
            $resourceClass = null;
111
        }
112
113
        if (null === $this->properties) {
114
            // to ensure sanity, nested properties must still be explicitly enabled
115
            return !$this->isPropertyNested($property, $resourceClass);
116
        }
117
118
        return \array_key_exists($property, $this->properties);
119
    }
120
121
    /**
122
     * Extracts properties to filter from the request.
123
     */
124
    protected function extractProperties(Request $request/*, string $resourceClass*/): array
125
    {
126
        @trigger_error(sprintf('The use of "%s::extractProperties()" is deprecated since 2.2. Use the "filters" key of the context instead.', __CLASS__), E_USER_DEPRECATED);
127
128
        $resourceClass = \func_num_args() > 1 ? (string) func_get_arg(1) : null;
129
        $needsFixing = false;
130
        if (null !== $this->properties) {
131
            foreach ($this->properties as $property => $value) {
132
                if (($this->isPropertyNested($property, $resourceClass) || $this->isPropertyEmbedded($property, $resourceClass)) && $request->query->has(str_replace('.', '_', $property))) {
0 ignored issues
show
It seems like $resourceClass can also be of type null; however, parameter $resourceClass of ApiPlatform\Core\Bridge\...r::isPropertyEmbedded() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

132
                if (($this->isPropertyNested($property, $resourceClass) || $this->isPropertyEmbedded($property, /** @scrutinizer ignore-type */ $resourceClass)) && $request->query->has(str_replace('.', '_', $property))) {
Loading history...
133
                    $needsFixing = true;
134
                }
135
            }
136
        }
137
138
        if ($needsFixing) {
139
            $request = RequestParser::parseAndDuplicateRequest($request);
140
        }
141
142
        return $request->query->all();
143
    }
144
145
    protected function denormalizePropertyName($property)
146
    {
147
        if (!$this->nameConverter instanceof NameConverterInterface) {
148
            return $property;
149
        }
150
151
        return implode('.', array_map([$this->nameConverter, 'denormalize'], explode('.', $property)));
152
    }
153
154
    protected function normalizePropertyName($property)
155
    {
156
        if (!$this->nameConverter instanceof NameConverterInterface) {
157
            return $property;
158
        }
159
160
        return implode('.', array_map([$this->nameConverter, 'normalize'], explode('.', $property)));
161
    }
162
}
163