Skip to content
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
18 changes: 15 additions & 3 deletions src/Transformer/ArrayTransformerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,24 @@ protected function createTransformer(Type $sourceType, Type $targetType, SourceP
return null;
}

if ([] === $sourceType->getCollectionValueTypes() || [] === $targetType->getCollectionValueTypes()) {
return new DictionaryTransformer(new CopyTransformer());
$sourceCollections = $sourceType->getCollectionValueTypes();
$targetCollections = $targetType->getCollectionValueTypes();

if ([] === $sourceCollections && [] !== $targetCollections) {
// consider array as a collection of array
$sourceCollections = [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, false)];
}

if ([] !== $sourceCollections && [] === $targetCollections) {
// consider array as a collection of array
$targetCollections = [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, false)];
}

$types = TypesMatching::fromSourceAndTargetTypes($sourceType->getCollectionValueTypes(), $targetType->getCollectionValueTypes());
if ([] === $sourceCollections || [] === $targetCollections) {
return new DictionaryTransformer(new CopyTransformer());
}

$types = TypesMatching::fromSourceAndTargetTypes($sourceCollections, $targetCollections);
$subItemTransformer = $this->chainTransformerFactory->getTransformer($types, $source, $target, $mapperMetadata);

if (null !== $subItemTransformer) {
Expand Down
28 changes: 28 additions & 0 deletions tests/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
use AutoMapper\Tests\Fixtures\ObjectsUnion\Foo;
use AutoMapper\Tests\Fixtures\ObjectsUnion\ObjectsUnionProperty;
use AutoMapper\Tests\Fixtures\ObjectWithDateTime;
use AutoMapper\Tests\Fixtures\ObjectWithPropertyAsUnknownArray\ComponentDto;
use AutoMapper\Tests\Fixtures\ObjectWithPropertyAsUnknownArray\Page;
use AutoMapper\Tests\Fixtures\ObjectWithPropertyAsUnknownArray\PageDto;
use AutoMapper\Tests\Fixtures\Order;
use AutoMapper\Tests\Fixtures\PetOwner;
use AutoMapper\Tests\Fixtures\PetOwnerWithConstructorArguments;
Expand Down Expand Up @@ -903,6 +906,31 @@ public function testIssue425(): void
self::assertEquals($data, $bar->property);
}

public function testObjectWithPropertyAsUnknownArrayToObject(): void
{
$entity = new Page();
$entity->components[] = ['name' => 'my name'];

$bar = $this->autoMapper->map($entity, PageDto::class);

self::assertEquals('my title', $bar->title);
self::assertCount(1, $bar->components);
self::assertInstanceOf(ComponentDto::class, $bar->components[0]);
self::assertEquals('my name', $bar->components[0]->name);
}

public function testObjectToObjectWithPropertyAsUnknownArray(): void
{
$dto = new PageDto('my title', [new ComponentDto('my name')]);
$bar = $this->autoMapper->map($dto, Page::class);

self::assertEquals('my title', $bar->title);
self::assertIsArray($bar->components);
self::assertCount(1, $bar->components);
self::assertIsArray($bar->components[0]);
self::assertEquals('my name', $bar->components[0]['name']);
}

public function testArrayWithKeys(): void
{
$arguments = ['foo', 'azerty' => 'bar', 'baz'];
Expand Down
13 changes: 13 additions & 0 deletions tests/Fixtures/ObjectWithPropertyAsUnknownArray/ComponentDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures\ObjectWithPropertyAsUnknownArray;

final readonly class ComponentDto
{
public function __construct(
public string $name,
) {
}
}
11 changes: 11 additions & 0 deletions tests/Fixtures/ObjectWithPropertyAsUnknownArray/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures\ObjectWithPropertyAsUnknownArray;

final class Page
{
public string $title = 'my title';
public array $components;
}
17 changes: 17 additions & 0 deletions tests/Fixtures/ObjectWithPropertyAsUnknownArray/PageDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures\ObjectWithPropertyAsUnknownArray;

final readonly class PageDto
{
/**
* @param list<ComponentDto> $components
*/
public function __construct(
public string $title,
public array $components,
) {
}
}