Debian Codegraph @

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