-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[ObjectMapper] Object to Object mapper component #51741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.git* export-ignore |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Check subtree split | ||
|
||
on: | ||
pull_request_target: | ||
|
||
jobs: | ||
close-pull-request: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Close pull request | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
if (context.repo.owner === "symfony") { | ||
github.rest.issues.createComment({ | ||
owner: "symfony", | ||
repo: context.repo.repo, | ||
issue_number: context.issue.number, | ||
body: ` | ||
Thanks for your Pull Request! We love contributions. | ||
|
||
However, you should instead open your PR on the main repository: | ||
https://github.com/symfony/symfony | ||
|
||
This repository is what we call a "subtree split": a read-only subset of that main repository. | ||
We're looking forward to your PR there! | ||
` | ||
}); | ||
|
||
github.rest.pulls.update({ | ||
owner: "symfony", | ||
repo: context.repo.repo, | ||
pull_number: context.issue.number, | ||
state: "closed" | ||
}); | ||
} | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\ObjectMapper\Attribute; | ||
|
||
/** | ||
* Configures a class or a property to map to. | ||
* | ||
* @experimental | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)] | ||
readonly class Map | ||
{ | ||
/** | ||
* @param string|class-string|null $source The property or the class to map from | ||
* @param string|class-string|null $target The property or the class to map to | ||
* @param string|bool|callable(mixed, object): bool|null $if A boolean, a service id or a callable that instructs whether to map | ||
* @param (string|callable(mixed, object): mixed)|(string|callable(mixed, object): mixed)[]|null $transform A service id or a callable that transforms the value during mapping | ||
soyuka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public function __construct( | ||
public ?string $target = null, | ||
public ?string $source = null, | ||
public mixed $if = null, | ||
public mixed $transform = null, | ||
) { | ||
soyuka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.3 | ||
--- | ||
|
||
* Add the component as experimental |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\ObjectMapper; | ||
|
||
/** | ||
* Service used by "Map::if". | ||
* | ||
* @template T of object | ||
* | ||
* @experimental | ||
* | ||
* {@see Symfony\Component\ObjectMapper\Attribute\Map} | ||
*/ | ||
interface ConditionCallableInterface | ||
{ | ||
/** | ||
* @param mixed $value The value being mapped | ||
* @param T $object The object we're working on | ||
*/ | ||
public function __invoke(mixed $value, object $object): bool; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\ObjectMapper\Exception; | ||
|
||
/** | ||
* @experimental | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
*/ | ||
interface ExceptionInterface extends \Throwable | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\ObjectMapper\Exception; | ||
|
||
/** | ||
* @experimental | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
*/ | ||
class MappingException extends RuntimeException | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\ObjectMapper\Exception; | ||
|
||
/** | ||
* @experimental | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
*/ | ||
final class MappingTransformException extends RuntimeException | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\ObjectMapper\Exception; | ||
|
||
/** | ||
* @experimental | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
*/ | ||
class RuntimeException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2025-present Fabien Potencier | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\ObjectMapper\Metadata; | ||
|
||
/** | ||
* Configures a class or a property to map to. | ||
* | ||
* @internal | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
*/ | ||
readonly class Mapping | ||
{ | ||
/** | ||
* @param string|class-string|null $source The property or the class to map from | ||
* @param string|class-string|null $target The property or the class to map to | ||
* @param string|bool|callable(mixed, object): bool|null $if A boolean, Symfony service name or a callable that instructs whether to map | ||
* @param (string|callable(mixed, object): mixed)|(string|callable(mixed, object): mixed)[]|null $transform A service id or a callable that transform the value during mapping | ||
*/ | ||
public function __construct( | ||
public ?string $target = null, | ||
public ?string $source = null, | ||
public mixed $if = null, | ||
public mixed $transform = null, | ||
) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\ObjectMapper\Metadata; | ||
|
||
/** | ||
* Factory to create Mapper metadata. | ||
* | ||
* @experimental | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
*/ | ||
interface ObjectMapperMetadataFactoryInterface | ||
{ | ||
/** | ||
* @param array<string, mixed> $context | ||
* | ||
* @return list<Mapping> | ||
*/ | ||
public function create(object $object, ?string $property = null, array $context = []): array; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @soyuka Shouldn't a |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\ObjectMapper\Metadata; | ||
|
||
use Symfony\Component\ObjectMapper\Attribute\Map; | ||
use Symfony\Component\ObjectMapper\Exception\MappingException; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
*/ | ||
final class ReflectionObjectMapperMetadataFactory implements ObjectMapperMetadataFactoryInterface | ||
{ | ||
public function create(object $object, ?string $property = null, array $context = []): array | ||
{ | ||
try { | ||
$refl = new \ReflectionClass($object); | ||
$mapTo = []; | ||
foreach (($property ? $refl->getProperty($property) : $refl)->getAttributes(Map::class, \ReflectionAttribute::IS_INSTANCEOF) as $mapAttribute) { | ||
$map = $mapAttribute->newInstance(); | ||
$mapTo[] = new Mapping($map->target, $map->source, $map->if, $map->transform); | ||
} | ||
|
||
return $mapTo; | ||
} catch (\ReflectionException $e) { | ||
throw new MappingException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.