shureban/laravel-object-mapper
Install via Composer:
composer require shureban/laravel-object-mapper
Publish the config (optional):
php artisan vendor:publish --provider="Shureban\ObjectMapper\ObjectMapperServiceProvider"
First Use Case: Transforming arrays to objects with automatic snake_case-to-camelCase conversion:
use Shureban\ObjectMapper\Facades\ObjectMapper;
$data = ['user_name' => 'john_doe', 'user_age' => 30];
$user = ObjectMapper::map($data, \App\Models\User::class);
// $user now has properties: userName, userAge
$object = ObjectMapper::map($arrayData, $targetClass);
$complexObject = ObjectMapper::map($nestedArray, \App\Models\Order::class);
// Automatically handles nested snake_case keys
// config/object-mapper.php
'mappers' => [
'App\Models\User' => \App\Mappers\UserMapper::class,
],
FormRequest to auto-map validated input:
public function rules() { /* ... */ }
public function map(array $data) {
return ObjectMapper::map($data, \App\Models\User::class);
}
public function toArray($request) {
return ObjectMapper::mapCollection($this->collection, \App\DTO\UserDTO::class);
}
user_name → userName).
protected $fillable = ['user_name'], ensure your setter methods match the auto-converted names (e.g., setUserName()).class or static may cause issues. Rename them in your source data.true/false). Override with explicit type hints in your model/DTO.ObjectMapper::setDebug(true); // Logs transformations to storage/logs/object-mapper.log
$mappedData = ObjectMapper::getMappedData(); // Returns the last transformation result
\Shureban\ObjectMapper\Transformers\TransformerInterface for complex logic.mapped and mapping events in your EventServiceProvider:
protected $listen = [
'Shureban\ObjectMapper\Events\Mapping' => [
\App\Listeners\LogMapping::class,
],
];
How can I help you explore Laravel packages today?