Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit3488fd9

Browse files
committed
[AutoMapper] Object to Object mapper component
1 parentf3c9644 commit3488fd9

19 files changed

+494
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Testsexport-ignore
2+
/phpunit.xml.distexport-ignore
3+
/.gitattributesexport-ignore
4+
/.gitignoreexport-ignore
Lines 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
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AutoMapper\Attributes;
4+
5+
useAttribute;
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+
finalclass MapIf
15+
{
16+
/**
17+
* @param string|callable(mixed $value, object $object): bool $if
18+
*/
19+
publicfunction__construct(publicreadonlystring|callable$if)
20+
{
21+
}
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AutoMapper\Attributes;
4+
5+
useAttribute;
6+
7+
/**
8+
* Configures 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+
finalclass MapTo
15+
{
16+
publicfunction__construct(publicreadonlystring$to)
17+
{
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AutoMapper\Attributes;
4+
5+
useAttribute;
6+
7+
/**
8+
* Transforms the mapped value with a function.
9+
*
10+
* @experimental
11+
* @author Antoine Bluchet <soyuka@gmail.com>
12+
*/
13+
#[Attribute(Attribute::TARGET_PROPERTY)]
14+
finalclass MapWith
15+
{
16+
/**
17+
* @param string|callable(mixed $value, object $object): bool $with
18+
*/
19+
publicfunction__construct(publicreadonlycallable|string$with)
20+
{
21+
}
22+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AutoMapper;
4+
5+
usePsr\Container\ContainerInterface;
6+
useSymfony\Component\AutoMapper\Exception\RuntimeException;
7+
useSymfony\Component\AutoMapper\Attributes\MapIf;
8+
useSymfony\Component\AutoMapper\Attributes\MapTo;
9+
useSymfony\Component\AutoMapper\Attributes\MapWith;
10+
useSymfony\Component\PropertyAccess\PropertyAccessorInterface;
11+
12+
/**
13+
* Object to object mapper.
14+
*
15+
* @experimental
16+
* @author Antoine Bluchet <soyuka@gmail.com>
17+
*/
18+
finalclass AutoMapperimplements AutoMapperInterface
19+
{
20+
publicfunction__construct(privatereadonly ?PropertyAccessorInterface$propertyAccessor =null,privatereadonly ?ContainerInterface$serviceLocator =null)
21+
{
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
publicfunctionmap(object$object,object|string$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+
if ($property->isStatic() {
51+
continue;
52+
}
53+
$propertyName =$property->getName();
54+
$mapTo =$this->getAttribute($property, MapTo::class)?->to ??$propertyName;
55+
if (!$toRefl->hasProperty($mapTo)) {
56+
continue;
57+
}
58+
59+
$value =$this->propertyAccessor ?$this->propertyAccessor->getValue($object,$propertyName) :$object->{$propertyName};
60+
$mapIf =$this->getCallable($this->getAttribute($property, MapIf::class)?->if);
61+
if (is_callable($mapIf) &&false ===$this->call($mapIf,$value,$object)) {
62+
continue;
63+
}
64+
65+
$mapWith =$this->getCallable($this->getAttribute($property, MapWith::class)?->with);
66+
if (is_callable($mapWith)) {
67+
$value =$this->call($mapWith,$value,$object);
68+
}
69+
70+
if (is_object($value) &&$to =$this->getAttribute(new \ReflectionClass($value), MapTo::class)?->to) {
71+
$value =$this->map($value,$to);
72+
}
73+
74+
if (array_key_exists($mapTo,$arguments)) {
75+
$arguments[$mapTo] =$value;
76+
}else {
77+
$this->propertyAccessor ?$this->propertyAccessor->setValue($mapped,$mapTo,$value) : ($mapped->{$mapTo} =$value);
78+
}
79+
}
80+
81+
$constructor->invokeArgs($mapped,$arguments);
82+
83+
return$mapped;
84+
}
85+
86+
privatefunctioncall(callable$fn,mixed$value,object$object):mixed
87+
{
88+
$refl =new \ReflectionFunction(\Closure::fromCallable($fn));
89+
$withParameters =$refl->getParameters();
90+
$withArgs = [$value];
91+
92+
// Let's not send object if we don't need to, gives the ability to call native functions
93+
foreach ($withParametersas$parameter) {
94+
if ($parameter->getName() ==='object') {
95+
$withArgs['object'] =$object;
96+
break;
97+
}
98+
}
99+
100+
returncall_user_func_array($fn,$withArgs);
101+
}
102+
103+
privatefunctiongetCallable(string|callable|null$fn): ?callable
104+
{
105+
if ($this->serviceLocator &&is_string($fn)) {
106+
return$this->serviceLocator->get($fn);
107+
}
108+
109+
return$fn;
110+
}
111+
112+
/**
113+
* @param class-string $name
114+
*/
115+
privatefunctiongetAttribute(mixed$refl,string$name,bool$throw =false):mixed
116+
{
117+
$a =$refl->getAttributes($name)[0] ??null;
118+
119+
if ($throw && !$a) {
120+
thrownewRuntimeException(sprintf('Attribute of type "%s" expected on "%s.',$name,$refl->getName()));
121+
}
122+
123+
return$a ?$a->newInstance() :$a;
124+
}
125+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespaceSymfony\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+
publicfunctionmap(object$object,object|string|null$to =null):mixed;
17+
}
Lines 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
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AutoMapper\Exception;
4+
5+
/**
6+
* @experimental
7+
* @author Antoine Bluchet <soyuka@gmail.com>
8+
*/
9+
interface ExceptionInterfaceextends \Throwable
10+
{
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AutoMapper\Exception;
4+
5+
/**
6+
* @experimental
7+
* @author Antoine Bluchet <soyuka@gmail.com>
8+
*/
9+
class RuntimeExceptionextends \RuntimeExceptionimplements ExceptionInterface
10+
{
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2004-present Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
AutoMapper Component
2+
================
3+
4+
The AutoMapper component allows you to map an object to another object,
5+
facilitating the mapping using attributes.
6+
7+
Resources
8+
---------
9+
10+
*[Documentation](https://symfony.com/doc/current/components/automapper.html)
11+
*[Contributing](https://symfony.com/doc/current/contributing/index.html)
12+
*[Report issues](https://github.com/symfony/symfony/issues) and
13+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
14+
in the[main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespaceSymfony\Component\Automapper\Tests\Fixtures;
4+
5+
useSymfony\Component\AutoMapper\Attributes\MapIf;
6+
useSymfony\Component\AutoMapper\Attributes\MapTo;
7+
useSymfony\Component\AutoMapper\Attributes\MapWith;
8+
9+
#[MapTo(B::class)]
10+
class A
11+
{
12+
#[MapTo('bar')]
13+
publicstring$foo;
14+
15+
publicstring$baz;
16+
17+
publicstring$notinb;
18+
19+
#[MapWith('strtoupper')]
20+
publicstring$transform;
21+
22+
#[MapWith([A::class,'concatFn'])]
23+
public ?string$concat =null;
24+
25+
#[MapIf('boolval')]
26+
publicbool$nomap =false;
27+
28+
publicC$relation;
29+
30+
publicD$relationNotMapped;
31+
32+
publicfunctiongetConcat()
33+
{
34+
return'should';
35+
}
36+
37+
publicstaticfunctionconcatFn($v,$object):string
38+
{
39+
return$v .$object->foo .$object->baz;
40+
}
41+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespaceSymfony\Component\Automapper\Tests\Fixtures;
4+
5+
class B
6+
{
7+
publicfunction__construct(privatestring$bar)
8+
{
9+
}
10+
publicstring$baz;
11+
publicstring$transform;
12+
publicstring$concat;
13+
publicbool$nomap =true;
14+
publicint$id;
15+
publicD$relation;
16+
publicD$relationNotMapped;
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespaceSymfony\Component\Automapper\Tests\Fixtures;
4+
5+
useSymfony\Component\AutoMapper\Attributes\MapTo;
6+
7+
#[MapTo(D::class)]
8+
class C
9+
{
10+
publicfunction__construct(#[MapTo('baz')]publicstring$foo, #[MapTo('bat')]publicstring$bar)
11+
{
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AutoMapper\Tests\Fixtures;
4+
5+
class D
6+
{
7+
publicfunction__construct(publicstring$baz,publicstring$bat)
8+
{
9+
}
10+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp