backbrain / php-automapper
PHP port of the popular automapper.org library
Installs: 2 546
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 2
Requires
- php: >=8.2
- jawira/case-converter: ^3.5
- nikic/php-parser: ^4.0|^5.0
- phpdocumentor/reflection-docblock: ^5.3
- phpdocumentor/type-resolver: ^1.8
- phpstan/phpdoc-parser: ^1.26
- psr/cache: ^3.0
- psr/log: ^3.0
- symfony/clock: ^6.0|^7.0
- symfony/expression-language: ^6.0|^7.0
- symfony/finder: ^6.0|^7.0
- symfony/property-access: ^6.0|^7.0
- symfony/property-info: ^6.0|^7.0
- symfony/uid: ^6.0|^7.0
Requires (Dev)
- doctrine/collections: ^2.2
- friendsofphp/php-cs-fixer: ^3.51
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^11.0
- symfony/dependency-injection: ^6.0|^7.0
- symfony/filesystem: ^6.0|^7.0
- symfony/framework-bundle: ^6.0|^7.0
- symfony/http-kernel: ^6.0|^7.0
- symfony/var-dumper: *
- dev-main
- v0.4.10
- v0.4.9
- v0.4.8
- v0.4.7
- v0.4.6
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.1
- v0.3.0
- v0.2.6
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.0
- v0.0.14
- v0.0.13
- v0.0.12
- v0.0.11
- v0.0.10
- v0.0.9
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- dev-dependabot/npm_and_yarn/docs/site/mdx-js/react-3.1.0
- dev-dependabot/npm_and_yarn/docs/site/http-proxy-middleware-2.0.7
This package is auto-updated.
Last update: 2024-11-01 20:06:54 UTC
README
PHP AutoMapper is a library designed to simplify the mapping of data between objects, inspired by the popular .NET library AutoMapper. It aims to reduce boilerplate code necessary for transferring data from one object structure to another, making your PHP application cleaner and maintenance easier.
Note: This project is still in alpha development and may not yet support all features of the original AutoMapper library. Interfaces and methods may change even in minor releases until a stable version v1.x is reached.
Installation
Use Composer to install PHP AutoMapper into your project:
composer require backbrain/php-automapper
Features
PHP AutoMapper strives to implement the core functionalities of the original .NET AutoMapper library. Here's a list of supported features:
- Convention-based mapping
- Custom value resolvers
- Nested object mapping
- Conditional property mapping
- Custom value converters
- Before and after mapping actions
- Support for AutoMapper profiles
- Reverse mapping
- Inline mapping configuration
- Mapping to existing objects
Please note that due to differences between C# and PHP, not all features from the original AutoMapper library are applicable or have been implemented at this stage.
Documentation
For a detailed documentation, please refer to the PHP AutoMapper Documentation site.
For general usage patterns and understanding AutoMapper concepts, please refer to the original AutoMapper documentation:
The concepts and configurations explained in the original documentation serve as a basis for understanding how to use PHP AutoMapper effectively. Where PHP AutoMapper diverges or extends the original library's functionality, specific documentation and examples will be provided within this project's wiki or documentation directory.
Usage Example
Here's a simple example of how to use PHP AutoMapper to map data between two objects. For more examples and detailed usage instructions, please refer to the examples directory.
<?php // php docs/example/01_basic.php use Backbrain\Automapper\Contract\Builder\Options; use Backbrain\Automapper\Contract\Builder\Config; use Backbrain\Automapper\MapperConfiguration; require_once __DIR__ . '/../../vendor/autoload.php'; class AccountDTO { public string $givenName; public string $familyName; } class ProfileDTO { private string $givenName; private string $familyName; private string $fullName; public function setGivenName(string $givenName): ProfileDTO { $this->givenName = $givenName; return $this; } public function setFamilyName(string $familyName): ProfileDTO { $this->familyName = $familyName; return $this; } public function setFullName(string $fullName): ProfileDTO { $this->fullName = $fullName; return $this; } } $config = new MapperConfiguration(fn (Config $config) => $config ->createMap(AccountDTO::class, ProfileDTO::class) ->forMember( 'fullName', fn (Options $opts) => $opts->mapFrom( fn (AccountDTO $source) => sprintf('%s %s', $source->givenName, $source->familyName) ) ) ); $account = new AccountDTO(); $account->givenName = 'John'; $account->familyName = 'Doe'; $autoMapper = $config->createMapper(); $profile = $autoMapper->map($account, ProfileDTO::class); dump($profile);
The dump shows the new ProfileDTO with the mapped properties:
^ ProfileDTO^ {#45
-givenName: "John"
-familyName: "Doe"
-fullName: "John Doe"
}
For more examples and detailed usage instructions, please refer to the examples directory.
Contributing
Contributions to PHP AutoMapper are welcome! Whether it's adding new features, improving existing ones, or writing documentation, your help is appreciated. Please refer to the CONTRIBUTING.md file for guidelines on how to contribute to this project.
Commit messages
We are using Angular Commit Message Conventions.
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
The <type>
must be one of the following:
feat
: A new featurefix
: A bug fixdocs
: Documentation only changesstyle
: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)refactor
: A code change that neither fixes a bug nor adds a featureperf
: A code change that improves performancetest
: Adding missing or correcting existing testschore
: Changes to the build process or auxiliary tools and libraries such as documentation generation
License
PHP AutoMapper is open-sourced software licensed under the MIT license.
This project is not affiliated with the original AutoMapper project but is inspired by its functionality and aims to bring similar capabilities to the PHP community.