-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[ObjectMapper] embed collection transformer #60442
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
Open
soyuka
wants to merge
3
commits into
symfony:7.4
Choose a base branch
from
soyuka:map-collection
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+104
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
...ymfony/Component/ObjectMapper/Tests/Fixtures/TransformCollection/TransformCollectionA.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection; | ||
|
||
use Symfony\Component\ObjectMapper\Attribute\Map; | ||
use Symfony\Component\ObjectMapper\Transform\MapCollection; | ||
|
||
class TransformCollectionA | ||
{ | ||
#[Map(transform: new MapCollection())] | ||
/** @var TransformCollectionC[] */ | ||
public array $foo; | ||
} |
9 changes: 9 additions & 0 deletions
9
...ymfony/Component/ObjectMapper/Tests/Fixtures/TransformCollection/TransformCollectionB.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection; | ||
|
||
class TransformCollectionB | ||
{ | ||
/** @var TransformCollectionD[] */ | ||
public array $foo; | ||
} |
14 changes: 14 additions & 0 deletions
14
...ymfony/Component/ObjectMapper/Tests/Fixtures/TransformCollection/TransformCollectionC.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection; | ||
|
||
use Symfony\Component\ObjectMapper\Attribute\Map; | ||
|
||
#[Map(target: TransformCollectionD::class)] | ||
class TransformCollectionC | ||
{ | ||
public function __construct( | ||
#[Map(target: 'baz')] | ||
public string $bar | ||
) {} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ymfony/Component/ObjectMapper/Tests/Fixtures/TransformCollection/TransformCollectionD.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\TransformCollection; | ||
|
||
class TransformCollectionD | ||
{ | ||
public function __construct( | ||
public string $baz | ||
) {} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/Symfony/Component/ObjectMapper/Transform/MapCollection.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?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\Transform; | ||
|
||
use Symfony\Component\ObjectMapper\Exception\MappingException; | ||
use Symfony\Component\ObjectMapper\ObjectMapper; | ||
use Symfony\Component\ObjectMapper\ObjectMapperInterface; | ||
use Symfony\Component\ObjectMapper\TransformCallableInterface; | ||
|
||
/** | ||
* @template T of object | ||
* | ||
* @implements TransformCallableInterface<object, T> | ||
*/ | ||
class MapCollection implements TransformCallableInterface | ||
{ | ||
public function __construct(private ObjectMapperInterface $objectMapper = new ObjectMapper()) | ||
{ | ||
} | ||
|
||
public function __invoke(mixed $value, object $source, ?object $target): mixed | ||
{ | ||
if (!is_iterable($value)) { | ||
throw new MappingException(sprintf('The MapCollection transform expects an iterable, "%s" given.', get_debug_type($value))); | ||
} | ||
|
||
$values = []; | ||
foreach ($value as $v) { | ||
$values[] = $this->objectMapper->map($v); | ||
} | ||
|
||
return $values; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name "$value" does not say anything about what this parameter means from a semantical POV.
"$sourceProperty" might be a better name. Calling a field/property of a class a "variable" is very much confusing to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree but the convention is to name the variable the same as its defined inside the interface, here its the value being mapped.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uff. Then this should definitely be changed in the interface. A field/property is not a variable ☝️🤓
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the value of an object's key, in PHP it's not necessary a property though its nitpicking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also forgot but this $value may also be the object being mapped :)