8000 [ObjectMapper] Object to Object mapper component by soyuka · Pull Request #51741 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"symfony/mime": "self.version",
"symfony/monolog-bridge": "self.version",
"symfony/notifier": "self.version",
"symfony/object-mapper": "self.version",
"symfony/options-resolver": "self.version",
"symfony/password-hasher": "self.version",
"symfony/process": "self.version",
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/ObjectMapper/.gitattributes
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.

9E7A
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.

3 changes: 3 additions & 0 deletions src/Symfony/Component/ObjectMapper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
37 changes: 37 additions & 0 deletions src/Symfony/Component/ObjectMapper/Attribute/Map.php
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
*/
public function __construct(
public ?string $target = null,
public ?string $source = null,
public mixed $if = null,
public mixed $transform = null,
) {
}
}
7 changes: 7 additions & 0 deletions src/Symfony/Component/ObjectMapper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

7.3
---

* Add the component as experimental
30 changes: 30 additions & 0 deletions src/Symfony/Component/ObjectMapper/ConditionCallableInterface.php
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
{
}
21 changes: 21 additions & 0 deletions src/Symfony/Component/ObjectMapper/Exception/MappingException.php
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
{
}
21 changes: 21 additions & 0 deletions src/Symfony/Component/ObjectMapper/Exception/RuntimeException.php
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
{
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/ObjectMapper/LICENSE
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.
36 changes: 36 additions & 0 deletions src/Symfony/Component/ObjectMapper/Metadata/Mapping.php
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@soyuka Shouldn't a @throws MappingException be added to this interface too since ReflectionObjectMapperMetadataFactory are throwing some ? I can do the PR if you agree with

}
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);
}
}
}
Loading
Loading
0