Skip to content

Commit

Permalink
Tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Sep 15, 2021
1 parent 5c3712d commit 9e7ed64
Show file tree
Hide file tree
Showing 46 changed files with 945 additions and 142 deletions.
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Each object have at least:

* a static method `create`. This method will correctly instantiate the object.
* can be converted into a binary string: `$object->__toString();` or `(string) $object`.
* a method `getNormalizedData($ignoreTags = false)` that converts the object into its normalized representation. Tags can be ignored with the first argument set to `true`.
* ~~a method `getNormalizedData($ignoreTags = false)` that converts the object into its normalized representation. Tags can be ignored with the first argument set to `true`.~~ (deprecated in v2.1)

### Positive Integer (Major Type 0)

Expand All @@ -61,8 +61,6 @@ $object = UnsignedIntegerObject::createFromHex('0AFFEBFF');
echo bin2hex((string)$object); // 1a0affebff
```

**Note: the method `getNormalizedData()` will always return integers as strings. This is needed to avoid lack of 64 bits integer support on PHP**

### Negative Integer (Major Type 1)

```php
Expand All @@ -75,8 +73,6 @@ $object = NegativeIntegerObject::create(-1000);
$object = NegativeIntegerObject::create(-10000);
```

**Note: the method `getNormalizedData()` will always return integers as strings. This is needed to avoid lack of 64 bits integer support on PHP**

### Byte String / Indefinite Length Byte String (Major Type 2)

Byte String and Indefinite Length Byte String objects have the same major type but are handled by two different classes in this library.
Expand Down Expand Up @@ -236,13 +232,8 @@ use CBOR\OtherObject\NullObject;
use CBOR\OtherObject\UndefinedObject;

$object = FalseObject::create();
$object->getNormalizedData(); // false

$object = NullObject::create();
$object->getNormalizedData(); // null

$object = UndefinedObject::create();
$object->getNormalizedData(); // 'undefined'
```

## Example
Expand Down Expand Up @@ -334,7 +325,7 @@ $otherObjectManager = OtherObject\OtherObjectManager::create()
$tagManager = Tag\TagObjectManager::create()
->add(Tag\DatetimeTag::class)
->add(Tag\TimestampTag::class)
->add(Tag\PositiveBigIntegerTag::class)
->add(Tag\UnsignedBigIntegerTag::class)
->add(Tag\NegativeBigIntegerTag::class)
->add(Tag\DecimalFractionTag::class)
->add(Tag\BigFloatTag::class)
Expand Down
3 changes: 3 additions & 0 deletions src/ByteStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function getLength(): int
return mb_strlen($this->value, '8bit');
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false): string
{
return $this->value;
Expand Down
18 changes: 18 additions & 0 deletions src/CBORObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,31 @@ interface CBORObject
public const FUTURE_USE_2 = 0b00011101;
public const FUTURE_USE_3 = 0b00011110;

public const TAG_STANDARD_DATETIME = 0;
public const TAG_EPOCH_DATETIME = 1;
public const TAG_UNSIGNED_BIG_NUM = 2;
public const TAG_NEGATIVE_BIG_NUM = 3;
public const TAG_DECIMAL_FRACTION = 4;
public const TAG_BIG_FLOAT = 5;
public const TAG_ENCODED_BASE64_URL = 21;
public const TAG_ENCODED_BASE64 = 22;
public const TAG_ENCODED_BASE16 = 23;
public const TAG_ENCODED_CBOR = 24;
public const TAG_URI = 32;
public const TAG_BASE64_URL = 33;
public const TAG_BASE64 = 34;
public const TAG_MIME = 36;
public const TAG_CBOR = 55799;

public function __toString(): string;

public function getMajorType(): int;

public function getAdditionalInformation(): int;

/**
* @deprecated The method will be removed on v3.0. No replacement
*
* @return mixed|null
*/
public function getNormalizedData(bool $ignoreTags = false);
Expand Down
18 changes: 9 additions & 9 deletions src/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,21 @@ private function processFinite(Stream $stream, int $mt, int $ai, ?string $val):
case CBORObject::MAJOR_TYPE_BYTE_STRING: //2
$length = null === $val ? $ai : Utils::binToInt($val);

return new ByteStringObject($stream->read($length));
return ByteStringObject::create($stream->read($length));
case CBORObject::MAJOR_TYPE_TEXT_STRING: //3
$length = null === $val ? $ai : Utils::binToInt($val);

return new TextStringObject($stream->read($length));
return TextStringObject::create($stream->read($length));
case CBORObject::MAJOR_TYPE_LIST: //4
$object = new ListObject();
$object = ListObject::create();
$nbItems = null === $val ? $ai : Utils::binToInt($val);
for ($i = 0; $i < $nbItems; ++$i) {
$object->add($this->process($stream, false));
}

return $object;
case CBORObject::MAJOR_TYPE_MAP: //5
$object = new MapObject();
$object = MapObject::create();
$nbItems = null === $val ? $ai : Utils::binToInt($val);
for ($i = 0; $i < $nbItems; ++$i) {
$object->add($this->process($stream, false), $this->process($stream, false));
Expand All @@ -116,7 +116,7 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR
{
switch ($mt) {
case CBORObject::MAJOR_TYPE_BYTE_STRING: //2
$object = new IndefiniteLengthByteStringObject();
$object = IndefiniteLengthByteStringObject::create();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
if (!$it instanceof ByteStringObject) {
throw new RuntimeException('Unable to parse the data. Infinite Byte String object can only get Byte String objects.');
Expand All @@ -126,7 +126,7 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR

return $object;
case CBORObject::MAJOR_TYPE_TEXT_STRING: //3
$object = new IndefiniteLengthTextStringObject();
$object = IndefiniteLengthTextStringObject::create();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
if (!$it instanceof TextStringObject) {
throw new RuntimeException('Unable to parse the data. Infinite Text String object can only get Text String objects.');
Expand All @@ -136,14 +136,14 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR

return $object;
case CBORObject::MAJOR_TYPE_LIST: //4
$object = new IndefiniteLengthListObject();
$object = IndefiniteLengthListObject::create();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
$object->add($it);
}

return $object;
case CBORObject::MAJOR_TYPE_MAP: //5
$object = new IndefiniteLengthMapObject();
$object = IndefiniteLengthMapObject::create();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
$object->append($it, $this->process($stream, false));
}
Expand All @@ -154,7 +154,7 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR
throw new InvalidArgumentException('Cannot parse the data. No enclosing indefinite.');
}

return new BreakObject();
return BreakObject::create();
case CBORObject::MAJOR_TYPE_UNSIGNED_INTEGER: //0
case CBORObject::MAJOR_TYPE_NEGATIVE_INTEGER: //1
case CBORObject::MAJOR_TYPE_TAG: //6
Expand Down
3 changes: 3 additions & 0 deletions src/IndefiniteLengthByteStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public function getLength(): int
return $length;
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false): string
{
$result = '';
Expand Down
5 changes: 5 additions & 0 deletions src/IndefiniteLengthListObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public function __toString(): string
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*
* @return mixed[]
*/
public function getNormalizedData(bool $ignoreTags = false): array
Expand All @@ -76,6 +78,9 @@ public function add(CBORObject $item): self
return $this;
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function count(): int
{
return count($this->data);
Expand Down
5 changes: 5 additions & 0 deletions src/IndefiniteLengthMapObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public function append(CBORObject $key, CBORObject $value): self
return $this;
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function count(): int
{
return count($this->data);
Expand All @@ -81,6 +84,8 @@ public function getIterator(): Iterator
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*
* @return mixed[]
*/
public function getNormalizedData(bool $ignoreTags = false): array
Expand Down
3 changes: 3 additions & 0 deletions src/IndefiniteLengthTextStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public function getLength(): int
return $length;
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false): string
{
$result = '';
Expand Down
2 changes: 2 additions & 0 deletions src/ListObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public function get(int $index): CBORObject
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*
* @return array<int|string, mixed>
*/
public function getNormalizedData(bool $ignoreTags = false): array
Expand Down
2 changes: 2 additions & 0 deletions src/MapObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public function getIterator(): Iterator
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*
* @return array<int|string, mixed>
*/
public function getNormalizedData(bool $ignoreTags = false): array
Expand Down
13 changes: 8 additions & 5 deletions src/NegativeIntegerObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ public static function createFromString(string $value): self
}

public function getValue(): string
{
return $this->getNormalizedData();
}

public function getNormalizedData(bool $ignoreTags = false): string
{
if (null === $this->data) {
return (string) (-1 - $this->additionalInformation);
Expand All @@ -78,6 +73,14 @@ public function getNormalizedData(bool $ignoreTags = false): string
return $minusOne->minus($result)->toBase(10);
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false): string
{
return $this->getValue();
}

private static function createBigInteger(BigInteger $integer): self
{
if ($integer->isGreaterThanOrEqualTo(BigInteger::zero())) {
Expand Down
3 changes: 3 additions & 0 deletions src/OtherObject/BreakObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string
return new self();
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false): bool
{
return false;
Expand Down
3 changes: 3 additions & 0 deletions src/OtherObject/DoublePrecisionFloatObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public static function create(string $value): self
return new self(27, $value);
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false)
{
$exp = $this->getExponent();
Expand Down
3 changes: 3 additions & 0 deletions src/OtherObject/FalseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string
return new self();
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false): bool
{
return false;
Expand Down
3 changes: 3 additions & 0 deletions src/OtherObject/GenericObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string
return new self($additionalInformation, $data);
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false)
{
return $this->data;
Expand Down
3 changes: 3 additions & 0 deletions src/OtherObject/HalfPrecisionFloatObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public static function create(string $value): self
return new self(25, $value);
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false)
{
$exp = $this->getExponent();
Expand Down
3 changes: 3 additions & 0 deletions src/OtherObject/NullObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string
return new self();
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false)
{
}
Expand Down
12 changes: 10 additions & 2 deletions src/OtherObject/SimpleObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,27 @@
use CBOR\Utils;
use function chr;
use InvalidArgumentException;
use function ord;

final class SimpleObject extends Base
{
public static function supportedAdditionalInformation(): array
{
return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 24];
return range(0, 31);
}

public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
{
if (null !== $data && ord($data) < 32) {
throw new InvalidArgumentException('Invalid simple value. Content data should not be present.');
}

return new self($additionalInformation, $data);
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false)
{
if (null === $this->data) {
Expand All @@ -45,7 +53,7 @@ public function getNormalizedData(bool $ignoreTags = false)
public static function create(int $value): self
{
switch (true) {
case $value < 24:
case $value < 32:
return new self($value, null);
case $value < 256:
return new self(24, chr($value));
Expand Down
3 changes: 3 additions & 0 deletions src/OtherObject/SinglePrecisionFloatObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public static function create(string $value): self
return new self(26, $value);
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false)
{
$exp = $this->getExponent();
Expand Down
3 changes: 3 additions & 0 deletions src/OtherObject/TrueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string
return new self();
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false): bool
{
return true;
Expand Down
3 changes: 3 additions & 0 deletions src/OtherObject/UndefinedObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public static function createFromLoadedData(int $additionalInformation, ?string
return new self();
}

/**
* @deprecated The method will be removed on v3.0. No replacement
*/
public function getNormalizedData(bool $ignoreTags = false)
{
return 'undefined';
Expand Down
Loading

0 comments on commit 9e7ed64

Please sign in to comment.