- How do I use this package in a Laravel API to replace JSON with CBOR for smaller payloads?
- Install the package via Composer, then use the `Encoder` and `Decoder` classes to serialize/deserialize data. For APIs, add middleware to handle `Accept: application/cbor` headers and encode responses with `Response::make($cborData, 200, ['Content-Type' => 'application/cbor'])`.
- Does this package support Laravel’s Eloquent for storing CBOR in database columns?
- Yes. Store CBOR as binary blobs (e.g., `mediumBlob` in MySQL) and use accessors/mutators to decode/encode. Example: Add a `cbor_preferences` column, then decode it in a model getter using `Decoder::create()->decode($this->cbor_preferences)`.
- What Laravel versions and PHP versions are officially supported?
- This package requires **PHP 8.0+** and is framework-agnostic, but works seamlessly with Laravel 8+. No Laravel-specific dependencies exist; it integrates via native PHP types (arrays/objects). Tested with Laravel’s latest LTS releases.
- How does the streaming decoder help with large CBOR payloads in Laravel queues?
- The streaming decoder processes CBOR incrementally, avoiding memory overload for large payloads (e.g., IoT telemetry). In Laravel queues, decode payloads with `Decoder::create()->decode(new StringStream($payload))` instead of loading entire blobs into memory.
- Can I customize CBOR tags for domain-specific semantics (e.g., COSE or CWT) in Laravel?
- Absolutely. The package supports custom tags via `TagRegistry`. Register your tags in a service provider (e.g., `TagRegistry::getInstance()->register(new MyCustomTag())`) and use them in encoding/decoding logic for protocols like COSE or CWT.
- Will this package conflict with existing JSON serialization in Laravel (e.g., `json_encode`)?
- No. Use it alongside Laravel’s JSON tools. For hybrid APIs, check `Accept` headers: return JSON for `application/json` and CBOR for `application/cbor`. The package normalizes CBOR to PHP arrays/objects, compatible with Laravel’s serialization.
- Do I need `ext-gmp` or `ext-bcmath` for basic CBOR usage in Laravel?
- No, they’re optional but improve performance for large integers/decimals. The package uses `brick/math` as a fallback. For most Laravel use cases (e.g., IoT data, WebAuthn), the default setup suffices unless dealing with extreme numeric precision.
- How do I validate CBOR payloads in Laravel requests (e.g., incoming WebAuthn tokens)?
- Use the `Decoder` to parse payloads and validate structure with Laravel’s validation rules. Example: `Validator::make(['cbor' => $request->cbor], ['cbor' => 'required|array'])` after decoding. For strict schemas, combine with `spomky-labs/assert` for custom assertions.
- Are there performance benchmarks comparing CBOR vs. JSON in Laravel?
- CBOR is typically **30–50% smaller** than JSON for similar data. Benchmark with your payloads: encode/decode 10K iterations and compare memory/CPU usage. Use `microtime(true)` for timing. For APIs, CBOR reduces bandwidth and improves latency for binary-heavy data.
- What are the alternatives to `spomky-labs/cbor-php` for Laravel, and why choose this one?
- Alternatives include `ruflin/elastica` (limited CBOR support) or JavaScript libraries via FFmpeg. This package is the **only PHP 8+ native, RFC 8949-compliant** solution with streaming, extensible tags, and Laravel-friendly normalization. It’s actively maintained and integrates cleanly with Laravel’s ecosystem.