Skip to content

Commit

Permalink
List and Map Objects tests and ArrayAccess
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Sep 30, 2021
1 parent 1588030 commit 08594e6
Show file tree
Hide file tree
Showing 6 changed files with 526 additions and 24 deletions.
70 changes: 69 additions & 1 deletion src/IndefiniteLengthListObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@

namespace CBOR;

use function array_key_exists;
use ArrayAccess;
use ArrayIterator;
use function count;
use Countable;
use InvalidArgumentException;
use Iterator;
use IteratorAggregate;

/**
* @phpstan-implements IteratorAggregate<int, CBORObject>
* @final
*/
class IndefiniteLengthListObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable
class IndefiniteLengthListObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess
{
private const MAJOR_TYPE = self::MAJOR_TYPE_LIST;
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;
Expand Down Expand Up @@ -81,6 +84,42 @@ public function add(CBORObject $item): self
return $this;
}

public function has(int $index): bool
{
return array_key_exists($index, $this->data);
}

public function remove(int $index): self
{
if (!$this->has($index)) {
return $this;
}
unset($this->data[$index]);
$this->data = array_values($this->data);

return $this;
}

public function get(int $index): CBORObject
{
if (!$this->has($index)) {
throw new InvalidArgumentException('Index not found.');
}

return $this->data[$index];
}

public function set(int $index, CBORObject $object): self
{
if (!$this->has($index)) {
throw new InvalidArgumentException('Index not found.');
}

$this->data[$index] = $object;

return $this;
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
Expand All @@ -96,4 +135,33 @@ public function getIterator(): Iterator
{
return new ArrayIterator($this->data);
}

public function offsetExists($offset): bool
{
return $this->has($offset);
}

/**
* @param int $offset
*/
public function offsetGet($offset): CBORObject
{
return $this->get($offset);
}

public function offsetSet($offset, $value): void
{
if (null === $offset) {
$this->add($value);

return;
}

$this->set($offset, $value);
}

public function offsetUnset($offset): void
{
$this->remove($offset);
}
}
111 changes: 101 additions & 10 deletions src/IndefiniteLengthMapObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace CBOR;

use function array_key_exists;
use ArrayAccess;
use ArrayIterator;
use function count;
use Countable;
Expand All @@ -24,7 +26,7 @@
* @phpstan-implements IteratorAggregate<int, MapItem>
* @final
*/
class IndefiniteLengthMapObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable
class IndefiniteLengthMapObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess
{
private const MAJOR_TYPE = self::MAJOR_TYPE_MAP;
private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE;
Expand Down Expand Up @@ -56,9 +58,66 @@ public function __toString(): string
return $result;
}

/**
* @deprecated The method will be removed on v3.0. Please use "add" instead
*/
public function append(CBORObject $key, CBORObject $value): self
{
$this->data[] = MapItem::create($key, $value);
return $this->add($key, $value);
}

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);

return $this;
}

/**
* @param int|string $key
*/
public function has($key): bool
{
return array_key_exists($key, $this->data);
}

/**
* @param int|string $index
*/
public function remove($index): self
{
if (!$this->has($index)) {
return $this;
}
unset($this->data[$index]);
$this->data = array_values($this->data);

return $this;
}

/**
* @param int|string $index
*/
public function get($index): MapItem
{
if (!$this->has($index)) {
throw new InvalidArgumentException('Index not found.');
}

return $this->data[$index];
}

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;

return $this;
}
Expand All @@ -84,17 +143,16 @@ public function getIterator(): Iterator
*/
public function normalize(): array
{
$result = [];
foreach ($this->data as $object) {
$keyObject = $object->getKey();
if (!$keyObject instanceof Normalizable) {
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 = $object->getValue();
$result[$keyObject->normalize()] = $valueObject instanceof Normalizable ? $valueObject->normalize() : $object;
}
$valueObject = $item->getValue();
$carry[$key->normalize()] = $valueObject instanceof Normalizable ? $valueObject->normalize() : $valueObject;

return $result;
return $carry;
}, []);
}

/**
Expand All @@ -106,4 +164,37 @@ public function getNormalizedData(bool $ignoreTags = false): array
{
return $this->normalize();
}

/**
* @param int|string $offset
*/
public function offsetExists($offset): bool
{
return $this->has($offset);
}

/**
* @param int|string $offset
*/
public function offsetGet($offset): MapItem
{
return $this->get($offset);
}

public function offsetSet($offset, $value): void
{
if (!$offset instanceof CBORObject && !$offset instanceof Normalizable) {
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);
}
}
65 changes: 62 additions & 3 deletions src/ListObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CBOR;

use function array_key_exists;
use ArrayAccess;
use ArrayIterator;
use function count;
use Countable;
Expand All @@ -24,7 +25,7 @@
/**
* @phpstan-implements IteratorAggregate<int, CBORObject>
*/
class ListObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable
class ListObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess
{
private const MAJOR_TYPE = self::MAJOR_TYPE_LIST;

Expand All @@ -51,7 +52,7 @@ public function __construct(array $data = [])
}, $data);

parent::__construct(self::MAJOR_TYPE, $additionalInformation);
$this->data = $data;
$this->data = array_values($data);
$this->length = $length;
}

Expand Down Expand Up @@ -84,15 +85,44 @@ public function add(CBORObject $object): self
return $this;
}

public function has(int $index): bool
{
return array_key_exists($index, $this->data);
}

public function remove(int $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 $index): CBORObject
{
if (!array_key_exists($index, $this->data)) {
if (!$this->has($index)) {
throw new InvalidArgumentException('Index not found.');
}

return $this->data[$index];
}

public function set(int $index, CBORObject $object): self
{
if (!$this->has($index)) {
throw new InvalidArgumentException('Index not found.');
}

$this->data[$index] = $object;
[$this->additionalInformation, $this->length] = LengthCalculator::getLengthOfArray($this->data);

return $this;
}

/**
* @return array<int, mixed>
*/
Expand Down Expand Up @@ -125,4 +155,33 @@ public function getIterator(): Iterator
{
return new ArrayIterator($this->data);
}

public function offsetExists($offset): bool
{
return $this->has($offset);
}

/**
* @param int $offset
*/
public function offsetGet($offset): CBORObject
{
return $this->get($offset);
}

public function offsetSet($offset, $value): void
{
if (null === $offset) {
$this->add($value);

return;
}

$this->set($offset, $value);
}

public function offsetUnset($offset): void
{
$this->remove($offset);
}
}
Loading

0 comments on commit 08594e6

Please sign in to comment.