| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\Denormalizer; |
| 6 |
|
| 7 |
use function assert; |
| 8 |
use function is_array; |
| 9 |
use function is_string; |
| 10 |
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 11 |
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 12 |
use Webauthn\AuthenticationExtensions\AuthenticationExtension; |
| 13 |
use Webauthn\AuthenticationExtensions\AuthenticationExtensions; |
| 14 |
|
| 15 |
final class AuthenticationExtensionsDenormalizer implements DenormalizerInterface, NormalizerInterface |
| 16 |
{ |
| 17 |
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed |
| 18 |
{ |
| 19 |
if ($data instanceof AuthenticationExtensions) { |
| 20 |
return AuthenticationExtensions::create($data->extensions); |
| 21 |
} |
| 22 |
assert(is_array($data), 'The data should be an array.'); |
| 23 |
foreach ($data as $key => $value) { |
| 24 |
if (! is_string($key)) { |
| 25 |
continue; |
| 26 |
} |
| 27 |
$data[$key] = AuthenticationExtension::create($key, $value); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
return AuthenticationExtensions::create($data); |
| 32 |
} |
| 33 |
|
| 34 |
public function supportsDenormalization( |
| 35 |
mixed $data, |
| 36 |
string $type, |
| 37 |
?string $format = null, |
| 38 |
array $context = [] |
| 39 |
): bool { |
| 40 |
return $type === AuthenticationExtensions::class; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function getSupportedTypes(?string $format): array |
| 47 |
{ |
| 48 |
return [ |
| 49 |
AuthenticationExtensions::class => true, |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function normalize(mixed $data, ?string $format = null, array $context = []): array |
| 57 |
{ |
| 58 |
assert($data instanceof AuthenticationExtensions); |
| 59 |
$extensions = []; |
| 60 |
foreach ($data->extensions as $extension) { |
| 61 |
$extensions[$extension->name] = $extension->value; |
| 62 |
} |
| 63 |
|
| 64 |
return $extensions; |
| 65 |
} |
| 66 |
|
| 67 |
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool |
| 68 |
{ |
| 69 |
return $data instanceof AuthenticationExtensions; |
| 70 |
} |
| 71 |
} |