diff --git a/Makefile b/Makefile index 7f3fe14..3cc25d4 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ it: coding-standards tests static-analyse performance-tests mutation-tests .PHONY: code-coverage -code-coverage: vendor ## Show test coverage rates +code-coverage: vendor ## Show test coverage rates (console) vendor/bin/phpunit --coverage-text .PHONY: code-coverage-html @@ -11,16 +11,12 @@ code-coverage-html: vendor ## Show test coverage rates (HTML) .PHONY: fix-coding-standards fix-coding-standards: vendor ## Fix all files using defined PHP-CS-FIXER rules - vendor/bin/php-cs-fixer fix --diff --verbose + vendor/bin/php-cs-fixer fix .PHONY: coding-standards coding-standards: vendor ## Check all files using defined PHP-CS-FIXER rules vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation --using-cache=no -.PHONY: mutation-tests -mutation-tests: vendor ## Run mutation tests with minimum MSI and covered MSI enabled - vendor/bin/infection --logger-github --git-diff-filter=AM -s --threads=$(nproc) --min-msi=50 --min-covered-msi=59 - .PHONY: tests tests: vendor ## Run all tests vendor/bin/phpunit --color @@ -30,21 +26,17 @@ vendor: composer.json composer.lock composer install .PHONY: tu -tu: vendor ## Run all unit tests +tu: vendor ## Run only unit tests vendor/bin/phpunit --color --group Unit .PHONY: tf -tf: vendor ## Run all functional tests +tf: vendor ## Run only functional tests vendor/bin/phpunit --color --group Functional .PHONY: static-analyse static-analyse: vendor ## Run static analyse vendor/bin/phpstan analyse -.PHONY: performance-tests -performance-tests: vendor ## Run performance test suite - vendor/bin/phpbench run -l dots --report aggregate - .PHONY: rector rector: vendor ## Check all files using Rector vendor/bin/rector process --ansi --dry-run diff --git a/README.md b/README.md index 32c99d5..0a340a9 100644 --- a/README.md +++ b/README.md @@ -322,7 +322,7 @@ $otherObjectManager = OtherObject\OtherObjectManager::create() ->add(OtherObject\DoublePrecisionFloatObject::class) ; -$tagManager = Tag\TagObjectManager::create() +$tagManager = Tag\TagManager::create() ->add(Tag\DatetimeTag::class) ->add(Tag\TimestampTag::class) ->add(Tag\UnsignedBigIntegerTag::class) diff --git a/phpstan.neon b/phpstan.neon index 54fe1b0..bf84d79 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -18,7 +18,7 @@ parameters: - message: '#Parameter .* of static method CBOR\\Utils\:\:binToInt\(\) expects string, string\|null given\.#' count: 1 - path: src/Tag/TagObjectManager.php + path: src/Tag/TagManager.php - message: '#Instanceof between CBOR\\CBORObject and CBOR\\CBORObject will always evaluate to true\.#' count: 1 diff --git a/src/Decoder.php b/src/Decoder.php index 4009c16..ad44c33 100644 --- a/src/Decoder.php +++ b/src/Decoder.php @@ -13,32 +13,29 @@ namespace CBOR; -use CBOR\OtherObject\BreakObject; -use CBOR\OtherObject\OtherObjectManager; -use CBOR\Tag\TagObjectManager; use InvalidArgumentException; use function ord; use RuntimeException; -final class Decoder +final class Decoder implements DecoderInterface { /** - * @var TagObjectManager + * @var Tag\TagManagerInterface */ private $tagObjectManager; /** - * @var OtherObjectManager + * @var OtherObject\OtherObjectManagerInterface */ private $otherTypeManager; - public function __construct(TagObjectManager $tagObjectManager, OtherObjectManager $otherTypeManager) + public function __construct(?Tag\TagManagerInterface $tagObjectManager = null, ?OtherObject\OtherObjectManagerInterface $otherTypeManager = null) { - $this->tagObjectManager = $tagObjectManager; - $this->otherTypeManager = $otherTypeManager; + $this->tagObjectManager = $tagObjectManager ?? $this->generateTagManager(); + $this->otherTypeManager = $otherTypeManager ?? $this->generateOtherObjectManager(); } - public static function create(TagObjectManager $tagObjectManager, OtherObjectManager $otherTypeManager): self + public static function create(?Tag\TagManagerInterface $tagObjectManager = null, ?OtherObject\OtherObjectManagerInterface $otherTypeManager = null): self { return new self($tagObjectManager, $otherTypeManager); } @@ -117,7 +114,7 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR switch ($mt) { case CBORObject::MAJOR_TYPE_BYTE_STRING: //2 $object = IndefiniteLengthByteStringObject::create(); - while (!($it = $this->process($stream, true)) instanceof BreakObject) { + while (!($it = $this->process($stream, true)) instanceof OtherObject\BreakObject) { if (!$it instanceof ByteStringObject) { throw new RuntimeException('Unable to parse the data. Infinite Byte String object can only get Byte String objects.'); } @@ -127,7 +124,7 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR return $object; case CBORObject::MAJOR_TYPE_TEXT_STRING: //3 $object = IndefiniteLengthTextStringObject::create(); - while (!($it = $this->process($stream, true)) instanceof BreakObject) { + while (!($it = $this->process($stream, true)) instanceof OtherObject\BreakObject) { if (!$it instanceof TextStringObject) { throw new RuntimeException('Unable to parse the data. Infinite Text String object can only get Text String objects.'); } @@ -137,14 +134,14 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR return $object; case CBORObject::MAJOR_TYPE_LIST: //4 $object = IndefiniteLengthListObject::create(); - while (!($it = $this->process($stream, true)) instanceof BreakObject) { + while (!($it = $this->process($stream, true)) instanceof OtherObject\BreakObject) { $object->add($it); } return $object; case CBORObject::MAJOR_TYPE_MAP: //5 $object = IndefiniteLengthMapObject::create(); - while (!($it = $this->process($stream, true)) instanceof BreakObject) { + while (!($it = $this->process($stream, true)) instanceof OtherObject\BreakObject) { $object->append($it, $this->process($stream, false)); } @@ -154,7 +151,7 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR throw new InvalidArgumentException('Cannot parse the data. No enclosing indefinite.'); } - return BreakObject::create(); + return OtherObject\BreakObject::create(); case CBORObject::MAJOR_TYPE_UNSIGNED_INTEGER: //0 case CBORObject::MAJOR_TYPE_NEGATIVE_INTEGER: //1 case CBORObject::MAJOR_TYPE_TAG: //6 @@ -162,4 +159,45 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR throw new InvalidArgumentException(sprintf('Cannot parse the data. Found infinite length for Major Type "%s" (%d).', str_pad(decbin($mt), 5, '0', STR_PAD_LEFT), $mt)); } } + + private function generateTagManager(): Tag\TagManagerInterface + { + return Tag\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) + ; + } + + private function generateOtherObjectManager(): OtherObject\OtherObjectManagerInterface + { + return OtherObject\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) + ; + } } diff --git a/src/DecoderInterface.php b/src/DecoderInterface.php new file mode 100644 index 0000000..3d7b192 --- /dev/null +++ b/src/DecoderInterface.php @@ -0,0 +1,19 @@ +data = $data; + $this->object = $object; + } + + public function __toString(): string + { + $result = parent::__toString(); + if (null !== $this->data) { + $result .= $this->data; + } + $result .= (string) $this->object; + + return $result; + } + + abstract public static function getTagId(): int; + + abstract public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): self; + + public function getValue(): CBORObject + { + return $this->object; + } +} diff --git a/src/Tag/Base16EncodingTag.php b/src/Tag/Base16EncodingTag.php index bfe50fd..94841a6 100644 --- a/src/Tag/Base16EncodingTag.php +++ b/src/Tag/Base16EncodingTag.php @@ -17,22 +17,22 @@ use CBOR\CBORObject; use CBOR\IndefiniteLengthByteStringObject; use CBOR\IndefiniteLengthTextStringObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use CBOR\TextStringObject; -final class Base16EncodingTag extends Base +final class Base16EncodingTag extends Tag { public static function getTagId(): int { return self::TAG_ENCODED_BASE16; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(self::TAG_ENCODED_BASE16, null, $object); } diff --git a/src/Tag/Base64EncodingTag.php b/src/Tag/Base64EncodingTag.php index 2bd2edc..f2f31ff 100644 --- a/src/Tag/Base64EncodingTag.php +++ b/src/Tag/Base64EncodingTag.php @@ -17,23 +17,23 @@ use CBOR\CBORObject; use CBOR\IndefiniteLengthByteStringObject; use CBOR\IndefiniteLengthTextStringObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use CBOR\TextStringObject; use InvalidArgumentException; -final class Base64EncodingTag extends Base +final class Base64EncodingTag extends Tag { public static function getTagId(): int { return self::TAG_ENCODED_BASE64; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(self::TAG_ENCODED_BASE64, null, $object); } diff --git a/src/Tag/Base64Tag.php b/src/Tag/Base64Tag.php index 138f226..5ad0b97 100644 --- a/src/Tag/Base64Tag.php +++ b/src/Tag/Base64Tag.php @@ -15,11 +15,11 @@ use CBOR\CBORObject; use CBOR\IndefiniteLengthTextStringObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use CBOR\TextStringObject; use InvalidArgumentException; -final class Base64Tag extends Base +final class Base64Tag extends Tag { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { @@ -35,12 +35,12 @@ public static function getTagId(): int return self::TAG_BASE64; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(self::TAG_BASE64, null, $object); } diff --git a/src/Tag/Base64UrlEncodingTag.php b/src/Tag/Base64UrlEncodingTag.php index 6cc3513..881706c 100644 --- a/src/Tag/Base64UrlEncodingTag.php +++ b/src/Tag/Base64UrlEncodingTag.php @@ -17,23 +17,23 @@ use CBOR\CBORObject; use CBOR\IndefiniteLengthByteStringObject; use CBOR\IndefiniteLengthTextStringObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use CBOR\TextStringObject; use CBOR\Utils; -final class Base64UrlEncodingTag extends Base +final class Base64UrlEncodingTag extends Tag { public static function getTagId(): int { return self::TAG_ENCODED_BASE64_URL; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(self::TAG_ENCODED_BASE64_URL, null, $object); } diff --git a/src/Tag/Base64UrlTag.php b/src/Tag/Base64UrlTag.php index aae0bd8..b3f6896 100644 --- a/src/Tag/Base64UrlTag.php +++ b/src/Tag/Base64UrlTag.php @@ -15,11 +15,11 @@ use CBOR\CBORObject; use CBOR\IndefiniteLengthTextStringObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use CBOR\TextStringObject; use InvalidArgumentException; -final class Base64UrlTag extends Base +final class Base64UrlTag extends Tag { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { @@ -35,12 +35,12 @@ public static function getTagId(): int return self::TAG_BASE64_URL; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(self::TAG_BASE64_URL, null, $object); } diff --git a/src/Tag/BigFloatTag.php b/src/Tag/BigFloatTag.php index 09a3462..96e1f5c 100644 --- a/src/Tag/BigFloatTag.php +++ b/src/Tag/BigFloatTag.php @@ -16,14 +16,14 @@ use CBOR\CBORObject; use CBOR\ListObject; use CBOR\NegativeIntegerObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use CBOR\UnsignedIntegerObject; use function count; use function extension_loaded; use InvalidArgumentException; use RuntimeException; -final class BigFloatTag extends Base +final class BigFloatTag extends Tag { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { @@ -51,17 +51,17 @@ public static function getTagId(): int return self::TAG_BIG_FLOAT; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(self::TAG_BIG_FLOAT, null, $object); } - public static function createFromExponentAndMantissa(CBORObject $e, CBORObject $m): Base + public static function createFromExponentAndMantissa(CBORObject $e, CBORObject $m): Tag { $object = ListObject::create() ->add($e) diff --git a/src/Tag/CBOREncodingTag.php b/src/Tag/CBOREncodingTag.php index 200daf5..8547b09 100644 --- a/src/Tag/CBOREncodingTag.php +++ b/src/Tag/CBOREncodingTag.php @@ -16,10 +16,10 @@ use CBOR\ByteStringObject; use CBOR\CBORObject; use CBOR\IndefiniteLengthByteStringObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use InvalidArgumentException; -final class CBOREncodingTag extends Base +final class CBOREncodingTag extends Tag { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { @@ -35,12 +35,12 @@ public static function getTagId(): int return self::TAG_ENCODED_CBOR; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(self::TAG_ENCODED_CBOR, null, $object); } diff --git a/src/Tag/CBORTag.php b/src/Tag/CBORTag.php index 0ced033..cf3d97f 100644 --- a/src/Tag/CBORTag.php +++ b/src/Tag/CBORTag.php @@ -14,21 +14,21 @@ namespace CBOR\Tag; use CBOR\CBORObject; -use CBOR\TagObject as Base; +use CBOR\Tag; -final class CBORTag extends Base +final class CBORTag extends Tag { public static function getTagId(): int { return self::TAG_CBOR; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(self::TAG_CBOR, null, $object); } diff --git a/src/Tag/DatetimeTag.php b/src/Tag/DatetimeTag.php index a5cabe8..9a36acf 100644 --- a/src/Tag/DatetimeTag.php +++ b/src/Tag/DatetimeTag.php @@ -15,7 +15,7 @@ use CBOR\CBORObject; use CBOR\IndefiniteLengthTextStringObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use CBOR\TextStringObject; use DateTimeImmutable; use InvalidArgumentException; @@ -23,7 +23,7 @@ /** * @final */ -class DatetimeTag extends Base +class DatetimeTag extends Tag { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { @@ -38,12 +38,12 @@ public static function getTagId(): int return self::TAG_STANDARD_DATETIME; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(self::TAG_STANDARD_DATETIME, null, $object); } diff --git a/src/Tag/DecimalFractionTag.php b/src/Tag/DecimalFractionTag.php index d004ec2..df3e699 100644 --- a/src/Tag/DecimalFractionTag.php +++ b/src/Tag/DecimalFractionTag.php @@ -16,14 +16,14 @@ use CBOR\CBORObject; use CBOR\ListObject; use CBOR\NegativeIntegerObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use CBOR\UnsignedIntegerObject; use function count; use function extension_loaded; use InvalidArgumentException; use RuntimeException; -final class DecimalFractionTag extends Base +final class DecimalFractionTag extends Tag { public function __construct(CBORObject $object) { @@ -50,12 +50,12 @@ public static function getTagId(): int return self::TAG_DECIMAL_FRACTION; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($object); } - public static function createFromExponentAndMantissa(CBORObject $e, CBORObject $m): Base + public static function createFromExponentAndMantissa(CBORObject $e, CBORObject $m): Tag { $object = ListObject::create() ->add($e) diff --git a/src/Tag/GenericTag.php b/src/Tag/GenericTag.php index 07043c9..e0a763d 100644 --- a/src/Tag/GenericTag.php +++ b/src/Tag/GenericTag.php @@ -14,16 +14,16 @@ namespace CBOR\Tag; use CBOR\CBORObject; -use CBOR\TagObject as Base; +use CBOR\Tag; -final class GenericTag extends Base +final class GenericTag extends Tag { public static function getTagId(): int { return -1; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } diff --git a/src/Tag/MimeTag.php b/src/Tag/MimeTag.php index a55068f..5640eff 100644 --- a/src/Tag/MimeTag.php +++ b/src/Tag/MimeTag.php @@ -15,11 +15,11 @@ use CBOR\CBORObject; use CBOR\IndefiniteLengthTextStringObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use CBOR\TextStringObject; use InvalidArgumentException; -final class MimeTag extends Base +final class MimeTag extends Tag { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { @@ -35,12 +35,12 @@ public static function getTagId(): int return self::TAG_MIME; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(self::TAG_MIME, null, $object); } diff --git a/src/Tag/NegativeBigIntegerTag.php b/src/Tag/NegativeBigIntegerTag.php index c3d49a5..6c96f89 100644 --- a/src/Tag/NegativeBigIntegerTag.php +++ b/src/Tag/NegativeBigIntegerTag.php @@ -17,10 +17,10 @@ use CBOR\ByteStringObject; use CBOR\CBORObject; use CBOR\IndefiniteLengthByteStringObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use InvalidArgumentException; -final class NegativeBigIntegerTag extends Base +final class NegativeBigIntegerTag extends Tag { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { @@ -36,12 +36,12 @@ public static function getTagId(): int return self::TAG_NEGATIVE_BIG_NUM; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(self::TAG_NEGATIVE_BIG_NUM, null, $object); } diff --git a/src/Tag/TagManager.php b/src/Tag/TagManager.php new file mode 100644 index 0000000..9dcbad9 --- /dev/null +++ b/src/Tag/TagManager.php @@ -0,0 +1,64 @@ +classes[$class::getTagId()] = $class; + + return $this; + } + + public function getClassForValue(int $value): string + { + return array_key_exists($value, $this->classes) ? $this->classes[$value] : GenericTag::class; + } + + public function createObjectForValue(int $additionalInformation, ?string $data, CBORObject $object): Tag + { + $value = $additionalInformation; + if ($additionalInformation >= 24) { + Utils::assertString($data, 'Invalid data'); + $value = Utils::binToInt($data); + } + /** @var Tag $class */ + $class = $this->getClassForValue($value); + + return $class::createFromLoadedData($additionalInformation, $data, $object); + } +} diff --git a/src/Tag/TagManagerInterface.php b/src/Tag/TagManagerInterface.php new file mode 100644 index 0000000..37e3862 --- /dev/null +++ b/src/Tag/TagManagerInterface.php @@ -0,0 +1,22 @@ +classes[$class::getTagId()] = $class; - - return $this; - } - - public function getClassForValue(int $value): string - { - return array_key_exists($value, $this->classes) ? $this->classes[$value] : GenericTag::class; - } - - public function createObjectForValue(int $additionalInformation, ?string $data, CBORObject $object): TagObject - { - $value = $additionalInformation; - if ($additionalInformation >= 24) { - Utils::assertString($data, 'Invalid data'); - $value = Utils::binToInt($data); - } - /** @var TagObject $class */ - $class = $this->getClassForValue($value); - - return $class::createFromLoadedData($additionalInformation, $data, $object); - } } diff --git a/src/Tag/TimestampTag.php b/src/Tag/TimestampTag.php index 195d6f7..7971c6e 100644 --- a/src/Tag/TimestampTag.php +++ b/src/Tag/TimestampTag.php @@ -18,12 +18,12 @@ use CBOR\OtherObject\DoublePrecisionFloatObject; use CBOR\OtherObject\HalfPrecisionFloatObject; use CBOR\OtherObject\SinglePrecisionFloatObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use CBOR\UnsignedIntegerObject; use DateTimeImmutable; use InvalidArgumentException; -final class TimestampTag extends Base +final class TimestampTag extends Tag { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { @@ -38,12 +38,12 @@ public static function getTagId(): int return self::TAG_EPOCH_DATETIME; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(1, null, $object); } diff --git a/src/Tag/UnsignedBigIntegerTag.php b/src/Tag/UnsignedBigIntegerTag.php index c91a843..2be051f 100644 --- a/src/Tag/UnsignedBigIntegerTag.php +++ b/src/Tag/UnsignedBigIntegerTag.php @@ -16,14 +16,14 @@ use CBOR\ByteStringObject; use CBOR\CBORObject; use CBOR\IndefiniteLengthByteStringObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use CBOR\Utils; use InvalidArgumentException; /** * @final */ -class UnsignedBigIntegerTag extends Base +class UnsignedBigIntegerTag extends Tag { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { @@ -39,12 +39,12 @@ public static function getTagId(): int return self::TAG_UNSIGNED_BIG_NUM; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(self::TAG_UNSIGNED_BIG_NUM, null, $object); } diff --git a/src/Tag/UriTag.php b/src/Tag/UriTag.php index a797b63..4d30270 100644 --- a/src/Tag/UriTag.php +++ b/src/Tag/UriTag.php @@ -15,11 +15,11 @@ use CBOR\CBORObject; use CBOR\IndefiniteLengthTextStringObject; -use CBOR\TagObject as Base; +use CBOR\Tag; use CBOR\TextStringObject; use InvalidArgumentException; -final class UriTag extends Base +final class UriTag extends Tag { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { @@ -35,12 +35,12 @@ public static function getTagId(): int return self::TAG_URI; } - public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base + public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag { return new self($additionalInformation, $data, $object); } - public static function create(CBORObject $object): Base + public static function create(CBORObject $object): Tag { return new self(self::TAG_URI, null, $object); } diff --git a/src/TagObject.php b/src/TagObject.php index 4504c8a..0ccb5c8 100644 --- a/src/TagObject.php +++ b/src/TagObject.php @@ -13,44 +13,9 @@ namespace CBOR; -abstract class TagObject extends AbstractCBORObject +/** + * @deprecated Will be removed in v3.0. Please use Tag instead + */ +abstract class TagObject extends Tag { - private const MAJOR_TYPE = self::MAJOR_TYPE_TAG; - - /** - * @var string|null - */ - protected $data; - - /** - * @var CBORObject - */ - protected $object; - - public function __construct(int $additionalInformation, ?string $data, CBORObject $object) - { - parent::__construct(self::MAJOR_TYPE, $additionalInformation); - $this->data = $data; - $this->object = $object; - } - - public function __toString(): string - { - $result = parent::__toString(); - if (null !== $this->data) { - $result .= $this->data; - } - $result .= (string) $this->object; - - return $result; - } - - abstract public static function getTagId(): int; - - abstract public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): self; - - public function getValue(): CBORObject - { - return $this->object; - } } diff --git a/tests/Type/BaseTestCase.php b/tests/Type/BaseTestCase.php index afe39ee..ea4b347 100644 --- a/tests/Type/BaseTestCase.php +++ b/tests/Type/BaseTestCase.php @@ -17,7 +17,7 @@ use CBOR\OtherObject; use CBOR\OtherObject\OtherObjectManager; use CBOR\Tag; -use CBOR\Tag\TagObjectManager; +use CBOR\Tag\TagManager; use PHPUnit\Framework\TestCase; /** @@ -44,7 +44,7 @@ protected function getDecoder(): Decoder ->add(OtherObject\SinglePrecisionFloatObject::class) ->add(OtherObject\DoublePrecisionFloatObject::class) ; - $tagObjectManager = TagObjectManager::create() + $tagObjectManager = TagManager::create() ->add(Tag\DatetimeTag::class) ->add(Tag\TimestampTag::class)