Commit 21b4dd7
committed
feature symfony#51741 [ObjectMapper] Object to Object mapper component (soyuka)
This PR was squashed before being merged into the 7.3 branch.
Discussion
----------
[ObjectMapper] Object to Object mapper component
| Q | A
| ------------- | ---
| Branch? | 7.3
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| License | MIT
| Doc PR | TBD this description will be used as docs
## Why ?
In the train back from [API Platform Con](https://api-platform.com/con/2023/) and after watching `@weaverryan` conference about API Platform's feature that helps [separating Doctrine Entities from POPOs](https://symfonycasts.com/screencast/api-platform-extending/state-options), we had the feeling that such a component is definitely missing from the Symfony ecosystem. Current implementations (automapper-plus, janephp/automapper) are good but feel to complicated when you want something accessible (DX and complexity). Here we're not trying to have exceptional performance, we just want an accessible API with few mandatory features. Basically those are:
- map properties from a class/object to another class/object (`Map(target: A::class)`)
- configure the property name each property is mapped to (`Map(target: 'prop')`)
- change the value during transformation (`Map(transform: 'ucfirst')`)
- control whether the value should be mapped or not (`Map(if: 'boolval')`)
- reuse objects if possible (works better with doctrine UOW)
Other implementation details:
- if a property exists in B, it'll "automatically" be mapped (doesn't handle type errors, should we ?)
- if a property doesn't exists in B it'll "automatically" **not** be mapped
The rest is not "auto".
## Mapper Component
The Mapper component allows you to map an object to another object,
facilitating the mapping using attributes.
### Usage
```php
use Symfony\Component\ObjectMapper\ObjectMapper;
use Symfony\Component\ObjectMapper\Attributes\Map;
// This maps class `A` to class `B`.
#[Map(B::class)]
class A
{
// This maps A::foo to B::bar.
#[Map(target: 'bar')]
public string $foo;
// This calls ucfirst on A::transform
#[Map(transform: 'ucfirst')]
public string $name;
// This doesn't map A::bar if it's value is falsy.
#[Map(if: 'boolval')]
public bool $bar = false;
}
$mapper = new ObjectMapper();
$mapper->map(new A);
```
The `Map` attribute has the following signature:
```php
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class Map
{
/**
*
* `@param` null|string|class-string $source The property or the class to map to
* `@param` null|string|class-string $target The property or the class to map from
* `@param` string|callable(mixed $value, object $object): bool $if A Symfony service name or a callable that instructs whether to map
* `@param` CallableType|CallableType[] $transform A Symfony service name or a callable that transform the value during mapping
*/
public function __construct(
public readonly ?string $source = null,
public readonly ?string $target = null,
public readonly mixed $if = null,
public readonly mixed $transform = null
){
}
}
```
`if` and `transform` are callable or Symfony services, not that we need to introduce an interface for services to implement, this will follow if we want to introduce the component inside the Framework Bundle.
The mapper also takes a `$source` argument if one needs to update an object instead of creating a new one:
```php
$b = new B;
$mapper = new ObjectMapper();
$mapper->map(new A, $b);
```
## TODO
- [x] AutoMapper or Automapper or something else? `Mapper` component with an `AutoMapper` implementation.
- [x] max depth? Supports recursivity, `maxDepth` is for serializers.
Commits
-------
3f99b03 [ObjectMapper] Object to Object mapper componentFile tree
53 files changed
+1763
-0
lines changed- src/Symfony/Component/ObjectMapper
- .github
- workflows
- Attribute
- Exception
- Metadata
- Tests
- Fixtures
- DeeperRecursion
- Flatten
- HydrateObject
- InstanceCallback
- MapStruct
- MultipleTargets
- Recursion
- ServiceLocator
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
53 files changed
+1763
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| 94 | + | |
94 | 95 | | |
95 | 96 | | |
96 | 97 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
Lines changed: 30 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
Lines changed: 21 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
0 commit comments