1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MadWizard\WebAuthn\Format; |
4
|
|
|
|
5
|
|
|
use ArrayObject; |
6
|
|
|
use JsonSerializable; |
7
|
|
|
use MadWizard\WebAuthn\Exception\CborException; |
8
|
|
|
|
9
|
|
|
final class CborMap implements JsonSerializable |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array[] |
13
|
|
|
* @phpstan-var array<string, array{0:mixed, 1:mixed}> |
14
|
|
|
*/ |
15
|
|
|
private $entries = []; |
16
|
|
|
|
17
|
92 |
|
public function __construct() |
18
|
|
|
{ |
19
|
92 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param mixed $key |
23
|
|
|
* |
24
|
|
|
* @throws CborException |
25
|
|
|
*/ |
26
|
76 |
|
private function getInternalKey($key): string |
27
|
|
|
{ |
28
|
76 |
|
$keyType = gettype($key); |
29
|
76 |
|
if ($keyType !== 'string' && $keyType !== 'integer') { |
30
|
1 |
|
throw new CborException('Only string and integer values may be used as keys.'); |
31
|
|
|
} |
32
|
75 |
|
return sprintf('%s:%s', $keyType, (string) $key); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param mixed $key |
37
|
|
|
* @param mixed $value |
38
|
|
|
* |
39
|
|
|
* @throws CborException |
40
|
|
|
*/ |
41
|
71 |
|
public function set($key, $value): void |
42
|
|
|
{ |
43
|
71 |
|
$this->entries[$this->getInternalKey($key)] = [$key, $value]; |
44
|
70 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param mixed $key |
48
|
|
|
* |
49
|
|
|
* @throws CborException |
50
|
|
|
*/ |
51
|
65 |
|
public function has($key): bool |
52
|
|
|
{ |
53
|
65 |
|
$internalKey = $this->getInternalKey($key); |
54
|
65 |
|
return array_key_exists($internalKey, $this->entries); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param mixed $key |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
* |
62
|
|
|
* @throws CborException |
63
|
|
|
*/ |
64
|
60 |
|
public function get($key) |
65
|
|
|
{ |
66
|
60 |
|
$internalKey = $this->getInternalKey($key); |
67
|
60 |
|
if (!array_key_exists($internalKey, $this->entries)) { |
68
|
1 |
|
throw new CborException("Key $internalKey is not present in CBOR map."); |
69
|
|
|
} |
70
|
59 |
|
return $this->entries[$internalKey][1]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param mixed $key |
75
|
|
|
* @param mixed $default |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
* |
79
|
|
|
* @throws CborException |
80
|
|
|
*/ |
81
|
9 |
|
public function getDefault($key, $default) |
82
|
|
|
{ |
83
|
9 |
|
return $this->has($key) ? $this->get($key) : $default; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param mixed $key |
88
|
|
|
* |
89
|
|
|
* @throws CborException |
90
|
|
|
*/ |
91
|
57 |
|
public function remove($key): void |
92
|
|
|
{ |
93
|
57 |
|
$internalKey = $this->getInternalKey($key); |
94
|
57 |
|
if (!isset($this->entries[$internalKey])) { |
95
|
1 |
|
throw new CborException("Key $internalKey is not present in CBOR map."); |
96
|
|
|
} |
97
|
56 |
|
unset($this->entries[$internalKey]); |
98
|
56 |
|
} |
99
|
|
|
|
100
|
55 |
|
public function count(): int |
101
|
|
|
{ |
102
|
55 |
|
return count($this->entries); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @phpstan-return array<array{0:mixed, 1:mixed}>> |
107
|
|
|
*/ |
108
|
8 |
|
public function getEntries(): array |
109
|
|
|
{ |
110
|
8 |
|
return array_values($this->entries); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getKeys(): array |
114
|
|
|
{ |
115
|
|
|
return array_map(function ($item) { return $item[0]; }, array_values($this->entries)); |
116
|
|
|
} |
117
|
|
|
|
118
|
63 |
|
public function copy(): self |
119
|
|
|
{ |
120
|
63 |
|
return clone $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
20 |
|
public static function fromArray(array $array): self |
124
|
|
|
{ |
125
|
20 |
|
$map = new CborMap(); |
126
|
20 |
|
foreach ($array as $k => $v) { |
127
|
20 |
|
$map->set($k, $v); |
128
|
|
|
} |
129
|
20 |
|
return $map; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return ArrayObject<int|string, mixed> |
134
|
|
|
*/ |
135
|
|
|
public function jsonSerialize(): ArrayObject |
136
|
|
|
{ |
137
|
|
|
$obj = new ArrayObject(); |
138
|
|
|
foreach ($this->entries as [$k, $v]) { |
139
|
|
|
$obj[$k] = $v; |
140
|
|
|
} |
141
|
|
|
return $obj; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|