Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Cbor Php Laravel Package

spomky-labs/cbor-php

Comprehensive PHP 8+ CBOR (RFC 8949) encoder/decoder with full major type support, extensible tags, streaming decoding, indefinite-length handling, and normalization to native PHP types; includes common tags and custom tag support.

View on GitHub
Deep Wiki
Context7

CBOR for PHP

CI Latest Stable Version Total Downloads License

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

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

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 Normalizable to 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)
  • Big Numbers (Tags 2, 3)
  • Decimal/Binary Fractions (Tags 4, 5)
  • Encoding hints (Tags 21, 22, 23)
  • URIs and MIME (Tags 32, 36)
  • 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:

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

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport