-
-
Notifications
You must be signed in to change notification settings - Fork 859
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
924 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/Metadata/Resource/Factory/BackedEnumResourceMetadataCollectionFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Metadata\Resource\Factory; | ||
|
||
use ApiPlatform\Metadata\Operations; | ||
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; | ||
|
||
/** | ||
* Triggers resource deprecations. | ||
* | ||
* @internal | ||
*/ | ||
final class BackedEnumResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface | ||
{ | ||
public const PROVIDER = 'api_platform.state_provider.backed_enum'; | ||
|
||
public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $decorated) | ||
{ | ||
} | ||
|
||
public function create(string $resourceClass): ResourceMetadataCollection | ||
{ | ||
$resourceMetadataCollection = $this->decorated->create($resourceClass); | ||
if (!is_a($resourceClass, \BackedEnum::class, true)) { | ||
return $resourceMetadataCollection; | ||
} | ||
|
||
foreach ($resourceMetadataCollection as $i => $resourceMetadata) { | ||
$newOperations = []; | ||
foreach ($resourceMetadata->getOperations() as $operationName => $operation) { | ||
$newOperations[$operationName] = $operation; | ||
|
||
if (null !== $operation->getProvider()) { | ||
continue; | ||
} | ||
|
||
$newOperations[$operationName] = $operation->withProvider(self::PROVIDER); | ||
} | ||
|
||
$newGraphQlOperations = []; | ||
foreach ($resourceMetadata->getGraphQlOperations() as $operationName => $operation) { | ||
$newGraphQlOperations[$operationName] = $operation; | ||
|
||
if (null !== $operation->getProvider()) { | ||
continue; | ||
} | ||
|
||
$newGraphQlOperations[$operationName] = $operation->withProvider(self::PROVIDER); | ||
} | ||
|
||
$resourceMetadataCollection[$i] = $resourceMetadata->withOperations(new Operations($newOperations))->withGraphQlOperations($newGraphQlOperations); | ||
} | ||
|
||
return $resourceMetadataCollection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\State\Provider; | ||
|
||
use ApiPlatform\Metadata\CollectionOperationInterface; | ||
use ApiPlatform\Metadata\Exception\RuntimeException; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProviderInterface; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
final class BackedEnumProvider implements ProviderInterface | ||
{ | ||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
{ | ||
$resourceClass = $operation->getClass(); | ||
if (!$resourceClass || !is_a($resourceClass, \BackedEnum::class, true)) { | ||
throw new RuntimeException('This resource is not an enum'); | ||
} | ||
|
||
if ($operation instanceof CollectionOperationInterface) { | ||
return $resourceClass::cases(); | ||
} | ||
|
||
$id = $uriVariables['id'] ?? null; | ||
if (null === $id) { | ||
throw new NotFoundHttpException('Not Found'); | ||
} | ||
|
||
if ($enum = $this->resolveEnum($resourceClass, $id)) { | ||
return $enum; | ||
} | ||
|
||
throw new NotFoundHttpException('Not Found'); | ||
} | ||
|
||
/** | ||
* @param class-string $resourceClass | ||
*/ | ||
private function resolveEnum(string $resourceClass, string|int $id): ?\BackedEnum | ||
{ | ||
$reflectEnum = new \ReflectionEnum($resourceClass); | ||
$type = (string) $reflectEnum->getBackingType(); | ||
|
||
if ('int' === $type) { | ||
if (!is_numeric($id)) { | ||
return null; | ||
} | ||
$enum = $resourceClass::tryFrom((int) $id); | ||
} else { | ||
$enum = $resourceClass::tryFrom($id); | ||
} | ||
|
||
// @deprecated enums will be indexable only by value in 4.0 | ||
$enum ??= array_reduce($resourceClass::cases(), static fn ($c, \BackedEnum $case) => $id === $case->name ? $case : $c, null); | ||
|
||
return $enum; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tests/Fixtures/TestBundle/ApiResource/BackedEnumIntegerResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
|
||
#[ApiResource] | ||
enum BackedEnumIntegerResource: int | ||
{ | ||
case Yes = 1; | ||
case No = 2; | ||
case Maybe = 3; | ||
|
||
public function getDescription(): string | ||
{ | ||
return match ($this) { | ||
self::Yes => 'We say yes', | ||
self::No => 'Computer says no', | ||
self::Maybe => 'Let me think about it', | ||
}; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/Fixtures/TestBundle/ApiResource/BackedEnumStringResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
|
||
#[ApiResource] | ||
enum BackedEnumStringResource: string | ||
{ | ||
case Yes = 'yes'; | ||
case No = 'no'; | ||
case Maybe = 'maybe'; | ||
|
||
public function getDescription(): string | ||
{ | ||
return match ($this) { | ||
self::Yes => 'We say yes', | ||
self::No => 'Computer says no', | ||
self::Maybe => 'Let me think about it', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.