- How do I install and set up `boshurik/mapper` in a Laravel project?
- Install via Composer with `composer require boshurik/mapper`, then initialize the mapper by creating a `MappingRegistry` and defining custom mapping rules between your source and destination classes. For Laravel, manually bind the mapper to the service container if using dependency injection. No Laravel-specific setup is required beyond basic Composer installation.
- Does this package support Laravel Eloquent models for database hydration?
- The package doesn’t natively integrate with Eloquent, so you’ll need to manually handle relationships, accessors, or fillable fields in your custom mapping rules. For Eloquent hydration, consider extending the mapper or using Laravel’s built-in casting/accessors alongside this library.
- Can I use this for bidirectional mapping (e.g., DTO → Entity → DTO) without data loss?
- Yes, the package supports bidirectional mapping, but you must define explicit rules for both directions. Test thoroughly for edge cases like circular references or nested objects, as the package lacks built-in safeguards for these scenarios.
- What Laravel versions and PHP versions does `boshurik/mapper` support?
- The package officially supports PHP 7.4+, but it may not fully work with PHP 8.x features like named arguments or union types due to its 2021 release date. Laravel compatibility is implied but untested beyond basic usage—test in your environment.
- How does this compare to Laravel’s built-in API Resources or `spatie/laravel-data` for DTOs?
- `boshurik/mapper` is more lightweight and flexible for custom mapping logic, while API Resources focus on API responses and `spatie/laravel-data` provides immutable DTOs. Choose this package if you need fine-grained control over transformations beyond what Laravel’s ecosystem offers.
- Is there a way to auto-map Eloquent models to DTOs without writing custom rules?
- No, the package requires explicit mapping rules. You’d need to extend the mapper or use traits to auto-generate mappings for Eloquent models, but this isn’t built-in. For auto-mapping, consider alternatives like `spatie/array-to-object` or manual hydrators.
- Will this package work with Laravel’s service container for dependency injection?
- Yes, you can manually bind the mapper to Laravel’s container using `$this->app->bind(MapperInterface::class, fn($app) => new Mapper($registry))`. However, the package lacks native Laravel integration, so DI setup is manual.
- Are there performance concerns with deeply nested objects or large datasets?
- Performance impact is minimal for simple mappings, but recursive reflection (used internally) could introduce overhead for deeply nested or large objects. Benchmark in your use case, especially if mapping high-frequency data like API requests.
- How do I handle circular references or complex types (e.g., collections) in mappings?
- The package doesn’t handle circular references automatically, so you’ll need to implement custom logic in your mapping rules. For collections, define rules for each item type or use PHP’s `iterator_to_array` for flat structures.
- Is this package actively maintained? Should I use it in production?
- The package is abandoned (last release 2021) with no PHP 8.x or Laravel-specific updates. Use it cautiously in production, or fork it to backport fixes. For critical projects, consider alternatives like `symfony/serializer` or `league/glide` with active maintenance.