Debian Codegraph @

lib/3rdParty/composer/spomky-labs/cbor-php/src/IndefiniteLengthMapObject.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CBOR;
6
7 use function array_key_exists;
8 use ArrayAccess;
9 use ArrayIterator;
10 use InvalidArgumentException;
11 use Iterator;
12 use IteratorAggregate;
13
14 /**
15 * @phpstan-implements ArrayAccess<int, CBORObject>
16 * @phpstan-implements IteratorAggregate<int, MapItem>
17 * @final
18 */
19 class IndefiniteLengthMapObject extends AbstractCBORObject implements IteratorAggregate, Normalizable, ArrayAccess
20 {
21 private const MAJOR_TYPE = self::MAJOR_TYPE_MAP;
22
23 private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;
24
25 /**
26 * @var MapItem[]
27 */
28 private array $data = [];
29
30 public function __construct()
31 {
32 parent::__construct(self::MAJOR_TYPE, self::ADDITIONAL_INFORMATION);
33 }
34
35 public function __toString(): string
36 {
37 $result = parent::__toString();
38 foreach ($this->data as $object) {
39 $result .= (string) $object->getKey();
40 $result .= (string) $object->getValue();
41 }
42
43 return $result . "\xFF";
44 }
45
46 public static function create(): self
47 {
48 return new self();
49 }
50
51 public function add(CBORObject $key, CBORObject $value): self
52 {
53 if (! $key instanceof Normalizable) {
54 throw new InvalidArgumentException('Invalid key. Shall be normalizable');
55 }
56 $this->data[$key->normalize()] = MapItem::create($key, $value);
57
58 return $this;
59 }
60
61 public function has(int|string $key): bool
62 {
63 return array_key_exists($key, $this->data);
64 }
65
66 public function remove(int|string $index): self
67 {
68 if (! $this->has($index)) {
69 return $this;
70 }
71 unset($this->data[$index]);
72 $this->data = array_values($this->data);
73
74 return $this;
75 }
76
77 public function get(int|string $index): CBORObject
78 {
79 if (! $this->has($index)) {
80 throw new InvalidArgumentException('Index not found.');
81 }
82
83 return $this->data[$index]->getValue();
84 }
85
86 public function set(MapItem $object): self
87 {
88 $key = $object->getKey();
89 if (! $key instanceof Normalizable) {
90 throw new InvalidArgumentException('Invalid key. Shall be normalizable');
91 }
92
93 $this->data[$key->normalize()] = $object;
94
95 return $this;
96 }
97
98 /**
99 * @return Iterator<int, MapItem>
100 */
101 public function getIterator(): Iterator
102 {
103 return new ArrayIterator($this->data);
104 }
105
106 /**
107 * @return mixed[]
108 */
109 public function normalize(): array
110 {
111 return array_reduce($this->data, static function (array $carry, MapItem $item): array {
112 $key = $item->getKey();
113 if (! $key instanceof Normalizable) {
114 throw new InvalidArgumentException('Invalid key. Shall be normalizable');
115 }
116 $valueObject = $item->getValue();
117 $carry[$key->normalize()] = $valueObject instanceof Normalizable ? $valueObject->normalize() : $valueObject;
118
119 return $carry;
120 }, []);
121 }
122
123 public function offsetExists($offset): bool
124 {
125 return $this->has($offset);
126 }
127
128 public function offsetGet($offset): CBORObject
129 {
130 return $this->get($offset);
131 }
132
133 public function offsetSet($offset, $value): void
134 {
135 if (! $offset instanceof CBORObject) {
136 throw new InvalidArgumentException('Invalid key');
137 }
138 if (! $value instanceof CBORObject) {
139 throw new InvalidArgumentException('Invalid value');
140 }
141
142 $this->set(MapItem::create($offset, $value));
143 }
144
145 public function offsetUnset($offset): void
146 {
147 $this->remove($offset);
148 }
149 }