Skip to content

Commit

Permalink
Tags fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Sep 17, 2021
1 parent fd0687c commit c1eb55a
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 80 deletions.
28 changes: 9 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,31 +278,21 @@ $object = MapObject::create()
```

The encoded result will be `0xa37428efbda1e29795e280bfe29795efbda129e29aa183f5f4c482211904d21907d0f66464617465c11a5e0be100`.
The normalized result is:

```php
array:3 [
"(。◕‿◕。)⚡" => array:3 [
0 => true
1 => false
2 => "12.34"
]
2000 => null
"date" => DateTimeImmutable @1577836800 {
date: 2020-01-01 00:00:00.0 +00:00
}
]
## Object Loading

```
If you want to load a CBOR encoded string, you just have to instantiate a `CBOR\Decoder` class.
This class does not need any argument.

```php
<?php

## Object Loading
use CBOR\Decoder;

If you want to load a CBOR encoded string, you just have to instantiate a `CBOR\Decoder` class.
This class needs the following arguments:
$decoder = Decoder::create();
```

* A `Tag` manager: this manager will be able to identify the tags associated to the data and create it accordingly if the tag ID is supported.
* An `Other Object` manager: this manager will be able to identify all other objects and create it accordingly if supported.
If needed, you can define custom sets of Tag and Other Object support managers.

```php
<?php
Expand Down
38 changes: 38 additions & 0 deletions src/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace CBOR;

use InvalidArgumentException;

abstract class Tag extends AbstractCBORObject
{
private const MAJOR_TYPE = self::MAJOR_TYPE_TAG;
Expand Down Expand Up @@ -45,6 +47,11 @@ public function __toString(): string
return $result;
}

public function getData(): ?string
{
return $this->data;
}

abstract public static function getTagId(): int;

abstract public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): self;
Expand All @@ -53,4 +60,35 @@ public function getValue(): CBORObject
{
return $this->object;
}

/**
* @return array{int, null|string}
*/
protected static function determineComponents(int $tag): array
{
switch (true) {
case $tag < 0:
throw new InvalidArgumentException('The value must be a positive integer.');
case $tag < 24:
return [$tag, null];
case $tag < 0xFF:
return [24, self::hex2bin(dechex($tag))];
case $tag < 0xFFFF:
return [25, self::hex2bin(dechex($tag))];
case $tag < 0xFFFFFFFF:
return [26, self::hex2bin(dechex($tag))];
default:
throw new InvalidArgumentException('Out of range. Please use PositiveBigIntegerTag tag with ByteStringObject object instead.');
}
}

private static function hex2bin(string $data): string
{
$result = hex2bin($data);
if (false === $result) {
throw new InvalidArgumentException('Unable to convert the data');
}

return $result;
}
}
4 changes: 3 additions & 1 deletion src/Tag/Base16EncodingTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(self::TAG_ENCODED_BASE16, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_ENCODED_BASE16);

return new self($ai, $data, $object);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Tag/Base64EncodingTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(self::TAG_ENCODED_BASE64, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_ENCODED_BASE64);

return new self($ai, $data, $object);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Tag/Base64Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(self::TAG_BASE64, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_BASE64);

return new self($ai, $data, $object);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Tag/Base64UrlEncodingTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(self::TAG_ENCODED_BASE64_URL, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_ENCODED_BASE64_URL);

return new self($ai, $data, $object);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Tag/Base64UrlTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(self::TAG_BASE64_URL, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_BASE64_URL);

return new self($ai, $data, $object);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Tag/BigFloatTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(self::TAG_BIG_FLOAT, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_BIG_FLOAT);

return new self($ai, $data, $object);
}

public static function createFromExponentAndMantissa(CBORObject $e, CBORObject $m): Tag
Expand Down
4 changes: 3 additions & 1 deletion src/Tag/CBOREncodingTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(self::TAG_ENCODED_CBOR, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_ENCODED_CBOR);

return new self($ai, $data, $object);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Tag/CBORTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(self::TAG_CBOR, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_CBOR);

return new self($ai, $data, $object);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Tag/DatetimeTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(self::TAG_STANDARD_DATETIME, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_STANDARD_DATETIME);

return new self($ai, $data, $object);
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/Tag/DecimalFractionTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

final class DecimalFractionTag extends Tag
{
public function __construct(CBORObject $object)
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
{
if (!extension_loaded('bcmath')) {
throw new RuntimeException('The extension "bcmath" is required to use this tag');
Expand All @@ -42,7 +42,14 @@ public function __construct(CBORObject $object)
throw new InvalidArgumentException('The mantissa must be a Positive or Negative Signed Integer or an Unsigned Integer object.');
}

parent::__construct(self::TAG_DECIMAL_FRACTION, null, $object);
parent::__construct($additionalInformation, $data, $object);
}

public static function create(CBORObject $object): self
{
[$ai, $data] = self::determineComponents(self::TAG_DECIMAL_FRACTION);

return new self($ai, $data, $object);
}

public static function getTagId(): int
Expand All @@ -52,7 +59,7 @@ public static function getTagId(): int

public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
{
return new self($object);
return new self($additionalInformation, $data, $object);
}

public static function createFromExponentAndMantissa(CBORObject $e, CBORObject $m): Tag
Expand All @@ -62,7 +69,7 @@ public static function createFromExponentAndMantissa(CBORObject $e, CBORObject $
->add($m)
;

return new self($object);
return self::create($object);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Tag/MimeTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(self::TAG_MIME, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_MIME);

return new self($ai, $data, $object);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Tag/NegativeBigIntegerTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(self::TAG_NEGATIVE_BIG_NUM, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_NEGATIVE_BIG_NUM);

return new self($ai, $data, $object);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Tag/TimestampTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(1, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_EPOCH_DATETIME);

return new self($ai, $data, $object);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Tag/UnsignedBigIntegerTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(self::TAG_UNSIGNED_BIG_NUM, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_UNSIGNED_BIG_NUM);

return new self($ai, $data, $object);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Tag/UriTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string

public static function create(CBORObject $object): Tag
{
return new self(self::TAG_URI, null, $object);
[$ai, $data] = self::determineComponents(self::TAG_URI);

return new self($ai, $data, $object);
}

/**
Expand Down
40 changes: 1 addition & 39 deletions tests/Type/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
namespace CBOR\Test\Type;

use CBOR\Decoder;
use CBOR\OtherObject;
use CBOR\OtherObject\OtherObjectManager;
use CBOR\Tag;
use CBOR\Tag\TagManager;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -33,41 +29,7 @@ class BaseTestCase extends TestCase
protected function getDecoder(): Decoder
{
if (null === $this->decoder) {
$otherObjectManager = OtherObjectManager::create()
->add(OtherObject\BreakObject::class)
->add(OtherObject\SimpleObject::class)
->add(OtherObject\FalseObject::class)
->add(OtherObject\TrueObject::class)
->add(OtherObject\NullObject::class)
->add(OtherObject\UndefinedObject::class)
->add(OtherObject\HalfPrecisionFloatObject::class)
->add(OtherObject\SinglePrecisionFloatObject::class)
->add(OtherObject\DoublePrecisionFloatObject::class)
;
$tagObjectManager = TagManager::create()
->add(Tag\DatetimeTag::class)
->add(Tag\TimestampTag::class)

->add(Tag\UnsignedBigIntegerTag::class)
->add(Tag\NegativeBigIntegerTag::class)

->add(Tag\DecimalFractionTag::class)
->add(Tag\BigFloatTag::class)

->add(Tag\Base64UrlEncodingTag::class)
->add(Tag\Base64EncodingTag::class)
->add(Tag\Base16EncodingTag::class)
->add(Tag\CBOREncodingTag::class)

->add(Tag\UriTag::class)
->add(Tag\Base64UrlTag::class)
->add(Tag\Base64Tag::class)
->add(Tag\MimeTag::class)

->add(Tag\CBORTag::class)
;

$this->decoder = Decoder::create($tagObjectManager, $otherObjectManager);
$this->decoder = Decoder::create();
}

return $this->decoder;
Expand Down
12 changes: 8 additions & 4 deletions tests/Type/Tag/SimpleTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function createValidUriTag(): void

static::assertInstanceOf(TextStringObject::class, $tag->getValue());
static::assertEquals(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType());
static::assertEquals(CBORObject::TAG_URI, $tag->getAdditionalInformation());
static::assertEquals(CBORObject::LENGTH_1_BYTE, $tag->getAdditionalInformation());
static::assertEquals(hex2bin(dechex(CBORObject::TAG_URI)), $tag->getData());
}

/**
Expand All @@ -65,7 +66,8 @@ public function createValidBase64Tag(): void

static::assertInstanceOf(TextStringObject::class, $tag->getValue());
static::assertEquals(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType());
static::assertEquals(CBORObject::TAG_BASE64, $tag->getAdditionalInformation());
static::assertEquals(CBORObject::LENGTH_1_BYTE, $tag->getAdditionalInformation());
static::assertEquals(hex2bin(dechex(CBORObject::TAG_BASE64)), $tag->getData());
}

/**
Expand All @@ -91,7 +93,8 @@ public function createValidBase64UrlTag(): void

static::assertInstanceOf(TextStringObject::class, $tag->getValue());
static::assertEquals(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType());
static::assertEquals(CBORObject::TAG_BASE64_URL, $tag->getAdditionalInformation());
static::assertEquals(CBORObject::LENGTH_1_BYTE, $tag->getAdditionalInformation());
static::assertEquals(hex2bin(dechex(CBORObject::TAG_BASE64_URL)), $tag->getData());
}

/**
Expand All @@ -117,6 +120,7 @@ public function createValidCBORTag(): void

static::assertInstanceOf(ByteStringObject::class, $tag->getValue());
static::assertEquals(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType());
static::assertEquals(CBORObject::TAG_CBOR, $tag->getAdditionalInformation());
static::assertEquals(CBORObject::LENGTH_2_BYTES, $tag->getAdditionalInformation());
static::assertEquals(hex2bin(dechex(CBORObject::TAG_CBOR)), $tag->getData());
}
}

0 comments on commit c1eb55a

Please sign in to comment.