Skip to content

Commit

Permalink
refactor: remove redundant $toClass from MicroMapper, allowing improv…
Browse files Browse the repository at this point in the history
…ement of generic templating
  • Loading branch information
bendavies committed Oct 20, 2023
1 parent 81190a2 commit 083ed66
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
16 changes: 9 additions & 7 deletions src/MapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* Also add #[AsMapper(from: Foo:class, to: Bar:class)] to each mapper class.
*
* @author Ryan Weaver <[email protected]>
*
* @template TFrom of object
* @template TTo of object
*/
interface MapperInterface
{
Expand All @@ -24,22 +27,21 @@ interface MapperInterface
* This method should load (e.g. from the database) or instantiate the "to object".
* Avoid populating any properties except for an identifier.
*
* @template TTo of object
*
* @param class-string<TTo> $toClass
* @param TFrom $from
* @param array<mixed> $context
*
* @return TTo
*/
public function load(object $from, string $toClass, array $context): object;
public function load(object $from, array $context): object;

/**
* Populate the data onto the "to object" from the "from object".
*
* Receives the "to object" returned from load().
*
* @template TTo of object
*
* @param TTo $to
* @param TFrom $from
* @param TTo $to
* @param array<mixed> $context
*
* @return TTo
*/
Expand Down
2 changes: 1 addition & 1 deletion src/MicroMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function map(object $from, string $toClass, array $context = []): object
continue;
}

$toObject = $mapperConfig->getMapper()->load($from, $toClass, $context);
$toObject = $mapperConfig->getMapper()->load($from, $context);

// avoid fully populated objects if max depth is reached
if (null === $this->maxDepth || $this->currentDepth < $this->maxDepth) {
Expand Down
11 changes: 6 additions & 5 deletions tests/PHPStan/data/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
* file that was distributed with this source code.
*/

use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\Tests\fixtures\Dinosaur;
use Symfonycasts\MicroMapper\Tests\fixtures\DinosaurDto;

use Symfonycasts\MicroMapper\Tests\fixtures\DinosaurToDtoMapper;
use function PHPStan\Testing\assertType;

function doMapperInterfaceLoad(MapperInterface $microMapper): void
function doMapperInterfaceLoad(DinosaurToDtoMapper $dinosaurMapper, Dinosaur $dinosaur): void
{
assertType(DinosaurDto::class, $microMapper->load(new \stdClass(), DinosaurDto::class));
assertType(DinosaurDto::class, $dinosaurMapper->load( $dinosaur, []));
}

function doMapperInterfacePopulate(MapperInterface $microMapper, DinosaurDto $dto): void
function doMapperInterfacePopulate(DinosaurToDtoMapper $dinosaurMapper, Dinosaur $dinosaur, DinosaurDto $dto): void
{
assertType(DinosaurDto::class, $microMapper->populate(new \stdClass(), $dto));
assertType(DinosaurDto::class, $dinosaurMapper->populate($dinosaur, $dto, []));
}
8 changes: 3 additions & 5 deletions tests/fixtures/DinoRegionToDtoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,24 @@
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

/** @implements MapperInterface<DinoRegion, DinoRegionDto> */
#[AsMapper(from: DinoRegion::class, to: DinoRegionDto::class)]
class DinoRegionToDtoMapper implements MapperInterface
{
public function __construct(private MicroMapperInterface $microMapper)
{
}

public function load(object $from, string $toClass, array $context): object
public function load(object $from, array $context): object
{
$dto = new $toClass();
$dto = new DinoRegionDto();
$dto->id = $from->id;

return $dto;
}

public function populate(object $from, object $to, array $context): object
{
\assert($from instanceof DinoRegion);
\assert($to instanceof DinoRegionDto);

$to->name = $from->name;
$to->climate = $from->climate;
$shallowDinosaurDtos = [];
Expand Down
8 changes: 3 additions & 5 deletions tests/fixtures/DinosaurToDtoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,24 @@
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

/** @implements MapperInterface<Dinosaur, DinosaurDto> */
#[AsMapper(from: Dinosaur::class, to: DinosaurDto::class)]
class DinosaurToDtoMapper implements MapperInterface
{
public function __construct(private MicroMapperInterface $microMapper)
{
}

public function load(object $from, string $toClass, array $context): object
public function load(object $from, array $context): object
{
$dto = new $toClass();
$dto = new DinosaurDto();
$dto->id = $from->id;

return $dto;
}

public function populate(object $from, object $to, array $context): object
{
\assert($from instanceof Dinosaur);
\assert($to instanceof DinosaurDto);

$to->genus = $from->genus;
$to->species = $from->species;
$to->region = $this->microMapper->map($from->region, DinoRegionDto::class, [
Expand Down

0 comments on commit 083ed66

Please sign in to comment.