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
Output Normalizer Bundle

Output Normalizer Bundle Laravel Package

alexanevsky/output-normalizer-bundle

View on GitHub
Deep Wiki
Context7

Provides functions which allows to flexibly customize the output of objects and entities to an array or JSON by defining normalization rules in classes.

Frequently asked questions about Output Normalizer Bundle
How does this bundle differ from Symfony’s built-in Serializer for Laravel projects?
This bundle focuses on **decoupled output design** by defining explicit `OutputInterface` classes per entity, unlike Symfony’s Serializer which relies on groups or attributes. It’s ideal for projects needing fine-grained control (e.g., mobile vs. web payloads) without modifying domain entities. For Laravel, it integrates via Symfony’s DI container if using Symfony components.
Can I use this with Laravel’s Eloquent ORM instead of Doctrine?
No, this bundle is **Doctrine ORM-specific** due to its tight integration with Symfony’s ecosystem. For Eloquent, you’d need to manually implement the `ObjectNormalizerInterface` or explore alternatives like API Platform’s `ApiResource`. The bundle assumes Doctrine’s metadata system for property mapping.
What’s the performance impact of using custom `OutputInterface` classes for every entity?
The overhead is minimal for basic use cases, but defining classes for every entity can bloat your codebase. Benchmark with tools like Blackfire to compare against Symfony’s Serializer. The bundle’s strength lies in **reducing payload size** (e.g., `EntityToId` for IDs-only outputs), which often offsets the cost.
How do I handle nested objects or circular references in outputs?
The bundle supports nested normalization via modifiers and custom normalizers, but circular references require explicit handling (e.g., skipping properties or using `ignore` attributes). The documentation is sparse here, so test edge cases early. For complex scenarios, consider combining it with Symfony’s `NormalizerInterface` for fallback logic.
Is this bundle compatible with Laravel 9+ without Symfony components?
No, this bundle **requires Symfony’s DI container** (via `symfony/dependency-injection`) and is designed for Symfony 5.4+/6.0. For pure Laravel, you’d need to manually wire the `OutputNormalizer` or adapt it to Laravel’s service container, losing features like attribute parsing (e.g., `#[EntityToId]`).
How do I test modifiers or normalizers without mocking the entire bundle?
Test modifiers/normalizers in isolation by injecting mock `OutputInterface` classes and verifying their output. Use PHPUnit’s `DataProvider` to test edge cases (e.g., null values, nested objects). For integration tests, mock the `OutputNormalizer` and assert the final array structure matches expectations.
What’s the migration path if I’m already using Symfony’s Serializer groups?
Replace `#[Groups]` with `OutputInterface` classes for critical entities, starting with high-traffic endpoints. Use feature flags to deprecate old serialization logic. The bundle’s `EntityToId` modifier can replicate Serializer’s `id` group functionality, but test thoroughly for breaking changes in nested object handling.
Are there alternatives for Laravel that offer similar flexibility?
For Laravel, consider **API Platform’s `ApiResource`** (if using Symfony components) or **Laravel’s built-in `JsonResource`** for simpler cases. If you need DDD alignment, explore **Spatie’s Laravel Data** or **Encore’s API Resources**, which offer similar output control without Symfony dependencies.
How do I configure the bundle to work with API Platform in a Laravel-Symfony hybrid app?
Avoid duplicate normalization by configuring API Platform to **skip its default normalizers** for entities using this bundle. In `config/packages/api_platform.yaml`, set `serialization_context: []` for those entities and rely solely on the `OutputNormalizer`. Test API responses to ensure no data is omitted or duplicated.
What’s the best way to handle dynamic output requirements (e.g., runtime-generated schemas)?
This bundle isn’t ideal for dynamic schemas due to its static `OutputInterface` design. For such cases, use Symfony’s Serializer with **runtime groups** or **API Platform’s dynamic filters**. If you must use this bundle, predefine all possible output variants and switch between them via modifiers or service arguments.
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.
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament