-
-
Notifications
You must be signed in to change notification settings - Fork 838
/
AbstractFilter.php
105 lines (87 loc) · 3.37 KB
/
AbstractFilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?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\Doctrine\Orm\Filter;
use ApiPlatform\Doctrine\Common\PropertyHelperTrait;
use ApiPlatform\Doctrine\Orm\PropertyHelperTrait as OrmPropertyHelperTrait;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
abstract class AbstractFilter implements FilterInterface
{
use OrmPropertyHelperTrait;
use PropertyHelperTrait;
protected LoggerInterface $logger;
public function __construct(protected ManagerRegistry $managerRegistry, ?LoggerInterface $logger = null, protected ?array $properties = null, protected ?NameConverterInterface $nameConverter = null)
{
$this->logger = $logger ?? new NullLogger();
}
/**
* {@inheritdoc}
*/
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
{
foreach ($context['filters'] as $property => $value) {
$this->filterProperty($this->denormalizePropertyName($property), $value, $queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);
}
}
/**
* Passes a property through the filter.
*/
abstract protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void;
protected function getManagerRegistry(): ManagerRegistry
{
return $this->managerRegistry;
}
protected function getProperties(): ?array
{
return $this->properties;
}
protected function getLogger(): LoggerInterface
{
return $this->logger;
}
/**
* @param string[] $properties
*/
public function setProperties(array $properties): void
{
$this->properties = $properties;
}
/**
* Determines whether the given property is enabled.
*/
protected function isPropertyEnabled(string $property, string $resourceClass): bool
{
if (null === $this->properties) {
// to ensure sanity, nested properties must still be explicitly enabled
return !$this->isPropertyNested($property, $resourceClass);
}
return \array_key_exists($property, $this->properties);
}
protected function denormalizePropertyName(string|int $property): string
{
if (!$this->nameConverter instanceof NameConverterInterface) {
return $property;
}
return implode('.', array_map($this->nameConverter->denormalize(...), explode('.', (string) $property)));
}
protected function normalizePropertyName(string $property): string
{
if (!$this->nameConverter instanceof NameConverterInterface) {
return $property;
}
return implode('.', array_map($this->nameConverter->normalize(...), explode('.', $property)));
}
}