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

Cborencode Laravel Package

2tvenom/cborencode

Laravel/PHP CBOR encoder for compact binary data serialization. Encode arrays and values into RFC 7049/8949-style CBOR for storage, caching, and APIs. Lightweight package focused on producing valid CBOR payloads with simple usage.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer:

composer require 2tvenom/cborencode

Then, simply use the encoder in your code:

use CBOR\Encoder;

$payload = [
    'device_id' => 'sensor-001',
    'temperature' => 23.7,
    'active' => true,
    'timestamp' => time(),
];

$encoded = Encoder::encode($payload);
// $encoded is a binary string suitable for HTTP request body, MQTT message, etc.

Start with encoding simple associative arrays or objects — this mirrors real-world API request/response serialization needs.

Implementation Patterns

  • API Payloads: Replace JSON with CBOR in REST/GraphQL APIs for bandwidth savings (e.g., mobile/IoT clients). Use middleware to auto-convert request/response bodies.
  • Message Queues & Protocols: Encode payloads for Kafka, RabbitMQ, or MQTT where minimal size improves throughput.
  • Caching: Store frequently accessed structures in compact binary form (e.g., Doctrine/Redis cache adapter).
  • Binary Protocol Interop: Implement custom protocols with CBOR-encoded messages (e.g., CoAP, wire protocols for embedded systems).
  • Type Hinting & Validation: Wrap the encoder to enforce CBOR-compliant types (string, int, float, bool, null, array, Traversable) before encoding.

Gotchas and Tips

  • No Decoding Support: This package only encodes — you’ll need a separate decoder (e.g., cbor.phar, phpcbor, or ext-cbor) for decoding. Double-check interoperability if decoding in other languages.
  • Determinism ≠ Stability: While the encoding is deterministic per run, PHP array order changes (pre-7.4) or object property visibility may cause inconsistent output across runs — ensure consistent array/object structures.
  • Floating-Point Precision: Use float values with care; IEEE 754 double-precision may not match expectations in cross-platform scenarios — consider string encoding for high-precision numbers (e.g., financial data).
  • No Tag Support: Extension types (e.g., timestamps, big integers) aren’t handled — map them manually (e.g., [4, $timestamp] for epoch seconds) if required.
  • Namespace Collision: The package uses CBOR\ — avoid conflicting classes or use autoloader aliases if integrating with other libraries.
  • Testing Tip: Write unit tests that assert both round-trip validity and byte-level consistency (using hash_file('sha256', $encoded)).
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
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
twbs/bootstrap4