rekalogika / mapper
An object mapper for PHP and Symfony. Maps an object to another object. Primarily used for transforming an entity to a DTO and vice versa.
Fund package maintenance!
priyadi
Installs: 12 975
Dependents: 1
Suggesters: 0
Security: 0
Stars: 26
Watchers: 2
Forks: 1
Open Issues: 2
Type:symfony-bundle
Requires
- php: >=8.2
- doctrine/collections: ^2.1
- phpdocumentor/reflection-docblock: ^5.3
- phpstan/phpdoc-parser: ^1.25
- psr/container: ^2.0
- symfony/cache: ^6.4 || ^7.0
- symfony/clock: ^6.4 || ^7.0
- symfony/config: ^6.4 || ^7.0
- symfony/console: ^6.4 || ^7.0
- symfony/dependency-injection: ^6.4.11 || ^6.5 || ^7.1.4 || ^7.2
- symfony/filesystem: ^6.4 || ^7.0
- symfony/finder: ^6.4 || ^7.0
- symfony/property-access: ^6.4 || ^7.0
- symfony/property-info: 6.4.0 - 6.4.8 || ^6.4.10 || ^6.5 || 7.0.0 - 7.0.8 || 7.1.0 - 7.1.1 || ^7.1.3 || ^7.2
- symfony/serializer: ^6.4 || ^7.0
- symfony/service-contracts: ^3.0
- symfony/var-exporter: ^6.4.1 || ^6.5 || ^7.0.1 || ^7.1
Requires (Dev)
- bnf/phpstan-psr-container: ^1.0
- brick/money: ^0.10.0
- dave-liddament/php-language-extensions: ^0.6.0
- dave-liddament/phpstan-php-language-extensions: ^0.5.0
- doctrine/doctrine-bundle: ^2.10
- doctrine/orm: ^2.14 || ^3.0
- doctrine/persistence: ^3.1
- ekino/phpstan-banned-code: ^2.1
- phpstan/phpstan: 1.12.2 || ^1.12.4 || ^1.13
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^10.5
- psalm/plugin-phpunit: ^0.19.0
- psr/log: ^3.0
- ramsey/uuid: ^3.0 || ^4.0
- rector/rector: ^1.2
- symfony/debug-bundle: ^6.4 || ^7.0
- symfony/dotenv: ^6.4 || ^7.0
- symfony/framework-bundle: ^6.4 || ^7.0
- symfony/http-kernel: ^6.4 || ^7.0
- symfony/monolog-bundle: ^2.0 || ^3.0
- symfony/polyfill-php83: ^1.30
- symfony/runtime: ^6.4 || ^7.0
- symfony/stopwatch: ^6.4 || ^7.0
- symfony/uid: ^6.4 || ^7.0
- symfony/var-dumper: ^6.4 || ^7.0
- symfony/web-profiler-bundle: ^6.4 || ^7.0
- symfony/yaml: ^6.4 || ^7.0
- twig/twig: ^2.12|^3.0
- vimeo/psalm: ^5.26
Suggests
- phpstan/extension-installer: Configures PHPStan automatically (use --dev option to install)
- phpstan/phpstan: Required to automatically dump mapping metadata to a file (use --dev option to install)
- v1.13.5
- v1.13.4
- v1.13.3
- v1.13.2
- v1.13.1
- v1.13.0
- v1.12.0
- v1.11.0
- dev-main / 1.10.x-dev
- v1.10.0
- v1.9.4
- v1.9.3
- v1.9.2
- v1.9.1
- v1.9.0
- v1.8.0
- v1.7.0
- v1.6.1
- v1.6.0
- v1.5.7
- v1.5.6
- v1.5.5
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
- v0.10.2
- v0.10.1
- v0.10.0
- v0.9.1
- v0.9.0
- v0.8.1
- v0.8.0
- v0.7.3
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.7
- v0.6.6
- v0.6.5
- v0.6.4
- v0.6.3
- v0.6.2
- v0.6.1
- 0.6.0
- v0.5.26
- v0.5.25
- v0.5.23
- v0.5.22
- v0.5.21
- v0.5.20
- v0.5.19
- v0.5.18
- v0.5.17
- v0.5.16
- v0.5.15
- v0.5.14
- v0.5.13
- v0.5.12
- v0.5.11
- v0.5.10
- v0.5.9
- v0.5.8
- v0.5.7
- v0.5.6
- v0.5.5
- v0.5.4
- v0.5.3
- v0.5.2
- v0.5.1
- dev-chore/rector-run
- dev-fix/mapperfactory-infloop
- dev-issue/250
- dev-dependabot/composer/phpstan/phpdoc-parser-tw-2.0
- dev-chore/rector4
- dev-chore/cleanup
- dev-fix/dynamic-property-2
- dev-deps/property-info
- dev-fix/issue92
- dev-chore/cs-fix
- dev-fix/key-transformation
- dev-fix/bundle-deprecation
- dev-refactor/static-autoloader
- dev-refactor/proxy
- dev-proxy-factory
- dev-mapping-engine-experiment
- dev-readwriteinfo
- dev-reverse-inheritance
This package is auto-updated.
Last update: 2024-11-13 09:39:46 UTC
README
rekalogika/mapper
is an object mapper for PHP and Symfony, also commonly known
as an automapper. It maps an object to another object. Primarily used to map an
entity to a DTO, but also useful for other mapping purposes. It removes the
complexity of mapping an object to another object, and even an object graph to
another object graph.
Full documentation is available at rekalogika.dev/mapper.
Installation
composer require rekalogika/mapper
Usage
use App\Entity\Book; use Rekalogika\Mapper\MapperInterface; // map a single object: /** @var MapperInterface $mapper */ /** @var Book $book */ $result = $mapper->map($book, BookDto::class); // map a single object to an existing object: $bookDto = new BookDto(); $mapper->map($book, $bookDto); // map an iterable of objects: /** @var IterableMapperInterface $iterableMapper */ /** @var iterable<Book> $books */ $bookDtos = $iterableMapper->mapIterable($books, BookDto::class);
Why Use a Mapper?
Why do we need to use a mapper to save a few keystrokes, and not just use something simple like this?
class BookDto { public static function create(Book $book): self { $dto = new self(); // ... return $dto; } }
Everyone must have that idea at some point. However, as the project grows, the target classes (DTOs) may start to reference each other, and become a rich object graph. Your code will start to have many special cases, and is no longer as simple as you thought it would be. It becomes harder to maintain, and then eventually forces you to sit back and try to resolve the problem. When (if?) you successfully engineer a solution, you will end up with something that resembles a mapping framework anyway.
Mapping can be simple, but can also become a highly complex task. A mapper is created out of necessity to handle the complexity, not just as a means of saving a few keystrokes.
Features
General
- Automatically lists the properties of the source and target, detects their types, and maps them accordingly.
- Reads the type from PHP type declaration and PHPDoc annotations, including the type of the nested objects.
- Does not attempt to circumvent your class constraints. Reads only from and writes only to public properties, getters, setters. Does not instantiate objects without their constructor.
- Constructor initialization.
- Handles nested objects.
- Handles recursion and circular references.
- Inheritance support. Maps to abstract classes and interfaces using an inheritance map attribute.
- Maps to and from
stdClass
, objects extendingstdClass
, and other objects with dynamic properties (#[AllowDynamicProperties]
). - Maps an object to an array, and vice versa.
- Support for third-party objects: Doctrine Collections, Symfony Uid, Ramsey UUID.
Custom Mapping
- Override the mapping of a specific property using a custom property mapper.
- Override the mapping between two specific classes using a custom object mapper.
- Extend the mapper by creating new transformers, or decorating the existing ones.
- Match classes using attributes in your transformers, in addition to using class names.
- Preset mapping. Provide a table of predetermined mappings that the mapper can use.
Object Lazy-Loading
- If possible, target objects are lazy-loaded. The mapping does not take place until the target is accessed, and will never take place if it is never accessed.
- Attempts to detect identifier properties on the source side. Those properties will be mapped eagerly to the target side, as they should not trigger the hydration of the source. As an example, API Platform will be able to generate IRIs without causing Doctrine to hydrate the entire object graph.
Arrays and Array-Like Objects
- Handles the mapping between
array
or array-like objects. - Handles adder and remover methods on the target side.
- Handles non-string & non-integer keys in array-like objects, including
SplObjectStorage
. - Option to remove existing items from the target if they are not present in the source.
Array-Like Lazy-Loading
- Lazy loading if the target is type-hinted with
ArrayAccess
,Traversable
orCollectionInterface
. The target will not iterate the source object until it is accessed, or never if it is never accessed. - Stream mapping. Maps the source members to the target side as they are being iterated. This may consume less memory.
- With lazy loading, if the source is a
Countable
, then the target will also be aCountable
. With an extra-lazy Doctrine Collection, the consumer will be able to count the target without causing a full hydration of the source.
Development Experience (DX)
- Helpful exception messages.
- Console commands for debugging.
- Data collector and profiler integration.
- Coded from the start using PHP 8, strict types, and maxed-out PHPStan and Psalm level.
To-Do List
- Option to read & write to private properties.
- Migrate engine to
symfony/type-info
. - Auto-detect static factory method.
- Use our own interface for proxy objects.
- Improve non-framework usage.
- Warm up proxies on build time from the list of classes provided by the user.
Documentation
License
MIT