-
-
Notifications
You must be signed in to change notification settings - Fork 928
Add Symfony Uid support #3715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Symfony Uid support #3715
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -53,6 +53,7 @@ | |||||
use Symfony\Component\Finder\Finder; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
use Symfony\Component\HttpClient\HttpClientTrait; | ||||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||||||
use Symfony\Component\Uid\AbstractUid; | ||||||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
use Symfony\Component\Yaml\Yaml; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
|
@@ -167,6 +168,10 @@ private function registerCommonConfiguration(ContainerBuilder $container, array | |||||
$loader->load('ramsey_uuid.xml'); | ||||||
} | ||||||
|
||||||
if (class_exists(AbstractUid::class)) { | ||||||
$loader->load('symfony_uid.xml'); | ||||||
} | ||||||
|
||||||
$container->setParameter('api_platform.enable_entrypoint', $config['enable_entrypoint']); | ||||||
$container->setParameter('api_platform.enable_docs', $config['enable_docs']); | ||||||
$container->setParameter('api_platform.title', $config['title']); | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="api_platform.identifier.symfony_ulid_normalizer" class="ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer\UlidNormalizer" public="false"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not make the two Normalizers in one? |
||
<tag name="api_platform.identifier.denormalizer" /> | ||
</service> | ||
|
||
<service id="api_platform.identifier.symfony_uuid_normalizer" class="ApiPlatform\Core\Bridge\Symfony\Identifier\Normalizer\UuidNormalizer" public="false"> | ||
<tag name="api_platform.identifier.denormalizer" /> | ||
</service> | ||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Core\Bridge\Symfony\Identifier\Normalizer; | ||
|
||
use ApiPlatform\Core\Exception\InvalidIdentifierException; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Uid\Ulid; | ||
|
||
/** | ||
* Denormalizes an ULID string to an instance of Symfony\Component\Uid\Ulid. | ||
*/ | ||
final class UlidNormalizer implements DenormalizerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function denormalize($data, $class, $format = null, array $context = []) | ||
{ | ||
try { | ||
return Ulid::fromString($data); | ||
} catch (\InvalidArgumentException $e) { | ||
throw new InvalidIdentifierException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDenormalization($data, $type, $format = null) | ||
{ | ||
return \is_string($data) && is_a($type, Ulid::class, true); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Core\Bridge\Symfony\Identifier\Normalizer; | ||
|
||
use ApiPlatform\Core\Exception\InvalidIdentifierException; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
/** | ||
* Denormalizes an UUID string to an instance of Symfony\Component\Uid\Uuid. | ||
*/ | ||
final class UuidNormalizer implements DenormalizerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function denormalize($data, $class, $format = null, array $context = []) | ||
{ | ||
try { | ||
return Uuid::fromString($data); | ||
} catch (\InvalidArgumentException $e) { | ||
throw new InvalidIdentifierException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDenormalization($data, $type, $format = null) | ||
{ | ||
return \is_string($data) && is_a($type, Uuid::class, true); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.