Skip to content

Commit

Permalink
Add mapMultiple method (#15)
Browse files Browse the repository at this point in the history
Co-authored-by: bocharsky-bw <[email protected]>
  • Loading branch information
lxix and bocharsky-bw authored Jun 27, 2024
1 parent 3ea3cb2 commit 5afc279
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/MicroMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,15 @@ public function map(object $from, string $toClass, array $context = []): object

throw new \Exception(sprintf('No mapper found for %s -> %s', $from::class, $toClass));
}

public function mapMultiple(iterable $fromIterable, string $toClass, array $context = []): array
{
$toObjects = [];

foreach ($fromIterable as $from) {
$toObjects[] = $this->map($from, $toClass, $context);
}

return $toObjects;
}
}
9 changes: 9 additions & 0 deletions src/MicroMapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ interface MicroMapperInterface
* @return TTo
*/
public function map(object $from, string $toClass, array $context = []): object;

/**
* @template TTo of object
*
* @param class-string<TTo> $toClass
*
* @return list<TTo>
*/
public function mapMultiple(iterable $fromIterable, string $toClass, array $context = []): array;
}
8 changes: 8 additions & 0 deletions tests/Unit/MicroMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,25 @@ public function testMap()
$this->assertSame('North America', $dto->name);
$this->assertSame('temperate', $dto->climate);
$this->assertCount(2, $dto->dinosaursMappedShallow);
$this->assertCount(2, $dto->dinosaursMultiMappedShallow);
$this->assertCount(2, $dto->dinosaursMappedDeep);
$this->assertCount(2, $dto->dinosaursMultiMappedDeep);

// id is mapped for both deep and shallow
$this->assertSame(3, $dto->dinosaursMappedShallow[0]->id);
$this->assertSame(3, $dto->dinosaursMultiMappedShallow[0]->id);
$this->assertSame(3, $dto->dinosaursMappedDeep[0]->id);
$this->assertSame(3, $dto->dinosaursMultiMappedDeep[0]->id);
// further properties are only in the deep
$this->assertNull($dto->dinosaursMappedShallow[0]->genus);
$this->assertNull($dto->dinosaursMultiMappedShallow[0]->genus);
$this->assertSame('Velociraptor', $dto->dinosaursMappedDeep[0]->genus);
$this->assertSame('Velociraptor', $dto->dinosaursMultiMappedDeep[0]->genus);
// the deep will have a region, but it will be shallow
$this->assertSame($dto->dinosaursMappedDeep[0]->region->id, 1);
$this->assertSame($dto->dinosaursMultiMappedDeep[0]->region->id, 1);
$this->assertNull($dto->dinosaursMappedDeep[0]->region->name);
$this->assertNull($dto->dinosaursMultiMappedDeep[0]->region->name);

$reflectionObject = new \ReflectionObject($mapper);
$objectHashesProperty = $reflectionObject->getProperty('objectHashes');
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/DinoRegionDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ class DinoRegionDto
* @var array DinosaurDto[]
*/
public array $dinosaursMappedDeep = [];
/**
* @var array DinosaurDto[]
*/
public array $dinosaursMultiMappedShallow = [];
/**
* @var array DinosaurDto[]
*/
public array $dinosaursMultiMappedDeep = [];
}
8 changes: 8 additions & 0 deletions tests/fixtures/DinoRegionToDtoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function populate(object $from, object $to, array $context): object
}
$to->dinosaursMappedShallow = $shallowDinosaurDtos;

$to->dinosaursMultiMappedShallow = $this->microMapper->mapMultiple($from->dinosaurs, DinosaurDto::class, [
MicroMapperInterface::MAX_DEPTH => 0,
]);

$deepDinosaurDtos = [];
foreach ($from->dinosaurs as $dino) {
$deepDinosaurDtos[] = $this->microMapper->map($dino, DinosaurDto::class, [
Expand All @@ -51,6 +55,10 @@ public function populate(object $from, object $to, array $context): object
}
$to->dinosaursMappedDeep = $deepDinosaurDtos;

$to->dinosaursMultiMappedDeep = $this->microMapper->mapMultiple($from->dinosaurs, DinosaurDto::class, [
MicroMapperInterface::MAX_DEPTH => 1,
]);

return $to;
}
}

0 comments on commit 5afc279

Please sign in to comment.