|
| 1 | +# Jane AutoMapper |
| 2 | + |
| 3 | +**Expiremental Library** |
| 4 | + |
| 5 | +Generate automapper class which allows to automap values from Class to Class. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | + * Optimized mapper from named class to named class (equal or very close to native perf) |
| 10 | + * Use symfony/property-info for mapping (allow extracting properties from reflection / phpdoc / serializer / doctrine / ...) |
| 11 | + |
| 12 | +## Planned |
| 13 | + |
| 14 | + * Custom property transformer |
| 15 | + * Array/Stdobject Mapper (a.k.a. Normalizers) |
| 16 | + * Type casting |
| 17 | + * MaxDepth / Circular Reference / Groups |
| 18 | + * Symfony/Serializer Bridge |
| 19 | + * Auto reverse mapping |
| 20 | + |
| 21 | +## Example |
| 22 | + |
| 23 | +```php |
| 24 | +class User |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var int |
| 28 | + */ |
| 29 | + private $id; |
| 30 | + /** |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + public $name; |
| 34 | + /** |
| 35 | + * @var string|int |
| 36 | + */ |
| 37 | + public $age; |
| 38 | + |
| 39 | + public function __construct($id, $name, $age) |
| 40 | + { |
| 41 | + $this->id = $id; |
| 42 | + $this->name = $name; |
| 43 | + $this->age = $age; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @return int |
| 48 | + */ |
| 49 | + public function getId() |
| 50 | + { |
| 51 | + return $this->id; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +class UserDTO |
| 56 | +{ |
| 57 | + /** |
| 58 | + * @var int |
| 59 | + */ |
| 60 | + public $id; |
| 61 | + /** |
| 62 | + * @var string |
| 63 | + */ |
| 64 | + public $name; |
| 65 | + /** |
| 66 | + * @var string|int |
| 67 | + */ |
| 68 | + public $age; |
| 69 | + /** |
| 70 | + * @var string |
| 71 | + */ |
| 72 | + public $email; |
| 73 | + |
| 74 | + /** |
| 75 | + * @var AddressDTO |
| 76 | + */ |
| 77 | + public $address; |
| 78 | + |
| 79 | + /** |
| 80 | + * @var AddressDTO[] |
| 81 | + */ |
| 82 | + public $addresses = []; |
| 83 | +} |
| 84 | + |
| 85 | +$compiler = new Compiler(new PropertyInfoExtractor( |
| 86 | + [new ReflectionExtractor()], |
| 87 | + [new ReflectionExtractor(), new PhpDocExtractor()], |
| 88 | + [new ReflectionExtractor()], |
| 89 | + [new ReflectionExtractor()] |
| 90 | +), new Accessor(), new TransformerFactory()); |
| 91 | + |
| 92 | +$configurationUser = new MapperConfiguration($this->compiler, User::class, UserDTO::class); |
| 93 | +$automapper = new AutoMapper(); |
| 94 | +$automapper->register($configurationUser); |
| 95 | + |
| 96 | +$user = new User(1, 'yolo', '13'); |
| 97 | + |
| 98 | +/** @var UserDTO $userDto */ |
| 99 | +$userDto = $automapper->map($user, UserDTO::class); |
| 100 | + |
| 101 | +``` |
0 commit comments