Passed
Push — master ( 5271f3...ae4caa )
by Thomas
02:30
created

CborMap   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 109
ccs 45
cts 50
cp 0.9
rs 10
c 1
b 0
f 0
wmc 22

16 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 7 2
A get() 0 7 2
A __construct() 0 2 1
A offsetUnset() 0 3 1
A offsetSet() 0 3 1
A jsonSerialize() 0 7 2
A getInternalKey() 0 7 3
A offsetGet() 0 3 1
A remove() 0 7 2
A getKeys() 0 3 1
A copy() 0 3 1
A has() 0 4 1
A getEntries() 0 3 1
A count() 0 3 1
A offsetExists() 0 3 1
A set() 0 3 1
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