spomky-labs/cbor-php
RFC 8949 CBOR encoder/decoder for PHP 8+. Supports all major types, tags, indefinite-length items, and streaming decode for low memory. Type-safe API with normalization to native PHP types; extensible custom tags.
Illuminate\Queue) for processing large CBOR payloads without memory overload..normalize() method bridge CBOR objects to native PHP types (arrays/scalars), reducing friction when integrating with Laravel’s Eloquent models or API responses. Example:
$cborData = $decoder->decode($request->getContent())->normalize();
$model->fill($cborData)->save();
$app->bind(CustomTagInterface::class, CustomHealthcareTag::class);
Redis::set('cbor_key', (string) $cborObject);
ext-mbstring, and brick/math (optional for large integers). Laravel’s default PHP stack (8.1+) already includes these, reducing setup friction.brick/math is a lightweight dependency with no known conflicts with Laravel’s core or popular packages (e.g., symfony/http-foundation).public function test_cbor_webauthn_assertion()
{
$cborAuthenticatorData = AuthenticatorDataTag::create(...);
$this->assertEquals(
$expectedBinary,
(string) $cborAuthenticatorData
);
}
| Risk Area | Mitigation Strategy |
|---|---|
| Performance Overhead | Benchmark against JSON (json_encode/json_decode) in Laravel’s AppServiceProvider boot method. CBOR should outperform JSON for structured data >500B. |
| Streaming Decoder | Test with Laravel’s queue workers (e.g., Illuminate\Queue\Worker) to validate memory efficiency for large payloads (e.g., 10MB+ CBOR blobs). |
| Tag System Complexity | Document custom tag registration in Laravel’s service provider and provide a base tag trait for consistency. Example: |
class HealthcareTag extends Tag implements Normalizable
{
use CBOR\Tag\NormalizableTrait;
}
| Backward Compatibility | Monitor brick/math updates (e.g., v0.15+) via Laravel’s composer.json conflict rules. |
| Error Handling | Wrap decoder calls in Laravel’s try-catch blocks and log errors via Log::error() for debugging. Example:
try {
$decoded = $decoder->decode($request->getContent());
} catch (CBOR\Exception\DecodeException $e) {
Log::error("CBOR decode failed: {$e->getMessage()}");
abort(400);
}
Payload Size vs. Complexity Tradeoff:
normalize() + json_encode() vs. direct JSON serialization.Streaming Requirements:
Custom Tag Needs:
$app->bind(CustomTagInterface::class, function ($app) {
return new CustomTag($app['config']['cbor.tags']);
});
WebAuthn/COSE Integration:
webauthn/webauthn package or paragonie/cose.Cache Storage:
$cached = Cache::get('cbor_key', fn() => (string) $cborObject);
$decoded = $decoder->decode($cached);
MapObject::create([...])).Illuminate\Http\Request/Response for CBOR-encoded API payloads.bytea) to reduce storage costs.ext-mbstring (enabled by default in Laravel’s PHP-FPM).ext-gmp/ext-bcmath for large integer/decimal performance (enable via php.ini or Laravel Forge).| Phase | Action | Laravel-Specific Example |
|---|---|---|
| Evaluation | Benchmark CBOR vs. JSON for your payloads using Laravel’s Benchmark facade or spatie/laravel-benchmark. |
```php |
| use Spatie\Benchmark\Benchmark; | ||
| Benchmark::run('CBOR vs JSON', function () { |
$cborTime = Benchmark::run('CBOR', fn() => $decoder->decode($cborData));
$jsonTime = Benchmark::run('JSON', fn() => json_decode($jsonData, true));
});
| **Pilot Integration** | Start with a **single endpoint** (e.g., `/api/telemetry`) to accept/reject CBOR payloads. Use middleware to validate CBOR content type: | ```php
// app/Http/Middleware/ValidateCborContentType.php
public function handle(Request $request, Closure $next) {
if ($request->isCbor()) { // Custom helper
$decoder = Decoder::create();
$request->merge(['cbor' => $decoder->decode($request->getContent())]);
}
return $next($request);
}
``` |
| **Core Integration** | Extend Laravel’s **request/response cycle** to support CBOR:
- Add `isCbor()` helper to `Illuminate\Support\Facades\Request`.
- Create a `CborResponse` macro for `Illuminate\Support\Facades\Response`.
- Register CBOR tags in a **service provider**. | ```php
// app/Providers/CborServiceProvider.php
public function register() {
$this->app->bind(CustomTagInterface::class, CustomTag::class);
}
``` |
| **Full Adoption** | Replace JSON serialization for:
- **Queue payloads** (e.g., `dispatch(new Job($cborPayload))`).
- **Cache storage** (e.g., `Cache::put('key', (string) $cborObject)`).
- **Database binary fields** (e.g., `->binary()` in Eloquent). | ```php
// Store CBOR in database
$model->cbor_data = (string) $cborObject;
$model->save();
``` |
### **Compatibility**
- **Laravel 10+**: Full compatibility due to PHP 8.1+ support.
- **Laravel 9.x**: Works but may require **shims** for PHP 8.0 features (e.g., union types). Test with
How can I help you explore Laravel packages today?