| 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, MapItem> |
| 28 |
* @final |
| 29 |
*/ |
| 30 |
class IndefiniteLengthMapObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess |
| 31 |
{ |
| 32 |
private const MAJOR_TYPE = self::MAJOR_TYPE_MAP; |
| 33 |
|
| 34 |
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var MapItem[] |
| 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->getKey(); |
| 51 |
$result .= (string) $object->getValue(); |
| 52 |
} |
| 53 |
|
| 54 |
return $result . "\xFF"; |
| 55 |
} |
| 56 |
|
| 57 |
public static function create(): self |
| 58 |
{ |
| 59 |
return new self(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @deprecated The method will be removed on v3.0. Please use "add" instead |
| 64 |
*/ |
| 65 |
public function append(CBORObject $key, CBORObject $value): self |
| 66 |
{ |
| 67 |
return $this->add($key, $value); |
| 68 |
} |
| 69 |
|
| 70 |
public function add(CBORObject $key, CBORObject $value): self |
| 71 |
{ |
| 72 |
if (! $key instanceof Normalizable) { |
| 73 |
throw new InvalidArgumentException('Invalid key. Shall be normalizable'); |
| 74 |
} |
| 75 |
$this->data[$key->normalize()] = MapItem::create($key, $value); |
| 76 |
|
| 77 |
return $this; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @param int|string $key |
| 82 |
*/ |
| 83 |
public function has($key): bool |
| 84 |
{ |
| 85 |
return array_key_exists($key, $this->data); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @param int|string $index |
| 90 |
*/ |
| 91 |
public function remove($index): self |
| 92 |
{ |
| 93 |
if (! $this->has($index)) { |
| 94 |
return $this; |
| 95 |
} |
| 96 |
unset($this->data[$index]); |
| 97 |
$this->data = array_values($this->data); |
| 98 |
|
| 99 |
return $this; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @param int|string $index |
| 104 |
*/ |
| 105 |
public function get($index): CBORObject |
| 106 |
{ |
| 107 |
if (! $this->has($index)) { |
| 108 |
throw new InvalidArgumentException('Index not found.'); |
| 109 |
} |
| 110 |
|
| 111 |
return $this->data[$index]->getValue(); |
| 112 |
} |
| 113 |
|
| 114 |
public function set(MapItem $object): self |
| 115 |
{ |
| 116 |
$key = $object->getKey(); |
| 117 |
if (! $key instanceof Normalizable) { |
| 118 |
throw new InvalidArgumentException('Invalid key. Shall be normalizable'); |
| 119 |
} |
| 120 |
|
| 121 |
$this->data[$key->normalize()] = $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, MapItem> |
| 136 |
*/ |
| 137 |
public function getIterator(): Iterator |
| 138 |
{ |
| 139 |
return new ArrayIterator($this->data); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @return mixed[] |
| 144 |
*/ |
| 145 |
public function normalize(): array |
| 146 |
{ |
| 147 |
return array_reduce($this->data, static function (array $carry, MapItem $item): array { |
| 148 |
$key = $item->getKey(); |
| 149 |
if (! $key instanceof Normalizable) { |
| 150 |
throw new InvalidArgumentException('Invalid key. Shall be normalizable'); |
| 151 |
} |
| 152 |
$valueObject = $item->getValue(); |
| 153 |
$carry[$key->normalize()] = $valueObject instanceof Normalizable ? $valueObject->normalize() : $valueObject; |
| 154 |
|
| 155 |
return $carry; |
| 156 |
}, []); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* @deprecated The method will be removed on v3.0. Please rely on the CBOR\Normalizable interface |
| 161 |
* |
| 162 |
* @return mixed[] |
| 163 |
*/ |
| 164 |
public function getNormalizedData(bool $ignoreTags = false): array |
| 165 |
{ |
| 166 |
return array_reduce($this->data, static function (array $carry, MapItem $item) use ($ignoreTags): array { |
| 167 |
$key = $item->getKey(); |
| 168 |
$valueObject = $item->getValue(); |
| 169 |
$carry[$key->getNormalizedData($ignoreTags)] = $valueObject->getNormalizedData($ignoreTags); |
| 170 |
|
| 171 |
return $carry; |
| 172 |
}, []); |
| 173 |
} |
| 174 |
|
| 175 |
public function offsetExists($offset): bool |
| 176 |
{ |
| 177 |
return $this->has($offset); |
| 178 |
} |
| 179 |
|
| 180 |
public function offsetGet($offset): CBORObject |
| 181 |
{ |
| 182 |
return $this->get($offset); |
| 183 |
} |
| 184 |
|
| 185 |
public function offsetSet($offset, $value): void |
| 186 |
{ |
| 187 |
if (! $offset instanceof CBORObject) { |
| 188 |
throw new InvalidArgumentException('Invalid key'); |
| 189 |
} |
| 190 |
if (! $value instanceof CBORObject) { |
| 191 |
throw new InvalidArgumentException('Invalid value'); |
| 192 |
} |
| 193 |
|
| 194 |
$this->set(MapItem::create($offset, $value)); |
| 195 |
} |
| 196 |
|
| 197 |
public function offsetUnset($offset): void |
| 198 |
{ |
| 199 |
$this->remove($offset); |
| 200 |
} |
| 201 |
} |
| 202 |
|