-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
base: 7.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||
{ | ||||||
/** | ||||||
* @param array<\Throwable> $innerExceptions The collection of inner exceptions reveals which discrete mapping has failed. | ||||||
*/ | ||||||
public function __construct(string $message, public readonly array $innerExceptions) | ||||||
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.
Suggested change
|
||||||
{ | ||||||
parent::__construct($message); | ||||||
} | ||||||
} |
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 | ||||||
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.
Suggested change
|
||||||
{ | ||||||
public function __construct(private ObjectMapperInterface $mapper) | ||||||
{ | ||||||
} | ||||||
|
||||||
public function yieldMappedObjects(array $sourceCollection, ?string $target = null): \Generator | ||||||
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. 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; | ||||||
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. we certainly need an option to throw on first error (or to not try/catching anything), I wanted to avoid any |
||||||
} | ||||||
} | ||||||
|
||||||
if ($exceptions) { | ||||||
throw new MapMultipleAggregateException('Mapping source collection has failed.', $exceptions); | ||||||
} | ||||||
} | ||||||
} |
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 | ||
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.
|
||
{ | ||
/** | ||
* @template T of object | ||
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. Should this annotation be on the class instead of the method, so devs can use 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. @alexandre-daubois I suggest to keep it consistent with the |
||
* | ||
* @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; | ||
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. I'd imagine something like: interface CollectionMapperInterface
{
public function map(iterable $source, object|string|null $target = null): \Generator;
} |
||
} |
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); | ||
} | ||
} |
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.
Something like
WrappedMappingException
? This naming idea comes from messenger'sWrappedExceptionsInterface
.