Skip to content

[ObjectMapper] Map a collection of objects #60615

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
wants to merge 2 commits into
base: 7.4
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/ObjectMapper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Map a collection of source objects via `MapMultiple::yieldMappedObjects()`

7.3
---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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 Martin Komischke <martin.komischke@gmail.com>
*/
final class MapMultipleAggregateException extends RuntimeException
Copy link
Contributor

Choose a reason for hiding this comment

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

Something like WrappedMappingException? This naming idea comes from messenger's WrappedExceptionsInterface.

{
/**
* @param array<\Throwable> $innerExceptions The collection of inner exceptions reveals which discrete mapping has failed.
*/
public function __construct(string $message, public readonly array $innerExceptions)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public function __construct(string $message, public readonly array $innerExceptions)
public function __construct(string $message, public readonly array $exceptions)

{
parent::__construct($message);
}
}
45 changes: 45 additions & 0 deletions src/Symfony/Component/ObjectMapper/MapMultiple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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;

use Symfony\Component\ObjectMapper\Exception\MapMultipleAggregateException;

/**
* Map multiple objects using the ObjectMapperInterface
*
* @experimental
*
* @author Martin Komischke <martin.komischke@gmail.com>
*/
final class MapMultiple implements MapMultipleInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
final class MapMultiple implements MapMultipleInterface
final class CollectionMapper implements CollectionMapperInterface

{
public function __construct(private ObjectMapperInterface $mapper)
{
}

public function yieldMappedObjects(array $sourceCollection, ?string $target = null): \Generator
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd use something like this:

public function map(iterable $source, object|string|null $target = null): \Generator

{
$exceptions = [];

foreach ($sourceCollection as $sourceObject) {
try {
yield $this->mapper->map($sourceObject, $target);
} catch (\Throwable $ex) {
$exceptions[] = $ex;
Copy link
Contributor

Choose a reason for hiding this comment

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

we certainly need an option to throw on first error (or to not try/catching anything), I wanted to avoid any $context maybe that it could be a constructor parameter?

}
}

if ($exceptions) {
throw new MapMultipleAggregateException('Mapping source collection has failed.', $exceptions);
}
}
}
34 changes: 34 additions & 0 deletions src/Symfony/Component/ObjectMapper/MapMultipleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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;

use Symfony\Component\ObjectMapper\Exception\MapMultipleAggregateException;

/**
* Map multiple objects using the ObjectMapper
*
* @experimental
*
* @author Martin Komischke <martin.komischke@gmail.com>
*/
interface MapMultipleInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

CollectionMapperInterface (to be consistent with ObjectMapperInterface).

{
/**
* @template T of object
Copy link
Member

Choose a reason for hiding this comment

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

Should this annotation be on the class instead of the method, so devs can use @implements MapMultipleInterface<Something>? I'm not sure this works well if the template definition is on the method

Copy link
Author

Choose a reason for hiding this comment

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

@alexandre-daubois I suggest to keep it consistent with the ObjectMapperInterface in the same package. The annotation is on the method there too. What do you think?

*
* @param array<T> $sourceCollection The array of objects to map from
* @return \Generator<int, object, mixed, void> yields a target object for each source object
*
* @throws MapMultipleAggregateException When mapping at least one of the source objects has failed.
*/
public function yieldMappedObjects(array $sourceCollection, string|null $target = null): \Generator;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd imagine something like:

interface CollectionMapperInterface
{
    public function map(iterable $source, object|string|null $target = null): \Generator;
}

}
83 changes: 83 additions & 0 deletions src/Symfony/Component/ObjectMapper/Tests/MapMultipleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\ObjectMapper\Exception\MapMultipleAggregateException;
use Symfony\Component\ObjectMapper\MapMultiple;
use Symfony\Component\ObjectMapper\ObjectMapper;
use Symfony\Component\ObjectMapper\Tests\Fixtures\C;
use Symfony\Component\ObjectMapper\Tests\Fixtures\D;

final class MapMultipleTest extends TestCase
{
public function testMappingSucceeds()
{
$source = [
new C(
foo: 'foo1',
bar: 'bar1'
),
new C(
foo: 'foo2',
bar: 'bar2'
),
];

$mapper = new MapMultiple(new ObjectMapper());

$target = iterator_to_array($mapper->yieldMappedObjects($source, D::class));

self::assertIsArray($target);
self::assertCount(2, $target);

self::assertInstanceOf(D::class, $target[0]);
/** @var D $firstTarget */
$firstTarget = $target[0];
self::assertSame('foo1', $firstTarget->baz);
self::assertSame('bar1', $firstTarget->bat);

self::assertInstanceOf(D::class, $target[1]);
/** @var D $secondTarget */
$secondTarget = $target[1];
self::assertSame('foo2', $secondTarget->baz);
self::assertSame('bar2', $secondTarget->bat);
}

public function testMappingSecondSourceObjectThrows()
{
$sourceCollection = [
new class('value1', 'value2') {
public function __construct(
public string $baz,
public string $bat
) {
}
},
new class(/* Nothing to map */) {}
];

$mapper = new MapMultiple(new ObjectMapper());

$targetCollection = [];

try {
foreach ($mapper->yieldMappedObjects($sourceCollection, D::class) as $targetObject) {
$targetCollection[] = $targetObject;
}

self::fail('Mapping second object should have thrown!');
} catch (MapMultipleAggregateException $ex) {
self::assertSame('Mapping source collection has failed.', $ex->getMessage());
self::assertCount(1, $ex->innerExceptions);
self::assertSame('Undefined property: class@anonymous::$baz', $ex->innerExceptions[0]->getMessage());
}

self::assertCount(1, $targetCollection, 'Mapping first object should have passed!');
self::assertInstanceOf(D::class, $targetCollection[0]);
/** @var D $firstTarget */
$firstTarget = $targetCollection[0];
self::assertSame('value1', $firstTarget->baz);
self::assertSame('value2', $firstTarget->bat);
}
}