spomky-labs / cbor-php
PHP的CBOR编码/解码器
Requires
- php: >=8.0
- ext-mbstring: *
- brick/math: ^0.9|^0.10|^0.11|^0.12
Requires (Dev)
- ext-json: *
- ekino/phpstan-banned-code: ^1.0
- infection/infection: ^0.29
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.0
- phpstan/phpstan-beberlei-assert: ^1.0
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/phpunit: ^10.1|^11.0
- qossmic/deptrac: ^2.0
- rector/rector: ^1.0
- roave/security-advisories: dev-latest
- symfony/var-dumper: ^6.0|^7.0
- symplify/easy-coding-standard: ^12.0
Suggests
- ext-bcmath: GMP or BCMath extensions will drastically improve the library performance. BCMath extension needed to handle the Big Float and Decimal Fraction Tags
- ext-gmp: GMP or BCMath extensions will drastically improve the library performance
- 3.2.x-dev
- 3.1.x-dev
- 3.1.0
- 3.0.x-dev
- 3.0.4
- 3.0.3
- 3.0.2
- v3.0.1
- v3.0.0
- v2.1.x-dev
- v2.1.0
- v2.0.x-dev
- v2.0.1
- v2.0.0
- v1.1.x-dev
- v1.1.1
- v1.1.0
- v1.0.x-dev
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-dependabot/composer/ekino/phpstan-banned-code-tw-1.0or-tw-2.0
- dev-update
- dev-3.0.x-merge-up-into-3.1.x_Hy9w9FAb
This package is auto-updated.
Last update: 2024-09-02 01:34:36 UTC
README
范围
此库可以帮助您使用 Concise Binary Object Representation (CBOR - RFC8949) 解码和创建对象。
安装
使用Composer安装库:composer require spomky-labs/cbor-php
。
此项目严格遵守语义版本控制。
支持
我为您提供解决方案并回答您的问题。
如果您真的喜欢该项目,以及我所做的工作,或者如果您希望我优先处理您的问题,那么您可以通过几杯🍻或更多来帮助我!
或者
文档
对象创建
此库支持RFC8949中定义的所有主要类型,并具有支持任何类型标记(主要类型6)和其他对象(主要类型7)的能力。
每个对象至少
- 有一个静态方法
create
。此方法将正确实例化对象。 - 可以转换为二进制字符串:
$object->__toString();
或(string) $object
。
正整数(主要类型0)
<?php use CBOR\UnsignedIntegerObject; $object = UnsignedIntegerObject::create(10); $object = UnsignedIntegerObject::create(1000); $object = UnsignedIntegerObject::create(10000); $object = UnsignedIntegerObject::createFromHex('0AFFEBFF'); echo bin2hex((string)$object); // 1a0affebff
负整数(主要类型1)
<?php use CBOR\NegativeIntegerObject; $object = NegativeIntegerObject::create(-10); $object = NegativeIntegerObject::create(-1000); $object = NegativeIntegerObject::create(-10000);
字节字符串/不定长度字节字符串(主要类型2)
字节字符串和不定长度字节字符串对象具有相同的主要类型,但在此库中由两个不同的类处理。
<?php use CBOR\ByteStringObject; // Byte String use CBOR\IndefiniteLengthByteStringObject; // Indefinite Length Byte String // Create a Byte String with value "Hello" $object = ByteStringObject::create('Hello'); // Create an Indefinite Length Byte String with value "Hello" ("He" + "" + "ll" + "o") $object = IndefiniteLengthByteStringObject::create() ->append('He') ->append('') ->append('ll') ->append('o') ;
文本字符串/不定长度文本字符串(主要类型3)
文本字符串和不定长度文本字符串对象具有相同的主要类型,但在此库中由两个不同的类处理。
<?php use CBOR\TextStringObject; // Text String use CBOR\IndefiniteLengthTextStringObject; // Indefinite Length Text String // Create a Text String with value "(。◕‿◕。)⚡" $object = TextStringObject::create('(。◕‿◕。)⚡'); // Create an Indefinite Length Text String with value "(。◕‿◕。)⚡" ("(。◕" + "" + "‿◕" + "。)⚡") $object = IndefiniteLengthTextStringObject::create() ->append('(。◕') ->append('') ->append('‿◕') ->append('。)⚡') ;
列表/不定长度列表(主要类型4)
列表和不定长度列表对象具有相同的主要类型,但在此库中由两个不同的类处理。列表对象中的项可以是CBOR对象类型中的任何一种。
<?php use CBOR\ListObject; // List use CBOR\IndefiniteLengthListObject; // Infinite List use CBOR\TextStringObject; use CBOR\UnsignedIntegerObject; // Create a List with a single item $object = ListObject::create() ->add(TextStringObject::create('(。◕‿◕。)⚡')) ; // Create an Infinite List with several items $object = IndefiniteLengthListObject::create() ->add(TextStringObject::create('(。◕‿◕。)⚡')) ->add(UnsignedIntegerObject::create(25)) ;
映射/不定长度映射(主要类型5)
映射和不定长度映射对象具有相同的主要类型,但在此库中由两个不同的类处理。映射对象中的键和值可以是CBOR对象类型中的任何一种。
然而,请务必小心处理键。请遵循以下建议:
- 键不应重复
- 键应为正整数或负整数、(不定长度)字节字符串或(不定长度)文本字符串的类型。其他类型可能会引起错误。
<?php use CBOR\MapObject; // Map use CBOR\IndefiniteLengthMapObject; // Infinite Map use CBOR\ByteStringObject; use CBOR\TextStringObject; use CBOR\UnsignedIntegerObject; use CBOR\NegativeIntegerObject; // Create a Map with a single item $object = MapObject::create() ->add(UnsignedIntegerObject::create(25), TextStringObject::create('(。◕‿◕。)⚡')) ; // Create an Infinite Map with several items $object = IndefiniteLengthMapObject::create() ->append(ByteStringObject::create('A'), NegativeIntegerObject::create(-652)) ->append(UnsignedIntegerObject::create(25), TextStringObject::create('(。◕‿◕。)⚡')) ;
标记(主要类型6)
此库可以支持任何类型的标记。它包含了一些在规范中描述的标记
- 十六进制编码
- Base 64编码
- Base 64 URL安全编码
- 大浮点数
- 小数分数
- 纪元
- 时间戳
- 正大整数
- 负大整数
您可以通过扩展抽象类CBOR\TagObject
轻松创建自己的标记。此库提供了一个CBOR\Tag\GenericTag
类,可用于任何其他未知/不受支持的标记。
<?php use CBOR\Tag\TimestampTag; use CBOR\UnsignedIntegerObject; // Create an unsigned object that represents the current timestamp $object = UnsignedIntegerObject::create(time()); // e.g. 1525873787 //We tag the object with the Timestamp Tag $taggedObject = TimestampTag::create($object); // Returns a \DateTimeImmutable object with timestamp at 1525873787
其他对象(主要类型7)
此库可以支持任何类型的“其他对象”。它包含了一些在规范中描述的对象
- 假
- 真
- 空
- 未定义
- 半精度浮点数
- 单精度浮点数
- 双精度浮点数
- 简单值
您可以通过扩展抽象类 CBOR\OtherObject
来轻松创建自己的对象。此库提供了一个 CBOR\OtherObject\GenericTag
类,可以用于任何其他未知/不支持的对象。
由于PHP不支持'未定义'对象,规范化方法将返回 'undefined'
。
<?php use CBOR\OtherObject\FalseObject; use CBOR\OtherObject\NullObject; use CBOR\OtherObject\UndefinedObject; $object = FalseObject::create(); $object = NullObject::create(); $object = UndefinedObject::create();
示例
<?php use CBOR\MapObject; use CBOR\OtherObject\UndefinedObject; use CBOR\TextStringObject; use CBOR\ListObject; use CBOR\NegativeIntegerObject; use CBOR\UnsignedIntegerObject; use CBOR\OtherObject\TrueObject; use CBOR\OtherObject\FalseObject; use CBOR\OtherObject\NullObject; use CBOR\Tag\DecimalFractionTag; use CBOR\Tag\TimestampTag; $object = MapObject::create() ->add( TextStringObject::create('(。◕‿◕。)⚡'), ListObject::create([ TrueObject::create(), FalseObject::create(), UndefinedObject::create(), DecimalFractionTag::createFromExponentAndMantissa( NegativeIntegerObject::create(-2), UnsignedIntegerObject::create(1234) ), ]) ) ->add( UnsignedIntegerObject::create(2000), NullObject::create() ) ->add( TextStringObject::create('date'), TimestampTag::create(UnsignedIntegerObject::create(1577836800)) ) ;
编码结果将是 0xa37428efbda1e29795e280bfe29795efbda129e29aa183f5f4c482211904d21907d0f66464617465c11a5e0be100
。
对象加载
如果您想加载一个CBOR编码的字符串,只需实例化一个CBOR\Decoder
类。此类不需要任何参数。
<?php use CBOR\Decoder; $decoder = Decoder::create();
如果需要,您可以定义自定义的标签和其他对象支持管理器集。
<?php use CBOR\Decoder; use CBOR\OtherObject; use CBOR\Tag; $otherObjectManager = OtherObject\OtherObjectManager::create() ->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) ; $tagManager = 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) ; $decoder = Decoder::create($tagManager, $otherObjectManager);
然后,解码器将读取您想要加载的数据。这些数据必须由实现CBOR\Stream
接口的对象处理。此库提供了一个CBOR\StringStream
类来流字符串。
<?php use CBOR\StringStream; // CBOR object (in hex for the example) $data = hex2bin('fb3fd5555555555555'); // String Stream $stream = StringStream::create($data); // Load the data $object = $decoder->decode($stream); // Return a CBOR\OtherObject\DoublePrecisionFloatObject class with normalized value ~0.3333 (1/3)
贡献
对新功能的请求、错误修复以及所有其他使此项目有用的想法都欢迎。您可以提供的最佳贡献是通过修复需要帮助的已打开问题。
请将所有问题报告到主存储库。
请确保遵循这些最佳实践。
许可证
此项目在MIT许可证下发布。