spomky-labs / cbor-php
CBOR Encoder/Decoder for PHP
Requires
- php: >=8.0
- ext-mbstring: *
- brick/math: ^0.9|^0.10|^0.11|^0.12|^0.13|^0.14|^0.15|^0.16|^0.17|^0.18|^0.19|^0.20|^1.0
- symfony/polyfill-php81: ^1.32
Requires (Dev)
- ext-json: *
- roave/security-advisories: dev-latest
- symfony/error-handler: ^6.4|^7.1|^8.0
- symfony/var-dumper: ^6.4|^7.1|^8.0
Suggests
- ext-bcmath: Improves the library performance when ext-gmp is missing, and is required to handle the Big Float and Decimal Fraction Tags (4 and 5)
- ext-gmp: Strongly recommended when decoding untrusted input: without it, converting the byte string of a Big Number Tag (2 and 3) is quadratic in its length. Also improves the library performance overall
Provides
None
Conflicts
None
Replaces
None
- 4.0.x-dev
- 3.5.x-dev
- 3.4.x-dev
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.x-dev
- 3.3.5
- 3.3.4
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.x-dev
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.x-dev
- 3.1.1
- 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-3.4.x-merge-up-into-3.5.x_UT9Vjj5R
- dev-fix/175-non-scalar-map-keys
- dev-perf/string-heads-and-decoder
- dev-feat/registry-tags
- dev-perf/containers-and-stream
- dev-fix/issue-153-robustness
- dev-fix/154-ci-hardening
- dev-deprecate/self-describe-cbor-tag
- dev-fix/tag-number-encoding
- dev-fix/int-encoding-64bit
- dev-3.3.x-merge-up-into-4.0.x_p1iByYFr
- dev-fix/decoder-max-nesting-depth
- dev-ci/release-workflow-full-checkout
- dev-3.3.x-merge-up-into-4.0.x_AITsVWbc
- dev-fix/brick-math-open-constraint
- dev-fix/release-workflow-merge-up
- dev-3.3.x-merge-up-into-4.0.x_MZH8mTlP
- dev-3.3.x-merge-up-into-4.0.x_qSJHIz98
- dev-fix/ci-and-brick-math-0.19
- dev-copilot/prepare-pr-support-latest-brick-math
- dev-3.3.x-merge-up-into-4.0.x-v2
- dev-fix-ci-3.3.x
- dev-3.3.x-merge-up-into-4.0.x
- dev-3.2.x-merge-up-into-3.3.x
- dev-3.0.x-merge-up-into-3.1.x_Hy9w9FAb
This package is auto-updated.
Last update: 2026-09-15 06:58:51 UTC
README
A comprehensive PHP library for encoding and decoding CBOR (Concise Binary Object Representation) data according to RFC 8949.
Features
- ✅ Full support for all CBOR major types (0-7)
- ✅ Extensible tag system with built-in support for common tags
- ✅ Streaming decoder for efficient memory usage
- ✅ Type-safe API with modern PHP 8.0+ features
- ✅ Comprehensive support for indefinite-length objects
- ✅ Built-in normalization to PHP native types
- ✅ Diagnostic notation (RFC 8949 section 8), annotated with the CDDL of a schema
Installation
composer require spomky-labs/cbor-php
Requirements:
- PHP 8.0 or higher
- ext-mbstring
- brick/math
Optional but recommended:
- ext-gmp or ext-bcmath for improved performance with large integers
- ext-bcmath for Big Float and Decimal Fraction support
This project follows semantic versioning strictly.
Quick Start
<?php use CBOR\Decoder; use CBOR\MapObject; use CBOR\TextStringObject; use CBOR\UnsignedIntegerObject; // Encoding: Create a CBOR object $map = MapObject::create() ->add(TextStringObject::create('name'), TextStringObject::create('John Doe')) ->add(TextStringObject::create('age'), UnsignedIntegerObject::create(30)); $encoded = (string) $map; // Decoding: Parse CBOR data $decoder = Decoder::create(); $decoded = $decoder->decode(StringStream::create($encoded)); // Normalize to native PHP types $data = $decoded->normalize(); // ['name' => 'John Doe', 'age' => '30']
Documentation
📖 Complete Documentation - Full API reference and guides
Quick Links
- Tags Reference - Complete guide to the 70+ supported CBOR tags
- Creating Custom Tags - Implement your own tags for domain-specific needs
- Diagnostic Notation - Print an item as RFC 8949 writes it,
[_ 1, [2, 3]], annotated with the CDDL of your schema - API Reference - Encoding and decoding API
- Examples - WebAuthn, COSE, IoT, and more
Major Types Overview
This library supports all CBOR major types defined in RFC 8949:
| Major Type | Description | Classes |
|---|---|---|
| 0 | Unsigned Integer | UnsignedIntegerObject |
| 1 | Negative Integer | NegativeIntegerObject |
| 2 | Byte String | ByteStringObject, IndefiniteLengthByteStringObject |
| 3 | Text String | TextStringObject, IndefiniteLengthTextStringObject |
| 4 | Array | ListObject, IndefiniteLengthListObject |
| 5 | Map | MapObject, IndefiniteLengthMapObject |
| 6 | Tag | Tag and subclasses - See Tags Reference |
| 7 | Other | TrueObject, FalseObject, NullObject, etc. |
Common API:
- All objects have a static
create()method for instantiation - All objects can be converted to binary:
(string) $object - Many objects implement
Normalizableto convert to native PHP types
Basic Usage Examples
For complete documentation with all examples, see the Documentation.
Working with Basic Types
use CBOR\UnsignedIntegerObject; use CBOR\TextStringObject; use CBOR\ListObject; use CBOR\MapObject; // Integers $number = UnsignedIntegerObject::create(42); // Strings $text = TextStringObject::create('Hello World'); // Arrays $list = ListObject::create([ UnsignedIntegerObject::create(1), TextStringObject::create('two'), ]); // Maps/Objects $map = MapObject::create() ->add(TextStringObject::create('key'), TextStringObject::create('value'));
Working with Tags
📖 For complete tags documentation, see Tags Reference
use CBOR\Tag\TimestampTag; use CBOR\Tag\DecimalFractionTag; use CBOR\UnsignedIntegerObject; // Timestamps $timestamp = TimestampTag::create(UnsignedIntegerObject::create(time())); $dateTime = $timestamp->normalize(); // DateTimeImmutable // Decimal fractions $decimal = DecimalFractionTag::createFromFloat(3.14159); echo $decimal->normalize(); // "3.14159"
Supported Tags:
- Date/Time (Tags 0, 1) and dates without a time (Tags 100, 1004)
- Big Numbers (Tags 2, 3), Decimal/Binary Fractions (Tags 4, 5) and Rationals (Tag 30)
- Encoding hints (Tags 21, 22, 23), embedded CBOR (Tags 24, 63)
- URIs, MIME and UUIDs (Tags 32, 36, 37, 257)
- COSE structures, countersignatures and CBOR Web Tokens (Tags 16, 17, 18, 19, 61, 96, 97, 98)
- Typed and multi-dimensional arrays (Tags 40, 41, 64-87, 1040)
- IP and network addresses (Tags 52, 54, 260, 261)
- And more...
Create your own: See Creating Custom Tags guide.
Complete Example
use CBOR\Decoder; use CBOR\MapObject; use CBOR\ListObject; use CBOR\TextStringObject; use CBOR\UnsignedIntegerObject; use CBOR\StringStream; use CBOR\Tag\TimestampTag; // Build a complex nested structure $data = MapObject::create() ->add( TextStringObject::create('user'), MapObject::create() ->add(TextStringObject::create('name'), TextStringObject::create('Alice')) ->add(TextStringObject::create('age'), UnsignedIntegerObject::create(30)) ) ->add( TextStringObject::create('scores'), ListObject::create([ UnsignedIntegerObject::create(95), UnsignedIntegerObject::create(87), ]) ) ->add( TextStringObject::create('timestamp'), TimestampTag::create(UnsignedIntegerObject::create(time())) ); // Encode to binary $encoded = (string) $data; // Decode back $decoder = Decoder::create(); $decoded = $decoder->decode(StringStream::create($encoded)); // Convert to native PHP types $phpData = $decoded->normalize();
For more examples including WebAuthn, COSE, IoT scenarios, and advanced usage, see the complete documentation.
Contributing
Contributions are welcome! Here's how you can help:
- Report bugs and request features via GitHub Issues
- Fix issues labeled "help wanted"
- Improve documentation
- Submit pull requests
Please read our contribution guidelines before submitting.
Support
If you find this project valuable, consider supporting its development:
License
This project is released under the MIT License.
Maintained by: Florent Morselli and contributors