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

api-platform / core / 16705318661

03 Aug 2025 01:05PM UTC coverage: 0.0% (-21.9%) from 21.944%
16705318661

Pull #7317

github

web-flow
Merge 1ca8642ff into d06b1a0a0
Pull Request #7317: Fix/4372 skip null values in hal

0 of 14 new or added lines in 3 files covered. (0.0%)

11680 existing lines in 376 files now uncovered.

0 of 51817 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/Doctrine/Common/Filter/BackedEnumFilterTrait.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\Common\Filter;
15

16
use ApiPlatform\Doctrine\Common\PropertyHelperTrait;
17
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
18
use Psr\Log\LoggerInterface;
19

20
/**
21
 * Trait for filtering the collection by backed enum values.
22
 *
23
 * Filters collection on equality of backed enum properties.
24
 *
25
 * For each property passed, if the resource does not have such property or if
26
 * the value is not one of cases the property is ignored.
27
 *
28
 * @author Rémi Marseille <marseille.remi@gmail.com>
29
 */
30
trait BackedEnumFilterTrait
31
{
32
    use PropertyHelperTrait;
33

34
    /**
35
     * @var array<string, class-string>
36
     */
37
    private array $enumTypes;
38

39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getDescription(string $resourceClass): array
43
    {
UNCOV
44
        $description = [];
×
45

UNCOV
46
        $properties = $this->getProperties();
×
UNCOV
47
        if (null === $properties) {
×
48
            $properties = array_fill_keys($this->getClassMetadata($resourceClass)->getFieldNames(), null);
×
49
        }
50

UNCOV
51
        foreach ($properties as $property => $unused) {
×
UNCOV
52
            if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isBackedEnumField($property, $resourceClass)) {
×
53
                continue;
×
54
            }
UNCOV
55
            $propertyName = $this->normalizePropertyName($property);
×
UNCOV
56
            $filterParameterNames = [$propertyName];
×
UNCOV
57
            $filterParameterNames[] = $propertyName.'[]';
×
58

UNCOV
59
            foreach ($filterParameterNames as $filterParameterName) {
×
UNCOV
60
                $isCollection = str_ends_with($filterParameterName, '[]');
×
61

UNCOV
62
                $enumValues = array_map(fn (\BackedEnum $case) => $case->value, $this->enumTypes[$property]::cases());
×
63

UNCOV
64
                $schema = $isCollection
×
UNCOV
65
                    ? ['type' => 'array', 'items' => ['type' => 'string', 'enum' => $enumValues]]
×
UNCOV
66
                    : ['type' => 'string', 'enum' => $enumValues];
×
67

UNCOV
68
                $description[$filterParameterName] = [
×
UNCOV
69
                    'property' => $propertyName,
×
UNCOV
70
                    'type' => 'string',
×
UNCOV
71
                    'required' => false,
×
UNCOV
72
                    'is_collection' => $isCollection,
×
UNCOV
73
                    'schema' => $schema,
×
UNCOV
74
                ];
×
75
            }
76
        }
77

UNCOV
78
        return $description;
×
79
    }
80

81
    abstract protected function getProperties(): ?array;
82

83
    abstract protected function getLogger(): LoggerInterface;
84

85
    abstract protected function normalizePropertyName(string $property): string;
86

87
    /**
88
     * Determines whether the given property refers to a backed enum field.
89
     */
90
    abstract protected function isBackedEnumField(string $property, string $resourceClass): bool;
91

92
    private function normalizeValue($value, string $property): mixed
93
    {
UNCOV
94
        $firstCase = $this->enumTypes[$property]::cases()[0] ?? null;
×
95
        if (
UNCOV
96
            \is_int($firstCase?->value)
×
UNCOV
97
            && false !== filter_var($value, \FILTER_VALIDATE_INT)
×
98
        ) {
UNCOV
99
            $value = (int) $value;
×
100
        }
101

UNCOV
102
        $values = array_map(fn (\BackedEnum $case) => $case->value, $this->enumTypes[$property]::cases());
×
103

UNCOV
104
        if (\in_array($value, $values, true)) {
×
UNCOV
105
            return $value;
×
106
        }
107

108
        $this->getLogger()->notice('Invalid filter ignored', [
×
109
            'exception' => new InvalidArgumentException(\sprintf('Invalid backed enum value for "%s" property, expected one of ( "%s" )',
×
110
                $property,
×
111
                implode('" | "', $values)
×
112
            )),
×
113
        ]);
×
114

115
        return null;
×
116
    }
117
}
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

© 2025 Coveralls, Inc