|
1 | 1 | # rekalogika/mapper
|
2 | 2 |
|
3 |
| -An object mapper (also called automapper) for PHP and Symfony. It maps an object |
4 |
| -to another object. Primarily used to map an entity to a DTO, but also useful for |
5 |
| -other mapping purposes. |
| 3 | +`rekalogika/mapper` is an object mapper for PHP and Symfony, also commonly known |
| 4 | +as an automapper. It maps an object to another object. Primarily used to map an |
| 5 | +entity to a DTO, but also useful for other mapping purposes. It removes the |
| 6 | +complexity of mapping an object to another object, and even an object graph to |
| 7 | +another object graph. |
6 | 8 |
|
7 | 9 | Full documentation is available at [rekalogika.dev/mapper](https://rekalogika.dev/mapper/).
|
8 | 10 |
|
| 11 | +## Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +composer require rekalogika/mapper |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +```php |
| 20 | +use App\Entity\Book; |
| 21 | +use Rekalogika\Mapper\MapperInterface; |
| 22 | + |
| 23 | +/** @var MapperInterface $mapper */ |
| 24 | +/** @var Book $book */ |
| 25 | + |
| 26 | +$result = $mapper->map($book, BookDto::class); |
| 27 | + |
| 28 | +// or map to an existing object |
| 29 | + |
| 30 | +$bookDto = new BookDto(); |
| 31 | +$mapper->map($book, $bookDto); |
| 32 | +``` |
| 33 | + |
| 34 | +## Why Use a Mapper? |
| 35 | + |
| 36 | +Why do we need to use a mapper to save a few keystrokes, and not just use |
| 37 | +something simple like this? |
| 38 | + |
| 39 | +```php |
| 40 | +class BookDto |
| 41 | +{ |
| 42 | + public static function create(Book $book): self |
| 43 | + { |
| 44 | + $dto = new self(); |
| 45 | + // ... |
| 46 | + |
| 47 | + return $dto; |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Everyone must have that idea at some point. However, as the project grows, the |
| 53 | +target classes (DTOs) may start to reference each other, and become a rich |
| 54 | +object graph. Your code will start to have many special cases, and is no longer |
| 55 | +as simple as you thought it would be. It becomes harder to maintain, and then |
| 56 | +eventually forces you to sit back and try to resolve the problem. When (if?) you |
| 57 | +successfully engineer a solution, you will end up with something that resembles |
| 58 | +a mapping framework anyway. |
| 59 | + |
| 60 | +Mapping can be simple, but can also become a highly complex task. A mapper is |
| 61 | +created out of necessity to handle the complexity, not just as a means of saving |
| 62 | +a few keystrokes. |
| 63 | + |
9 | 64 | ## Features
|
10 | 65 |
|
11 | 66 | * Automatically lists the properties of the source and target, detects their
|
@@ -48,34 +103,7 @@ Full documentation is available at [rekalogika.dev/mapper](https://rekalogika.de
|
48 | 103 |
|
49 | 104 | * Option to read & write to private properties.
|
50 | 105 | * Migrate engine to `symfony/type-info`.
|
51 |
| - |
52 |
| -## Installation |
53 |
| - |
54 |
| -```bash |
55 |
| -composer require rekalogika/mapper |
56 |
| -``` |
57 |
| -## Usage |
58 |
| - |
59 |
| -In Symfony projects, simply autowire `MapperInterface`. In non Symfony projects, |
60 |
| -instantiate a `MapperFactory`, and use `getMapper()` to get an instance of |
61 |
| -`MapperInterface`. |
62 |
| - |
63 |
| -To map objects, you can use the `map()` method of `MapperInterface`. |
64 |
| - |
65 |
| -```php |
66 |
| -use Rekalogika\Mapper\MapperInterface; |
67 |
| - |
68 |
| -/** @var MapperInterface $mapper */ |
69 |
| - |
70 |
| -$book = new Book(); |
71 |
| -$result = $mapper->map($book, BookDto::class); |
72 |
| - |
73 |
| -// or map to an existing object |
74 |
| - |
75 |
| -$book = new Book(); |
76 |
| -$bookDto = new BookDto(); |
77 |
| -$mapper->map($book, $bookDto); |
78 |
| -``` |
| 106 | +* Auto-detect static factory method. |
79 | 107 |
|
80 | 108 | ## Documentation
|
81 | 109 |
|
|
0 commit comments