Skip to content

Commit 5afc279

Browse files
lxixbocharsky-bw
andauthored
Add mapMultiple method (#15)
Co-authored-by: bocharsky-bw <[email protected]>
1 parent 3ea3cb2 commit 5afc279

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

src/MicroMapper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,15 @@ public function map(object $from, string $toClass, array $context = []): object
8484

8585
throw new \Exception(sprintf('No mapper found for %s -> %s', $from::class, $toClass));
8686
}
87+
88+
public function mapMultiple(iterable $fromIterable, string $toClass, array $context = []): array
89+
{
90+
$toObjects = [];
91+
92+
foreach ($fromIterable as $from) {
93+
$toObjects[] = $this->map($from, $toClass, $context);
94+
}
95+
96+
return $toObjects;
97+
}
8798
}

src/MicroMapperInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,13 @@ interface MicroMapperInterface
2424
* @return TTo
2525
*/
2626
public function map(object $from, string $toClass, array $context = []): object;
27+
28+
/**
29+
* @template TTo of object
30+
*
31+
* @param class-string<TTo> $toClass
32+
*
33+
* @return list<TTo>
34+
*/
35+
public function mapMultiple(iterable $fromIterable, string $toClass, array $context = []): array;
2736
}

tests/Unit/MicroMapperTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,25 @@ public function testMap()
4242
$this->assertSame('North America', $dto->name);
4343
$this->assertSame('temperate', $dto->climate);
4444
$this->assertCount(2, $dto->dinosaursMappedShallow);
45+
$this->assertCount(2, $dto->dinosaursMultiMappedShallow);
4546
$this->assertCount(2, $dto->dinosaursMappedDeep);
47+
$this->assertCount(2, $dto->dinosaursMultiMappedDeep);
4648

4749
// id is mapped for both deep and shallow
4850
$this->assertSame(3, $dto->dinosaursMappedShallow[0]->id);
51+
$this->assertSame(3, $dto->dinosaursMultiMappedShallow[0]->id);
4952
$this->assertSame(3, $dto->dinosaursMappedDeep[0]->id);
53+
$this->assertSame(3, $dto->dinosaursMultiMappedDeep[0]->id);
5054
// further properties are only in the deep
5155
$this->assertNull($dto->dinosaursMappedShallow[0]->genus);
56+
$this->assertNull($dto->dinosaursMultiMappedShallow[0]->genus);
5257
$this->assertSame('Velociraptor', $dto->dinosaursMappedDeep[0]->genus);
58+
$this->assertSame('Velociraptor', $dto->dinosaursMultiMappedDeep[0]->genus);
5359
// the deep will have a region, but it will be shallow
5460
$this->assertSame($dto->dinosaursMappedDeep[0]->region->id, 1);
61+
$this->assertSame($dto->dinosaursMultiMappedDeep[0]->region->id, 1);
5562
$this->assertNull($dto->dinosaursMappedDeep[0]->region->name);
63+
$this->assertNull($dto->dinosaursMultiMappedDeep[0]->region->name);
5664

5765
$reflectionObject = new \ReflectionObject($mapper);
5866
$objectHashesProperty = $reflectionObject->getProperty('objectHashes');

tests/fixtures/DinoRegionDto.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@ class DinoRegionDto
2222
* @var array DinosaurDto[]
2323
*/
2424
public array $dinosaursMappedDeep = [];
25+
/**
26+
* @var array DinosaurDto[]
27+
*/
28+
public array $dinosaursMultiMappedShallow = [];
29+
/**
30+
* @var array DinosaurDto[]
31+
*/
32+
public array $dinosaursMultiMappedDeep = [];
2533
}

tests/fixtures/DinoRegionToDtoMapper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public function populate(object $from, object $to, array $context): object
4343
}
4444
$to->dinosaursMappedShallow = $shallowDinosaurDtos;
4545

46+
$to->dinosaursMultiMappedShallow = $this->microMapper->mapMultiple($from->dinosaurs, DinosaurDto::class, [
47+
MicroMapperInterface::MAX_DEPTH => 0,
48+
]);
49+
4650
$deepDinosaurDtos = [];
4751
foreach ($from->dinosaurs as $dino) {
4852
$deepDinosaurDtos[] = $this->microMapper->map($dino, DinosaurDto::class, [
@@ -51,6 +55,10 @@ public function populate(object $from, object $to, array $context): object
5155
}
5256
$to->dinosaursMappedDeep = $deepDinosaurDtos;
5357

58+
$to->dinosaursMultiMappedDeep = $this->microMapper->mapMultiple($from->dinosaurs, DinosaurDto::class, [
59+
MicroMapperInterface::MAX_DEPTH => 1,
60+
]);
61+
5462
return $to;
5563
}
5664
}

0 commit comments

Comments
 (0)