Skip to content
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

refactor: remove redundant $toClass from MicroMapper, allowing improvement of generic templating #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be array<string, mixed> ? I think the context is expected to be an associative array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

*
* @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