Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cf9d105

Browse filesBrowse files
committed
[AutoMapper] Object to Object mapper component
1 parent fbf6f56 commit cf9d105
Copy full SHA for cf9d105

File tree

Expand file treeCollapse file tree

19 files changed

+484
-0
lines changed
Filter options
Expand file treeCollapse file tree

19 files changed

+484
-0
lines changed
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Symfony\Component\AutoMapper\Attributes;
4+
5+
use Attribute;
6+
7+
/**
8+
* Condition whether to map a property.
9+
*
10+
* @experimental
11+
* @author Antoine Bluchet <soyuka@gmail.com>
12+
*/
13+
#[Attribute(Attribute::TARGET_PROPERTY)]
14+
final class MapIf
15+
{
16+
/**
17+
* @param callable $if
18+
*/
19+
public function __construct(public mixed $if)
20+
{
21+
}
22+
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Symfony\Component\AutoMapper\Attributes;
4+
5+
use Attribute;
6+
7+
/**
8+
* Configure a class or a property to map to.
9+
*
10+
* @experimental
11+
* @author Antoine Bluchet <soyuka@gmail.com>
12+
*/
13+
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)]
14+
final class MapTo
15+
{
16+
public function __construct(public string $to)
17+
{
18+
}
19+
}
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Symfony\Component\AutoMapper\Attributes;
4+
5+
use Attribute;
6+
7+
/**
8+
* Transform the mapped value with a funtion.
9+
*
10+
* @experimental
11+
* @author Antoine Bluchet <soyuka@gmail.com>
12+
*/
13+
#[Attribute(Attribute::TARGET_PROPERTY)]
14+
final class MapWith
15+
{
16+
/**
17+
* @param callable $with
18+
*/
19+
public function __construct(public mixed $with)
20+
{
21+
}
22+
}
+122Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace Symfony\Component\AutoMapper;
4+
5+
use Psr\Container\ContainerInterface;
6+
use Symfony\Component\AutoMapper\Exception\RuntimeException;
7+
use Symfony\Component\AutoMapper\Attributes\MapIf;
8+
use Symfony\Component\AutoMapper\Attributes\MapTo;
9+
use Symfony\Component\AutoMapper\Attributes\MapWith;
10+
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
11+
12+
/**
13+
* Object to object mapper.
14+
*
15+
* @experimental
16+
* @author Antoine Bluchet <soyuka@gmail.com>
17+
*/
18+
final class AutoMapper implements AutoMapperInterface
19+
{
20+
public function __construct(private ?PropertyAccessorInterface $propertyAccessor = null, private ?ContainerInterface $serviceLocator = null)
21+
{
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function map(object $object, object|string|null $to = null): mixed
28+
{
29+
$refl = new \ReflectionClass($object);
30+
31+
if (!$to) {
32+
$to = $this->getAttribute($refl, MapTo::class, true)->to;
33+
}
34+
35+
$arguments = [];
36+
if (is_object($to)) {
37+
$toRefl = new \ReflectionClass(get_class($to));
38+
$mapped = $to;
39+
} else {
40+
$toRefl = new \ReflectionClass($to);
41+
$mapped = $toRefl->newInstanceWithoutConstructor();
42+
}
43+
44+
$constructor = $toRefl->getConstructor();
45+
foreach ($constructor?->getParameters() ?? [] as $parameter) {
46+
$arguments[$parameter->getName()] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null;
47+
}
48+
49+
foreach ($refl->getProperties() as $property) {
50+
$propertyName = $property->getName();
51+
$mapTo = $this->getAttribute($property, MapTo::class)?->to ?? $propertyName;
52+
if (!$toRefl->hasProperty($mapTo)) {
53+
continue;
54+
}
55+
56+
$value = $this->propertyAccessor ? $this->propertyAccessor->getValue($object, $propertyName) : $object->{$propertyName};
57+
$mapIf = $this->getCallable($this->getAttribute($property, MapIf::class)?->if);
58+
if (is_callable($mapIf) && false === $this->call($mapIf, $value, $object)) {
59+
continue;
60+
}
61+
62+
$mapWith = $this->getCallable($this->getAttribute($property, MapWith::class)?->with);
63+
if (is_callable($mapWith)) {
64+
$value = $this->call($mapWith, $value, $object);
65+
}
66+
67+
if (is_object($value) && $to = $this->getAttribute(new \ReflectionClass(get_class($value)), MapTo::class)?->to) {
68+
$value = $this->map($value, $to);
69+
}
70+
71+
if (array_key_exists($mapTo, $arguments)) {
72+
$arguments[$mapTo] = $value;
73+
} else {
74+
$this->propertyAccessor ? $this->propertyAccessor->setValue($mapped, $mapTo, $value) : ($mapped->{$mapTo} = $value);
75+
}
76+
}
77+
78+
$constructor->invokeArgs($mapped, $arguments);
79+
80+
return $mapped;
81+
}
82+
83+
private function call(callable $fn, mixed $value, object $object)
84+
{
85+
$refl = new \ReflectionFunction(\Closure::fromCallable($fn));
86+
$withParameters = $refl->getParameters();
87+
$withArgs = [$value];
88+
89+
// Let's not send object if we don't need to, gives the ability to call native functions
90+
foreach ($withParameters as $parameter) {
91+
if ($parameter->getName() === 'object') {
92+
$withArgs['object'] = $object;
93+
break;
94+
}
95+
}
96+
97+
return call_user_func_array($fn, $withArgs);
98+
}
99+
100+
private function getCallable(string|callable|null $fn)
101+
{
102+
if ($this->serviceLocator && is_string($fn) && $this->serviceLocator->has($fn)) {
103+
return $this->serviceLocator->get($fn);
104+
}
105+
106+
return $fn;
107+
}
108+
109+
/**
110+
* @param class-string $name
111+
*/
112+
private function getAttribute(mixed $refl, string $name, bool $throw = false): mixed
113+
{
114+
$a = $refl->getAttributes($name)[0] ?? null;
115+
116+
if ($throw && !$a) {
117+
throw new RuntimeException(sprintf('Attribute of type "%s" expected on "%s.', $name, $refl->getName()));
118+
}
119+
120+
return $a ? $a->newInstance() : $a;
121+
}
122+
}
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Symfony\Component\AutoMapper;
4+
5+
/**
6+
* Object to object mapper.
7+
*
8+
* @experimental
9+
* @author Antoine Bluchet <soyuka@gmail.com>
10+
*/
11+
interface AutoMapperInterface {
12+
/**
13+
* @param object $object The object to map from
14+
* @param null|class-string|object $to The object or class to map to
15+
*/
16+
public function map(object $object, object|string|null $to = null): mixed;
17+
}
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
6.4
5+
---
6+
7+
* Automapper component
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\AutoMapper\Exception;
4+
5+
/**
6+
* @experimental
7+
* @author Antoine Bluchet <soyuka@gmail.com>
8+
*/
9+
interface ExceptionInterface extends \Throwable
10+
{
11+
}
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\AutoMapper\Exception;
4+
5+
/**
6+
* @experimental
7+
* @author Antoine Bluchet <soyuka@gmail.com>
8+
*/
9+
class RuntimeException extends \RuntimeException implements ExceptionInterface
10+
{
11+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.