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

api-platform / core / 14246153067

03 Apr 2025 02:56PM UTC coverage: 7.286% (-0.002%) from 7.288%
14246153067

push

github

web-flow
Merge commit from fork

12 of 160 new or added lines in 7 files covered. (7.5%)

2343 existing lines in 152 files now uncovered.

12450 of 170870 relevant lines covered (7.29%)

12.06 hits per line

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

87.5
/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\PropertyAwareFilterInterface;
17
use ApiPlatform\Doctrine\Common\PropertyHelperTrait;
18
use ApiPlatform\Doctrine\Odm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait;
19
use ApiPlatform\Metadata\Operation;
20
use Doctrine\ODM\MongoDB\Aggregation\Builder;
21
use Doctrine\Persistence\ManagerRegistry;
22
use Psr\Log\LoggerInterface;
23
use Psr\Log\NullLogger;
24
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
25

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

39
    public function __construct(protected ManagerRegistry $managerRegistry, ?LoggerInterface $logger = null, protected ?array $properties = null, protected ?NameConverterInterface $nameConverter = null)
40
    {
41
        $this->logger = $logger ?? new NullLogger();
335✔
42
    }
43

44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function apply(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
48
    {
UNCOV
49
        foreach ($context['filters'] as $property => $value) {
155✔
UNCOV
50
            $this->filterProperty($this->denormalizePropertyName($property), $value, $aggregationBuilder, $resourceClass, $operation, $context);
129✔
51
        }
52
    }
53

54
    /**
55
     * Passes a property through the filter.
56
     */
57
    abstract protected function filterProperty(string $property, $value, Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void;
58

59
    protected function getManagerRegistry(): ManagerRegistry
60
    {
61
        return $this->managerRegistry;
261✔
62
    }
63

64
    protected function getProperties(): ?array
65
    {
66
        return $this->properties;
248✔
67
    }
68

69
    /**
70
     * @param string[] $properties
71
     */
72
    public function setProperties(array $properties): void
73
    {
UNCOV
74
        $this->properties = $properties;
145✔
75
    }
76

77
    protected function getLogger(): LoggerInterface
78
    {
UNCOV
79
        return $this->logger;
14✔
80
    }
81

82
    /**
83
     * Determines whether the given property is enabled.
84
     */
85
    protected function isPropertyEnabled(string $property, string $resourceClass): bool
86
    {
UNCOV
87
        if (null === $this->properties) {
129✔
88
            // to ensure sanity, nested properties must still be explicitly enabled
UNCOV
89
            return !$this->isPropertyNested($property, $resourceClass);
3✔
90
        }
91

UNCOV
92
        return \array_key_exists($property, $this->properties);
127✔
93
    }
94

95
    protected function denormalizePropertyName(string|int $property): string
96
    {
UNCOV
97
        if (!$this->nameConverter instanceof NameConverterInterface) {
131✔
98
            return (string) $property;
×
99
        }
100

UNCOV
101
        return implode('.', array_map($this->nameConverter->denormalize(...), explode('.', (string) $property)));
131✔
102
    }
103

104
    protected function normalizePropertyName(string $property): string
105
    {
UNCOV
106
        if (!$this->nameConverter instanceof NameConverterInterface) {
245✔
107
            return $property;
×
108
        }
109

UNCOV
110
        return implode('.', array_map($this->nameConverter->normalize(...), explode('.', $property)));
245✔
111
    }
112
}
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