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

api-platform / core / 9562426465

18 Jun 2024 09:20AM UTC coverage: 62.633%. First build
9562426465

push

github

web-flow
feat: BackedEnum resources (#6309)

* fix(metadata): Only add GET operations for enums when ApiResource doesn't specify operations

* feat(state): backed enum provider

* fix(metadata): enum resource identifier default to value

* fix(metadata): get method metadata for BackedEnums

* test: resource with enum properties schema

* what I would like

* test: backed enums

---------

Co-authored-by: soyuka <soyuka@users.noreply.github.com>

50 of 53 new or added lines in 5 files covered. (94.34%)

11014 of 17585 relevant lines covered (62.63%)

60.44 hits per line

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

85.0
/src/State/Provider/BackedEnumProvider.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\State\Provider;
15

16
use ApiPlatform\Metadata\CollectionOperationInterface;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\State\ProviderInterface;
20
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
21

22
final class BackedEnumProvider implements ProviderInterface
23
{
24
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
25
    {
26
        $resourceClass = $operation->getClass();
64✔
27
        if (!$resourceClass || !is_a($resourceClass, \BackedEnum::class, true)) {
64✔
NEW
28
            throw new RuntimeException('This resource is not an enum');
×
29
        }
30

31
        if ($operation instanceof CollectionOperationInterface) {
64✔
32
            return $resourceClass::cases();
28✔
33
        }
34

35
        $id = $uriVariables['id'] ?? null;
36✔
36
        if (null === $id) {
36✔
NEW
37
            throw new NotFoundHttpException('Not Found');
×
38
        }
39

40
        if ($enum = $this->resolveEnum($resourceClass, $id)) {
36✔
41
            return $enum;
28✔
42
        }
43

44
        throw new NotFoundHttpException('Not Found');
8✔
45
    }
46

47
    /**
48
     * @param class-string $resourceClass
49
     */
50
    private function resolveEnum(string $resourceClass, string|int $id): ?\BackedEnum
51
    {
52
        $reflectEnum = new \ReflectionEnum($resourceClass);
36✔
53
        $type = (string) $reflectEnum->getBackingType();
36✔
54

55
        if ('int' === $type) {
36✔
56
            if (!is_numeric($id)) {
32✔
NEW
57
                return null;
×
58
            }
59
            $enum = $resourceClass::tryFrom((int) $id);
32✔
60
        } else {
61
            $enum = $resourceClass::tryFrom($id);
4✔
62
        }
63

64
        // @deprecated enums will be indexable only by value in 4.0
65
        $enum ??= array_reduce($resourceClass::cases(), static fn ($c, \BackedEnum $case) => $id === $case->name ? $case : $c, null);
36✔
66

67
        return $enum;
36✔
68
    }
69
}
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