| 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 |
*/ |
| 29 |
final class MapObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess |
| 30 |
{ |
| 31 |
private const MAJOR_TYPE = self::MAJOR_TYPE_MAP; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var MapItem[] |
| 35 |
*/ |
| 36 |
private $data; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var string|null |
| 40 |
*/ |
| 41 |
private $length; |
| 42 |
|
| 43 |
/** |
| 44 |
* @param MapItem[] $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 MapItem) { |
| 51 |
throw new InvalidArgumentException('The list must contain only MapItem objects.'); |
| 52 |
} |
| 53 |
}, $data); |
| 54 |
|
| 55 |
parent::__construct(self::MAJOR_TYPE, $additionalInformation); |
| 56 |
$this->data = $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 .= $object->getKey() |
| 68 |
->__toString() |
| 69 |
; |
| 70 |
$result .= $object->getValue() |
| 71 |
->__toString() |
| 72 |
; |
| 73 |
} |
| 74 |
|
| 75 |
return $result; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @param MapItem[] $data |
| 80 |
*/ |
| 81 |
public static function create(array $data = []): self |
| 82 |
{ |
| 83 |
return new self($data); |
| 84 |
} |
| 85 |
|
| 86 |
public function add(CBORObject $key, CBORObject $value): self |
| 87 |
{ |
| 88 |
if (! $key instanceof Normalizable) { |
| 89 |
throw new InvalidArgumentException('Invalid key. Shall be normalizable'); |
| 90 |
} |
| 91 |
$this->data[$key->normalize()] = MapItem::create($key, $value); |
| 92 |
[$this->additionalInformation, $this->length] = LengthCalculator::getLengthOfArray($this->data); |
| 93 |
|
| 94 |
return $this; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @param int|string $key |
| 99 |
*/ |
| 100 |
public function has($key): bool |
| 101 |
{ |
| 102 |
return array_key_exists($key, $this->data); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @param int|string $index |
| 107 |
*/ |
| 108 |
public function remove($index): self |
| 109 |
{ |
| 110 |
if (! $this->has($index)) { |
| 111 |
return $this; |
| 112 |
} |
| 113 |
unset($this->data[$index]); |
| 114 |
$this->data = array_values($this->data); |
| 115 |
[$this->additionalInformation, $this->length] = LengthCalculator::getLengthOfArray($this->data); |
| 116 |
|
| 117 |
return $this; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @param int|string $index |
| 122 |
*/ |
| 123 |
public function get($index): CBORObject |
| 124 |
{ |
| 125 |
if (! $this->has($index)) { |
| 126 |
throw new InvalidArgumentException('Index not found.'); |
| 127 |
} |
| 128 |
|
| 129 |
return $this->data[$index]->getValue(); |
| 130 |
} |
| 131 |
|
| 132 |
public function set(MapItem $object): self |
| 133 |
{ |
| 134 |
$key = $object->getKey(); |
| 135 |
if (! $key instanceof Normalizable) { |
| 136 |
throw new InvalidArgumentException('Invalid key. Shall be normalizable'); |
| 137 |
} |
| 138 |
|
| 139 |
$this->data[$key->normalize()] = $object; |
| 140 |
[$this->additionalInformation, $this->length] = LengthCalculator::getLengthOfArray($this->data); |
| 141 |
|
| 142 |
return $this; |
| 143 |
} |
| 144 |
|
| 145 |
public function count(): int |
| 146 |
{ |
| 147 |
return count($this->data); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @return Iterator<int, MapItem> |
| 152 |
*/ |
| 153 |
public function getIterator(): Iterator |
| 154 |
{ |
| 155 |
return new ArrayIterator($this->data); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* @return array<int|string, mixed> |
| 160 |
*/ |
| 161 |
public function normalize(): array |
| 162 |
{ |
| 163 |
return array_reduce($this->data, static function (array $carry, MapItem $item): array { |
| 164 |
$key = $item->getKey(); |
| 165 |
if (! $key instanceof Normalizable) { |
| 166 |
throw new InvalidArgumentException('Invalid key. Shall be normalizable'); |
| 167 |
} |
| 168 |
$valueObject = $item->getValue(); |
| 169 |
$carry[$key->normalize()] = $valueObject instanceof Normalizable ? $valueObject->normalize() : $valueObject; |
| 170 |
|
| 171 |
return $carry; |
| 172 |
}, []); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* @deprecated The method will be removed on v3.0. Please rely on the CBOR\Normalizable interface |
| 177 |
* |
| 178 |
* @return array<int|string, mixed> |
| 179 |
*/ |
| 180 |
public function getNormalizedData(bool $ignoreTags = false): array |
| 181 |
{ |
| 182 |
return array_reduce($this->data, static function (array $carry, MapItem $item) use ($ignoreTags): array { |
| 183 |
$key = $item->getKey(); |
| 184 |
$valueObject = $item->getValue(); |
| 185 |
$carry[$key->getNormalizedData($ignoreTags)] = $valueObject->getNormalizedData($ignoreTags); |
| 186 |
|
| 187 |
return $carry; |
| 188 |
}, []); |
| 189 |
} |
| 190 |
|
| 191 |
public function offsetExists($offset): bool |
| 192 |
{ |
| 193 |
return $this->has($offset); |
| 194 |
} |
| 195 |
|
| 196 |
public function offsetGet($offset): CBORObject |
| 197 |
{ |
| 198 |
return $this->get($offset); |
| 199 |
} |
| 200 |
|
| 201 |
public function offsetSet($offset, $value): void |
| 202 |
{ |
| 203 |
if (! $offset instanceof CBORObject) { |
| 204 |
throw new InvalidArgumentException('Invalid key'); |
| 205 |
} |
| 206 |
if (! $value instanceof CBORObject) { |
| 207 |
throw new InvalidArgumentException('Invalid value'); |
| 208 |
} |
| 209 |
|
| 210 |
$this->set(MapItem::create($offset, $value)); |
| 211 |
} |
| 212 |
|
| 213 |
public function offsetUnset($offset): void |
| 214 |
{ |
| 215 |
$this->remove($offset); |
| 216 |
} |
| 217 |
} |
| 218 |
|