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