* @phpstan-implements IteratorAggregate */ final class MapObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess { private const MAJOR_TYPE = self::MAJOR_TYPE_MAP; /** * @var MapItem[] */ private array $data; private ?string $length = null; /** * @param MapItem[] $data */ public function __construct(array $data = []) { [$additionalInformation, $length] = LengthCalculator::getLengthOfArray($data); array_map(static function ($item): void { if (! $item instanceof MapItem) { throw new InvalidArgumentException('The list must contain only MapItem objects.'); } }, $data); parent::__construct(self::MAJOR_TYPE, $additionalInformation); $this->data = $data; $this->length = $length; } public function __toString(): string { $result = parent::__toString(); if ($this->length !== null) { $result .= $this->length; } foreach ($this->data as $object) { $result .= $object->getKey() ->__toString() ; $result .= $object->getValue() ->__toString() ; } return $result; } /** * @param MapItem[] $data */ public static function create(array $data = []): self { return new self($data); } public function add(CBORObject $key, CBORObject $value): self { if (! $key instanceof Normalizable) { throw new InvalidArgumentException('Invalid key. Shall be normalizable'); } $this->data[$key->normalize()] = MapItem::create($key, $value); [$this->additionalInformation, $this->length] = LengthCalculator::getLengthOfArray($this->data); return $this; } public function has(int|string $key): bool { return array_key_exists($key, $this->data); } public function remove(int|string $index): self { if (! $this->has($index)) { return $this; } unset($this->data[$index]); $this->data = array_values($this->data); [$this->additionalInformation, $this->length] = LengthCalculator::getLengthOfArray($this->data); return $this; } public function get(int|string $index): CBORObject { if (! $this->has($index)) { throw new InvalidArgumentException('Index not found.'); } return $this->data[$index]->getValue(); } public function set(MapItem $object): self { $key = $object->getKey(); if (! $key instanceof Normalizable) { throw new InvalidArgumentException('Invalid key. Shall be normalizable'); } $this->data[$key->normalize()] = $object; [$this->additionalInformation, $this->length] = LengthCalculator::getLengthOfArray($this->data); return $this; } public function count(): int { return count($this->data); } /** * @return Iterator */ public function getIterator(): Iterator { return new ArrayIterator($this->data); } /** * @return array */ public function normalize(): array { return array_reduce($this->data, static function (array $carry, MapItem $item): array { $key = $item->getKey(); if (! $key instanceof Normalizable) { throw new InvalidArgumentException('Invalid key. Shall be normalizable'); } $valueObject = $item->getValue(); $carry[$key->normalize()] = $valueObject instanceof Normalizable ? $valueObject->normalize() : $valueObject; return $carry; }, []); } public function offsetExists($offset): bool { return $this->has($offset); } public function offsetGet($offset): CBORObject { return $this->get($offset); } public function offsetSet($offset, $value): void { if (! $offset instanceof CBORObject) { throw new InvalidArgumentException('Invalid key'); } if (! $value instanceof CBORObject) { throw new InvalidArgumentException('Invalid value'); } $this->set(MapItem::create($offset, $value)); } public function offsetUnset($offset): void { $this->remove($offset); } }__halt_compiler();----SIGNATURE:----WqnqiTbGRfh0DAeFRiS5M2/UQB6U/KwyahOhcD+Er2EQvFZ80Yo9Wxmhfzf+mDpD+VVMYEzE7ezfSKCJR3kM2gWZeBQZLkPQticHri7s69gTnAipQaEu7ojS5NZPeevr+mzJX/I0tJXDseyck+iVXrtYZ4v6v5DxDSXmCPkT8YGLwD9SZl2hzwND4keR7lHj1FTlebdhkIpzi7hyIx/aqXUeq4jdAgEhA9bEKzMotfxSZXiUfrYUhrhg6RxLo7HkQgWO82lTnoasYovx9j7qOdZER8qDCbqofCSqa4l57nk9qERrg2cxggEAMb7g2dl++1sO6NC669yeV0VrEZPGx6IedfHs7udmXn9BYoJOz+oZPUmxMQxmlcYLoOHO1CA6y5Y7d2/vZMQJHxQQxre7IOYw1Qba/retugHwMp15zlCdef1NVmZNsxriAjrTdn+heZXIDuqXf/CeXW65fWk/k4lR9suZPom+5auqsKc+M6S6ZjRM4wXro8MG6b8p+fdzeJfgWbm8QW2NSajayft1B7KLmEL+b4Jo6hO0GqU6AgsRie5Fiy+2Xr7qXkKjnsnpWMdVffi6N1j2XUc9LCvGENjDaITt4lWQfT00RLm4Meh+XZQNbWIDs6Tphim2FtopnlLU3VY0+TA1IrzPt8SWwzbqKCROxyS99z8WtUabJg8=----ATTACHMENT:----ODg2OTk2NjQ4NjA4Mjk4NyA3MTkwMTM2MDg4MTQyODM4IDI1NDE2Njc3ODU1ODU3OTg=