• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 20001323174

07 Dec 2025 08:10AM UTC coverage: 25.292% (+0.001%) from 25.291%
20001323174

push

github

soyuka
chore: bump metadata to 4.3.x-dev

14642 of 57891 relevant lines covered (25.29%)

28.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

25.0
/src/Doctrine/Odm/Filter/AbstractFilter.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Doctrine\Odm\Filter;
15

16
use ApiPlatform\Doctrine\Common\Filter\ManagerRegistryAwareInterface;
17
use ApiPlatform\Doctrine\Common\Filter\PropertyAwareFilterInterface;
18
use ApiPlatform\Doctrine\Common\PropertyHelperTrait;
19
use ApiPlatform\Doctrine\Odm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait;
20
use ApiPlatform\Metadata\Exception\RuntimeException;
21
use ApiPlatform\Metadata\Operation;
22
use Doctrine\ODM\MongoDB\Aggregation\Builder;
23
use Doctrine\Persistence\ManagerRegistry;
24
use Psr\Log\LoggerInterface;
25
use Psr\Log\NullLogger;
26
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
27

28
/**
29
 * {@inheritdoc}
30
 *
31
 * Abstract class for easing the implementation of a filter.
32
 *
33
 * @author Alan Poulain <contact@alanpoulain.eu>
34
 */
35
abstract class AbstractFilter implements FilterInterface, PropertyAwareFilterInterface, ManagerRegistryAwareInterface
36
{
37
    use MongoDbOdmPropertyHelperTrait;
38
    use PropertyHelperTrait;
39
    protected LoggerInterface $logger;
40

41
    public function __construct(
42
        protected ?ManagerRegistry $managerRegistry = null,
43
        ?LoggerInterface $logger = null,
44
        protected ?array $properties = null,
45
        protected ?NameConverterInterface $nameConverter = null,
46
    ) {
47
        $this->logger = $logger ?? new NullLogger();
2✔
48
    }
49

50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
54
    {
55
        foreach ($context['filters'] as $property => $value) {
×
56
            $this->filterProperty($this->denormalizePropertyName($property), $value, $aggregationBuilder, $resourceClass, $operation, $context);
×
57
        }
58
    }
59

60
    /**
61
     * Passes a property through the filter.
62
     *
63
     * @param array<string, mixed> $context
64
     *
65
     * @param-out array<string, mixed> $context
66
     */
67
    abstract protected function filterProperty(string $property, mixed $value, Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void;
68

69
    public function hasManagerRegistry(): bool
70
    {
71
        return $this->managerRegistry instanceof ManagerRegistry;
2✔
72
    }
73

74
    public function getManagerRegistry(): ManagerRegistry
75
    {
76
        if (!$this->hasManagerRegistry()) {
2✔
77
            throw new RuntimeException('ManagerRegistry must be initialized before accessing it.');
×
78
        }
79

80
        return $this->managerRegistry;
2✔
81
    }
82

83
    public function setManagerRegistry(ManagerRegistry $managerRegistry): void
84
    {
85
        $this->managerRegistry = $managerRegistry;
×
86
    }
87

88
    /**
89
     * @return array<string, mixed>|null
90
     */
91
    public function getProperties(): ?array
92
    {
93
        return $this->properties;
2✔
94
    }
95

96
    /**
97
     * @param array<string, mixed> $properties
98
     */
99
    public function setProperties(array $properties): void
100
    {
101
        $this->properties = $properties;
×
102
    }
103

104
    protected function getLogger(): LoggerInterface
105
    {
106
        return $this->logger;
×
107
    }
108

109
    /**
110
     * Determines whether the given property is enabled.
111
     */
112
    protected function isPropertyEnabled(string $property, string $resourceClass): bool
113
    {
114
        if (null === $this->properties) {
×
115
            // to ensure sanity, nested properties must still be explicitly enabled
116
            return !$this->isPropertyNested($property, $resourceClass);
×
117
        }
118

119
        return \array_key_exists($property, $this->properties);
×
120
    }
121

122
    protected function denormalizePropertyName(string|int $property): string
123
    {
124
        if (!$this->nameConverter instanceof NameConverterInterface) {
×
125
            return (string) $property;
×
126
        }
127

128
        return implode('.', array_map($this->nameConverter->denormalize(...), explode('.', (string) $property)));
×
129
    }
130

131
    protected function normalizePropertyName(string $property): string
132
    {
133
        if (!$this->nameConverter instanceof NameConverterInterface) {
×
134
            return $property;
×
135
        }
136

137
        return implode('.', array_map($this->nameConverter->normalize(...), explode('.', $property)));
×
138
    }
139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc