PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.7.0
Defender Security – Malware Scanner, Login Security & Firewall v5.7.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / spomky-labs / cbor-php / src / ListObject.php

ListObject.php in Defender Security – Malware Scanner, Login Security & Firewall 5.7.0, at vendor/spomky-labs/cbor-php/src/ListObject.php

188 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 /*
6 * The MIT License (MIT)
7 *
8 * Copyright (c) 2018-2020 Spomky-Labs
9 *
10 * This software may be modified and distributed under the terms
11 * of the MIT license. See the LICENSE file for details.
12 */
13
14 namespace CBOR;
15
16 use function array_key_exists;
17 use ArrayAccess;
18 use ArrayIterator;
19 use function count;
20 use Countable;
21 use InvalidArgumentException;
22 use Iterator;
23 use IteratorAggregate;
24
25 /**
26 * @phpstan-implements ArrayAccess<int, CBORObject>
27 * @phpstan-implements IteratorAggregate<int, CBORObject>
28 */
29 class ListObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess
30 {
31 private const MAJOR_TYPE = self::MAJOR_TYPE_LIST;
32
33 /**
34 * @var CBORObject[]
35 */
36 private $data;
37
38 /**
39 * @var string|null
40 */
41 private $length;
42
43 /**
44 * @param CBORObject[] $data
45 */
46 public function __construct(array $data = [])
47 {
48 [$additionalInformation, $length] = LengthCalculator::getLengthOfArray($data);
49 array_map(static function ($item): void {
50 if (! $item instanceof CBORObject) {
51 throw new InvalidArgumentException('The list must contain only CBORObject objects.');
52 }
53 }, $data);
54
55 parent::__construct(self::MAJOR_TYPE, $additionalInformation);
56 $this->data = array_values($data);
57 $this->length = $length;
58 }
59
60 public function __toString(): string
61 {
62 $result = parent::__toString();
63 if ($this->length !== null) {
64 $result .= $this->length;
65 }
66 foreach ($this->data as $object) {
67 $result .= (string) $object;
68 }
69
70 return $result;
71 }
72
73 /**
74 * @param CBORObject[] $data
75 */
76 public static function create(array $data = []): self
77 {
78 return new self($data);
79 }
80
81 public function add(CBORObject $object): self
82 {
83 $this->data[] = $object;
84 [$this->additionalInformation, $this->length] = LengthCalculator::getLengthOfArray($this->data);
85
86 return $this;
87 }
88
89 public function has(int $index): bool
90 {
91 return array_key_exists($index, $this->data);
92 }
93
94 public function remove(int $index): self
95 {
96 if (! $this->has($index)) {
97 return $this;
98 }
99 unset($this->data[$index]);
100 $this->data = array_values($this->data);
101 [$this->additionalInformation, $this->length] = LengthCalculator::getLengthOfArray($this->data);
102
103 return $this;
104 }
105
106 public function get(int $index): CBORObject
107 {
108 if (! $this->has($index)) {
109 throw new InvalidArgumentException('Index not found.');
110 }
111
112 return $this->data[$index];
113 }
114
115 public function set(int $index, CBORObject $object): self
116 {
117 if (! $this->has($index)) {
118 throw new InvalidArgumentException('Index not found.');
119 }
120
121 $this->data[$index] = $object;
122 [$this->additionalInformation, $this->length] = LengthCalculator::getLengthOfArray($this->data);
123
124 return $this;
125 }
126
127 /**
128 * @return array<int, mixed>
129 */
130 public function normalize(): array
131 {
132 return array_map(static function (CBORObject $object) {
133 return $object instanceof Normalizable ? $object->normalize() : $object;
134 }, $this->data);
135 }
136
137 /**
138 * @deprecated The method will be removed on v3.0. Please rely on the CBOR\Normalizable interface
139 *
140 * @return array<int|string, mixed>
141 */
142 public function getNormalizedData(bool $ignoreTags = false): array
143 {
144 return array_map(static function (CBORObject $object) use ($ignoreTags) {
145 return $object->getNormalizedData($ignoreTags);
146 }, $this->data);
147 }
148
149 public function count(): int
150 {
151 return count($this->data);
152 }
153
154 /**
155 * @return Iterator<int, CBORObject>
156 */
157 public function getIterator(): Iterator
158 {
159 return new ArrayIterator($this->data);
160 }
161
162 public function offsetExists($offset): bool
163 {
164 return $this->has($offset);
165 }
166
167 public function offsetGet($offset): CBORObject
168 {
169 return $this->get($offset);
170 }
171
172 public function offsetSet($offset, $value): void
173 {
174 if ($offset === null) {
175 $this->add($value);
176
177 return;
178 }
179
180 $this->set($offset, $value);
181 }
182
183 public function offsetUnset($offset): void
184 {
185 $this->remove($offset);
186 }
187 }
188