Debian Codegraph @

lib/3rdParty/composer/web-auth/webauthn-lib/src/Denormalizer/PublicKeyCredentialUserEntityDenormalizer.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Webauthn\Denormalizer;
6
7 use function array_key_exists;
8 use function assert;
9 use ParagonIE\ConstantTime\Base64UrlSafe;
10 use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
11 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
12 use Webauthn\Exception\InvalidDataException;
13 use Webauthn\PublicKeyCredentialUserEntity;
14 use Webauthn\Util\Base64;
15
16 final class PublicKeyCredentialUserEntityDenormalizer implements DenormalizerInterface, NormalizerInterface
17 {
18 /**
19 * @throws InvalidDataException
20 */
21 public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
22 {
23 /** @var array{id?: string, name: string, displayName: string} $data */
24 if (! array_key_exists('id', $data)) {
25 return $data;
26 }
27 $data['id'] = Base64::decode($data['id']);
28
29 return PublicKeyCredentialUserEntity::create($data['name'], $data['id'], $data['displayName']);
30 }
31
32 public function supportsDenormalization(
33 mixed $data,
34 string $type,
35 ?string $format = null,
36 array $context = []
37 ): bool {
38 return $type === PublicKeyCredentialUserEntity::class;
39 }
40
41 /**
42 * @return array<class-string, bool>
43 */
44 public function getSupportedTypes(?string $format): array
45 {
46 return [
47 PublicKeyCredentialUserEntity::class => true,
48 ];
49 }
50
51 /**
52 * @return array<string, mixed>
53 */
54 public function normalize(mixed $object, ?string $format = null, array $context = []): array
55 {
56 assert($object instanceof PublicKeyCredentialUserEntity);
57 return [
58 'id' => Base64UrlSafe::encodeUnpadded($object->id),
59 'name' => $object->name,
60 'displayName' => $object->displayName,
61 ];
62 }
63
64 public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
65 {
66 return $data instanceof PublicKeyCredentialUserEntity;
67 }
68 }