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
|
88 |
|
public function __construct() |
18
|
|
|
{ |
19
|
88 |
|
} |
20
|
|
|
|
21
|
69 |
|
private function getInternalKey($key): string |
22
|
|
|
{ |
23
|
69 |
|
$keyType = gettype($key); |
24
|
69 |
|
if ($keyType !== 'string' && $keyType !== 'integer') { |
25
|
1 |
|
throw new CborException('Only string and integer values may be used as keys.'); |
26
|
|
|
} |
27
|
68 |
|
return sprintf('%s:%s', $keyType, (string) $key); |
28
|
|
|
} |
29
|
|
|
|
30
|
65 |
|
public function set($key, $value): void |
31
|
|
|
{ |
32
|
65 |
|
$this->entries[$this->getInternalKey($key)] = [$key, $value]; |
33
|
64 |
|
} |
34
|
|
|
|
35
|
60 |
|
public function has($key): bool |
36
|
|
|
{ |
37
|
60 |
|
$internalKey = $this->getInternalKey($key); |
38
|
60 |
|
return array_key_exists($internalKey, $this->entries); |
39
|
|
|
} |
40
|
|
|
|
41
|
57 |
|
public function get($key) |
42
|
|
|
{ |
43
|
57 |
|
$internalKey = $this->getInternalKey($key); |
44
|
57 |
|
if (!array_key_exists($internalKey, $this->entries)) { |
45
|
1 |
|
throw new CborException("Key $internalKey is not present in CBOR map."); |
46
|
|
|
} |
47
|
56 |
|
return $this->entries[$internalKey][1]; |
48
|
|
|
} |
49
|
|
|
|
50
|
9 |
|
public function getDefault($key, $default) |
51
|
|
|
{ |
52
|
9 |
|
return $this->has($key) ? $this->get($key) : $default; |
53
|
|
|
} |
54
|
|
|
|
55
|
54 |
|
public function remove($key) |
56
|
|
|
{ |
57
|
54 |
|
$internalKey = $this->getInternalKey($key); |
58
|
54 |
|
if (!isset($this->entries[$internalKey])) { |
59
|
1 |
|
throw new CborException("Key $internalKey is not present in CBOR map."); |
60
|
|
|
} |
61
|
53 |
|
unset($this->entries[$internalKey]); |
62
|
53 |
|
} |
63
|
|
|
|
64
|
52 |
|
public function count(): int |
65
|
|
|
{ |
66
|
52 |
|
return count($this->entries); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @phpstan-return array<array{0:mixed, 1:mixed}>> |
71
|
|
|
*/ |
72
|
2 |
|
public function getEntries(): array |
73
|
|
|
{ |
74
|
2 |
|
return array_values($this->entries); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getKeys(): array |
78
|
|
|
{ |
79
|
|
|
return array_map(function ($item) { return $item[0]; }, array_values($this->entries)); |
80
|
|
|
} |
81
|
|
|
|
82
|
58 |
|
public function copy(): self |
83
|
|
|
{ |
84
|
58 |
|
return clone $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
14 |
|
public static function fromArray(array $array): self |
88
|
|
|
{ |
89
|
14 |
|
$map = new CborMap(); |
90
|
14 |
|
foreach ($array as $k => $v) { |
91
|
14 |
|
$map->set($k, $v); |
92
|
|
|
} |
93
|
14 |
|
return $map; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function jsonSerialize() |
97
|
|
|
{ |
98
|
|
|
$obj = new ArrayObject(); |
99
|
|
|
foreach ($this->entries as [$k, $v]) { |
100
|
|
|
$obj[$k] = $v; |
101
|
|
|
} |
102
|
|
|
return $obj; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|