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

api-platform / core / 14954769666

11 May 2025 10:14AM UTC coverage: 0.0% (-8.5%) from 8.457%
14954769666

Pull #7135

github

web-flow
Merge bf21e0bc7 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

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

11040 existing lines in 370 files now uncovered.

0 of 48303 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
    {
44
        $description = [];
×
45

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

51
        foreach ($properties as $property => $unused) {
×
52
            if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isBackedEnumField($property, $resourceClass)) {
×
53
                continue;
×
54
            }
55
            $propertyName = $this->normalizePropertyName($property);
×
56
            $description[$propertyName] = [
×
57
                'property' => $propertyName,
×
58
                'type' => 'string',
×
59
                'required' => false,
×
60
                'schema' => [
×
61
                    'type' => 'string',
×
62
                    'enum' => array_map(fn (\BackedEnum $case) => $case->value, $this->enumTypes[$property]::cases()),
×
63
                ],
×
64
            ];
×
65
        }
66

67
        return $description;
×
68
    }
69

70
    abstract protected function getProperties(): ?array;
71

72
    abstract protected function getLogger(): LoggerInterface;
73

74
    abstract protected function normalizePropertyName(string $property): string;
75

76
    /**
77
     * Determines whether the given property refers to a backed enum field.
78
     */
79
    abstract protected function isBackedEnumField(string $property, string $resourceClass): bool;
80

81
    private function normalizeValue($value, string $property): mixed
82
    {
83
        $firstCase = $this->enumTypes[$property]::cases()[0] ?? null;
×
84
        if (
85
            \is_int($firstCase?->value)
×
86
            && false !== filter_var($value, \FILTER_VALIDATE_INT)
×
87
        ) {
UNCOV
88
            $value = (int) $value;
×
89
        }
90

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

93
        if (\in_array($value, $values, true)) {
×
94
            return $value;
×
95
        }
96

UNCOV
97
        $this->getLogger()->notice('Invalid filter ignored', [
×
UNCOV
98
            'exception' => new InvalidArgumentException(\sprintf('Invalid backed enum value for "%s" property, expected one of ( "%s" )',
×
UNCOV
99
                $property,
×
UNCOV
100
                implode('" | "', $values)
×
UNCOV
101
            )),
×
UNCOV
102
        ]);
×
103

UNCOV
104
        return null;
×
105
    }
106
}
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