- Can Symfony Object Mapper replace manual DTO mapping in Laravel controllers?
- Yes, it replaces manual mapping by using attributes like #[MapProperty] to define transformations between objects. For example, map a Laravel User model to a UserResponseDTO in a single line. This reduces repetitive code in controllers and improves maintainability, especially for APIs with many DTOs.
- What Laravel versions and PHP versions does Symfony Object Mapper support?
- The package requires PHP 8.1+ due to native attribute support. Laravel compatibility depends on Symfony’s DI integration; it works with Laravel 9+ (PHP 8.0+) but may need a service provider for older versions. Test thoroughly if using PHP 8.0, as some attribute features may require polyfills.
- How do I integrate Symfony Object Mapper into a Laravel project?
- Install via Composer (`composer require symfony/object-mapper`), then register it as a singleton in a Laravel service provider. Bind the `ObjectMapperInterface` to the Symfony implementation. For standalone use, instantiate it directly in controllers/services, though DI integration is recommended for testability.
- Does Symfony Object Mapper handle nested objects or collections?
- Yes, it supports nested objects and collections via the `CollectionTransformer`. For example, map a `Post` with nested `Comment` collections to a `PostResponseDTO` with `CommentResponseDTO` arrays. Configure this in attributes or via the mapper’s `setCollectionTransformer()` method.
- What are the performance implications of using Symfony Object Mapper in high-traffic APIs?
- Attribute reflection and runtime mapping introduce minor overhead compared to manual mapping or compiled solutions like Mapperly. Benchmark against alternatives (e.g., spatie/laravel-data) for your specific use case. For high-throughput APIs, consider caching mapped objects or using standalone transformers for critical paths.
- Can I use Symfony Object Mapper without adopting Symfony’s DependencyInjection?
- Yes, it can be used standalone in controllers or services via direct instantiation. However, DI integration (e.g., via a Laravel service provider) improves testability and reusability. If your project avoids Symfony DI, standalone usage is viable, though you’ll miss features like auto-wiring or condition-based mapping.
- How does Symfony Object Mapper compare to spatie/laravel-data for Laravel projects?
- Both simplify DTO mapping, but Symfony Object Mapper uses PHP attributes (PHP 8+) for declarative rules, while spatie/laravel-data relies on YAML/XML config. Choose Symfony Object Mapper if you prefer attributes and Symfony’s ecosystem; spatie/laravel-data may suit projects needing simpler config or PHP 7.x support.
- Are there alternatives to Symfony Object Mapper for Laravel that don’t require PHP 8?
- For PHP 7.x, consider `spatie/laravel-data` (YAML/XML config) or `Mapperly` (compile-time mapping). If you’re on PHP 8.0, polyfills like `nikic/php-parser` can enable attributes, but native support is recommended. Evaluate trade-offs between runtime flexibility (Symfony) and compile-time performance (Mapperly).
- How does Symfony Object Mapper handle circular references in mapped objects?
- Circular references (e.g., `User` ↔ `Post`) require custom handling. Use the `setCircularReferenceHandler()` method to define fallback logic, such as ignoring cycles or throwing exceptions. For complex cases, combine with manual transforms or disable mapping for problematic properties via #[Ignore].
- Will Symfony Object Mapper work with Laravel’s existing attribute system (e.g., #[AsRequest])?
- Yes, it integrates seamlessly with Laravel’s attributes. For example, use #[MapProperty] alongside #[AsRequest] in API resources. Both systems coexist without conflict, as they target different layers (mapping vs. request/response conversion). Test edge cases where attributes might overlap in the same class.