| 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 |
* @final |
| 29 |
*/ |
| 30 |
class IndefiniteLengthListObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess |
| 31 |
{ |
| 32 |
private const MAJOR_TYPE = self::MAJOR_TYPE_LIST; |
| 33 |
|
| 34 |
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var CBORObject[] |
| 38 |
*/ |
| 39 |
private $data = []; |
| 40 |
|
| 41 |
public function __construct() |
| 42 |
{ |
| 43 |
parent::__construct(self::MAJOR_TYPE, self::ADDITIONAL_INFORMATION); |
| 44 |
} |
| 45 |
|
| 46 |
public function __toString(): string |
| 47 |
{ |
| 48 |
$result = parent::__toString(); |
| 49 |
foreach ($this->data as $object) { |
| 50 |
$result .= (string) $object; |
| 51 |
} |
| 52 |
|
| 53 |
return $result . "\xFF"; |
| 54 |
} |
| 55 |
|
| 56 |
public static function create(): self |
| 57 |
{ |
| 58 |
return new self(); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @return mixed[] |
| 63 |
*/ |
| 64 |
public function normalize(): array |
| 65 |
{ |
| 66 |
return array_map(static function (CBORObject $object) { |
| 67 |
return $object instanceof Normalizable ? $object->normalize() : $object; |
| 68 |
}, $this->data); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @deprecated The method will be removed on v3.0. Please rely on the CBOR\Normalizable interface |
| 73 |
* |
| 74 |
* @return mixed[] |
| 75 |
*/ |
| 76 |
public function getNormalizedData(bool $ignoreTags = false): array |
| 77 |
{ |
| 78 |
return array_map(static function (CBORObject $object) use ($ignoreTags) { |
| 79 |
return $object->getNormalizedData($ignoreTags); |
| 80 |
}, $this->data); |
| 81 |
} |
| 82 |
|
| 83 |
public function add(CBORObject $item): self |
| 84 |
{ |
| 85 |
$this->data[] = $item; |
| 86 |
|
| 87 |
return $this; |
| 88 |
} |
| 89 |
|
| 90 |
public function has(int $index): bool |
| 91 |
{ |
| 92 |
return array_key_exists($index, $this->data); |
| 93 |
} |
| 94 |
|
| 95 |
public function remove(int $index): self |
| 96 |
{ |
| 97 |
if (! $this->has($index)) { |
| 98 |
return $this; |
| 99 |
} |
| 100 |
unset($this->data[$index]); |
| 101 |
$this->data = array_values($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 |
|
| 123 |
return $this; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @deprecated The method will be removed on v3.0. No replacement |
| 128 |
*/ |
| 129 |
public function count(): int |
| 130 |
{ |
| 131 |
return count($this->data); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* @return Iterator<int, CBORObject> |
| 136 |
*/ |
| 137 |
public function getIterator(): Iterator |
| 138 |
{ |
| 139 |
return new ArrayIterator($this->data); |
| 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 === null) { |
| 155 |
$this->add($value); |
| 156 |
|
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
$this->set($offset, $value); |
| 161 |
} |
| 162 |
|
| 163 |
public function offsetUnset($offset): void |
| 164 |
{ |
| 165 |
$this->remove($offset); |
| 166 |
} |
| 167 |
} |
| 168 |
|