Debian Codegraph @

lib/3rdParty/composer/web-auth/cose-lib/src/Key/Key.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Cose\Key;
6
7 use function array_key_exists;
8 use InvalidArgumentException;
9 use function sprintf;
10
11 class Key
12 {
13 public const TYPE = 1;
14
15 public const TYPE_OKP = 1;
16
17 public const TYPE_EC2 = 2;
18
19 public const TYPE_RSA = 3;
20
21 public const TYPE_OCT = 4;
22
23 public const TYPE_NAME_OKP = 'OKP';
24
25 public const TYPE_NAME_EC2 = 'EC';
26
27 public const TYPE_NAME_RSA = 'RSA';
28
29 public const TYPE_NAME_OCT = 'oct';
30
31 public const KID = 2;
32
33 public const ALG = 3;
34
35 public const KEY_OPS = 4;
36
37 public const BASE_IV = 5;
38
39 /**
40 * @var array<int|string, mixed>
41 */
42 private readonly array $data;
43
44 /**
45 * @param array<int|string, mixed> $data
46 */
47 public function __construct(array $data)
48 {
49 if (! array_key_exists(self::TYPE, $data)) {
50 throw new InvalidArgumentException('Invalid key: the type is not defined');
51 }
52 $this->data = $data;
53 }
54
55 /**
56 * @param array<int|string, mixed> $data
57 */
58 public static function create(array $data): self
59 {
60 return new self($data);
61 }
62
63 /**
64 * @param array<int, mixed> $data
65 */
66 public static function createFromData(array $data): self
67 {
68 if (! array_key_exists(self::TYPE, $data)) {
69 throw new InvalidArgumentException('Invalid key: the type is not defined');
70 }
71
72 return match ($data[self::TYPE]) {
73 '1' => new OkpKey($data),
74 '2' => new Ec2Key($data),
75 '3' => new RsaKey($data),
76 '4' => new SymmetricKey($data),
77 default => self::create($data),
78 };
79 }
80
81 public function type(): int|string
82 {
83 return $this->data[self::TYPE];
84 }
85
86 public function alg(): int
87 {
88 return (int) $this->get(self::ALG);
89 }
90
91 /**
92 * @return array<int|string, mixed>
93 */
94 public function getData(): array
95 {
96 return $this->data;
97 }
98
99 public function has(int|string $key): bool
100 {
101 return array_key_exists($key, $this->data);
102 }
103
104 public function get(int|string $key): mixed
105 {
106 if (! array_key_exists($key, $this->data)) {
107 throw new InvalidArgumentException(sprintf('The key has no data at index %d', $key));
108 }
109
110 return $this->data[$key];
111 }
112 }