Debian Codegraph @

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