Skip to content

Commit 6ee4ee8

Browse files
committed
refactor: remove redundant $toClass from MicroMapper, allowing improvement of generic templating
1 parent 81190a2 commit 6ee4ee8

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

src/MapperInterface.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
* Also add #[AsMapper(from: Foo:class, to: Bar:class)] to each mapper class.
1616
*
1717
* @author Ryan Weaver <[email protected]>
18+
*
19+
* @template TFrom of object
20+
* @template TTo of object
1821
*/
1922
interface MapperInterface
2023
{
@@ -24,22 +27,21 @@ interface MapperInterface
2427
* This method should load (e.g. from the database) or instantiate the "to object".
2528
* Avoid populating any properties except for an identifier.
2629
*
27-
* @template TTo of object
28-
*
29-
* @param class-string<TTo> $toClass
30+
* @param TFrom $from
31+
* @param array<mixed> $context
3032
*
3133
* @return TTo
3234
*/
33-
public function load(object $from, string $toClass, array $context): object;
35+
public function load(object $from, array $context): object;
3436

3537
/**
3638
* Populate the data onto the "to object" from the "from object".
3739
*
3840
* Receives the "to object" returned from load().
3941
*
40-
* @template TTo of object
41-
*
42-
* @param TTo $to
42+
* @param TFrom $from
43+
* @param TTo $to
44+
* @param array<mixed> $context
4345
*
4446
* @return TTo
4547
*/

src/MicroMapper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function map(object $from, string $toClass, array $context = []): object
6464
continue;
6565
}
6666

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

6969
// avoid fully populated objects if max depth is reached
7070
if (null === $this->maxDepth || $this->currentDepth < $this->maxDepth) {

tests/PHPStan/data/mapper.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
use Symfonycasts\MicroMapper\MapperInterface;
12+
use Symfonycasts\MicroMapper\Tests\fixtures\Dinosaur;
1313
use Symfonycasts\MicroMapper\Tests\fixtures\DinosaurDto;
1414

15+
use Symfonycasts\MicroMapper\Tests\fixtures\DinosaurToDtoMapper;
1516
use function PHPStan\Testing\assertType;
1617

17-
function doMapperInterfaceLoad(MapperInterface $microMapper): void
18+
function doMapperInterfaceLoad(DinosaurToDtoMapper $dinosaurMapper, Dinosaur $dinosaur): void
1819
{
19-
assertType(DinosaurDto::class, $microMapper->load(new \stdClass(), DinosaurDto::class));
20+
assertType(DinosaurDto::class, $dinosaurMapper->load( $dinosaur, []));
2021
}
2122

22-
function doMapperInterfacePopulate(MapperInterface $microMapper, DinosaurDto $dto): void
23+
function doMapperInterfacePopulate(DinosaurToDtoMapper $dinosaurMapper, Dinosaur $dinosaur, DinosaurDto $dto): void
2324
{
24-
assertType(DinosaurDto::class, $microMapper->populate(new \stdClass(), $dto));
25+
assertType(DinosaurDto::class, $dinosaurMapper->populate($dinosaur, $dto, []));
2526
}

tests/fixtures/DinoRegionToDtoMapper.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@
1414
use Symfonycasts\MicroMapper\MicroMapperInterface;
1515

1616
#[AsMapper(from: DinoRegion::class, to: DinoRegionDto::class)]
17+
/**
18+
* @implements MapperInterface<DinoRegion, DinoRegionDto>
19+
*/
1720
class DinoRegionToDtoMapper implements MapperInterface
1821
{
1922
public function __construct(private MicroMapperInterface $microMapper)
2023
{
2124
}
2225

23-
public function load(object $from, string $toClass, array $context): object
26+
public function load(object $from, array $context): object
2427
{
25-
$dto = new $toClass();
28+
$dto = new DinoRegionDto();
2629
$dto->id = $from->id;
2730

2831
return $dto;
2932
}
3033

3134
public function populate(object $from, object $to, array $context): object
3235
{
33-
\assert($from instanceof DinoRegion);
34-
\assert($to instanceof DinoRegionDto);
35-
3636
$to->name = $from->name;
3737
$to->climate = $from->climate;
3838
$shallowDinosaurDtos = [];

tests/fixtures/DinosaurToDtoMapper.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,26 @@
1212
use Symfonycasts\MicroMapper\AsMapper;
1313
use Symfonycasts\MicroMapper\MapperInterface;
1414
use Symfonycasts\MicroMapper\MicroMapperInterface;
15-
15+
/**
16+
* @implements MapperInterface<Dinosaur, DinosaurDto>
17+
*/
1618
#[AsMapper(from: Dinosaur::class, to: DinosaurDto::class)]
1719
class DinosaurToDtoMapper implements MapperInterface
1820
{
1921
public function __construct(private MicroMapperInterface $microMapper)
2022
{
2123
}
2224

23-
public function load(object $from, string $toClass, array $context): object
25+
public function load(object $from, array $context): object
2426
{
25-
$dto = new $toClass();
27+
$dto = new DinosaurDto();
2628
$dto->id = $from->id;
2729

2830
return $dto;
2931
}
3032

3133
public function populate(object $from, object $to, array $context): object
3234
{
33-
\assert($from instanceof Dinosaur);
34-
\assert($to instanceof DinosaurDto);
35-
3635
$to->genus = $from->genus;
3736
$to->species = $from->species;
3837
$to->region = $this->microMapper->map($from->region, DinoRegionDto::class, [

0 commit comments

Comments
 (0)