Debian Codegraph @

lib/3rdParty/composer/web-auth/webauthn-lib/src/Denormalizer/TrustPathDenormalizer.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 function is_array;
10 use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
11 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
12 use Webauthn\Exception\InvalidTrustPathException;
13 use Webauthn\TrustPath\CertificateTrustPath;
14 use Webauthn\TrustPath\EmptyTrustPath;
15 use Webauthn\TrustPath\TrustPath;
16
17 final class TrustPathDenormalizer implements DenormalizerInterface, NormalizerInterface
18 {
19 /**
20 * @throws InvalidTrustPathException
21 */
22 public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
23 {
24 /** @var array{x5c?: list<string>, type?: string} $data */
25 return match (true) {
26 array_key_exists('x5c', $data) && is_array($data['x5c']) => CertificateTrustPath::create($data['x5c']),
27 $data === [], isset($data['type']) && $data['type'] === EmptyTrustPath::class => EmptyTrustPath::create(),
28 default => throw new InvalidTrustPathException('Unsupported trust path type'),
29 };
30 }
31
32 public function supportsDenormalization(
33 mixed $data,
34 string $type,
35 ?string $format = null,
36 array $context = []
37 ): bool {
38 return $type === TrustPath::class;
39 }
40
41 /**
42 * @return array<class-string, bool>
43 */
44 public function getSupportedTypes(?string $format): array
45 {
46 return [
47 TrustPath::class => true,
48 ];
49 }
50
51 /**
52 * @return array<string, mixed>
53 */
54 public function normalize(mixed $data, ?string $format = null, array $context = []): array
55 {
56 assert($data instanceof TrustPath);
57 return match (true) {
58 $data instanceof CertificateTrustPath => [
59 'x5c' => $data->certificates,
60 ],
61 $data instanceof EmptyTrustPath => [],
62 default => throw new InvalidTrustPathException('Unsupported trust path type'),
63 };
64 }
65
66 public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
67 {
68 return $data instanceof TrustPath;
69 }
70 }