Welcome to the Symfony Structured Mapper Bundle! This bundle provides an efficient way to map between Data Transfer Objects (DTOs) and Entities in Symfony applications. Leveraging the Structured Mapper library, it allows for clear and concise attribute-based mapping.
In modern PHP applications, especially those built with Symfony, managing data flow between different layers can be complex. DTOs serve as a bridge between your application and its data storage. This bundle simplifies the process of transforming data between these layers, ensuring your application remains clean and maintainable.
- Attribute-based Mapping: Use PHP attributes to define mappings directly in your DTOs.
- Seamless Integration: Works effortlessly with Symfony and Doctrine.
- Easy Configuration: Minimal setup required to get started.
- Flexible: Supports custom value transformers for complex mappings.
- High Performance: Built with performance in mind, using the Structured Mapper library.
To install the Symfony Structured Mapper Bundle, you can use Composer. Run the following command in your terminal:
composer require sajidirnd/symfony-structured-mapper-bundle
After installation, ensure to enable the bundle in your config/bundles.php
file:
return [
// Other bundles...
Sajidirnd\SymfonyStructuredMapperBundle\SajidirndSymfonyStructuredMapperBundle::class => ['all' => true],
];
To start using the Symfony Structured Mapper Bundle, you need to create your DTOs and Entities. The bundle allows you to define mappings directly within your DTOs using attributes.
namespace App\DTO;
use Sajidirnd\SymfonyStructuredMapperBundle\Attribute\MapTo;
class UserDTO
{
#[MapTo('name')]
public string $fullName;
#[MapTo('email')]
public string $emailAddress;
#[MapTo('age')]
public int $age;
}
namespace App\Entity;
class User
{
private string $name;
private string $email;
private int $age;
// Getters and setters...
}
You can customize the behavior of the bundle through your Symfony configuration files. The default configuration should suffice for most use cases, but advanced options are available.
In your config/packages/symfony_structured_mapper.yaml
:
symfony_structured_mapper:
# Custom configuration options
value_transformers:
# Define your custom transformers here
Here are some common use cases for the Symfony Structured Mapper Bundle:
To map a DTO to an Entity, you can use the Mapper
service provided by the bundle:
use App\DTO\UserDTO;
use App\Entity\User;
use Sajidirnd\SymfonyStructuredMapperBundle\Service\Mapper;
class UserService
{
private Mapper $mapper;
public function __construct(Mapper $mapper)
{
$this->mapper = $mapper;
}
public function createUser(UserDTO $userDTO): User
{
$user = new User();
$this->mapper->map($userDTO, $user);
return $user;
}
}
Similarly, you can map an Entity back to a DTO:
public function getUserDTO(User $user): UserDTO
{
$userDTO = new UserDTO();
$this->mapper->map($user, $userDTO);
return $userDTO;
}
We welcome contributions to the Symfony Structured Mapper Bundle! If you have suggestions, bug fixes, or new features, please open an issue or submit a pull request.
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and commit them.
- Push your branch to your forked repository.
- Open a pull request.
This bundle is licensed under the MIT License. See the LICENSE file for more information.
For support, please check the Releases section for the latest updates and bug fixes. If you encounter issues, feel free to open an issue in the repository.
Thank you for using the Symfony Structured Mapper Bundle! We hope it makes your development process smoother and more efficient. Happy coding!