-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDinoRegionToDtoMapper.php
More file actions
62 lines (51 loc) · 1.99 KB
/
Copy pathDinoRegionToDtoMapper.php
File metadata and controls
62 lines (51 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfonycasts\MicroMapper\Tests\fixtures;
use Symfonycasts\MicroMapper\AsMapper;
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, array $context): object
{
$dto = new DinoRegionDto();
$dto->id = $from->id;
return $dto;
}
public function populate(object $from, object $to, array $context): object
{
$to->name = $from->name;
$to->climate = $from->climate;
$shallowDinosaurDtos = [];
foreach ($from->dinosaurs as $dino) {
$shallowDinosaurDtos[] = $this->microMapper->map($dino, DinosaurDto::class, [
MicroMapperInterface::MAX_DEPTH => 0,
]);
}
$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, [
MicroMapperInterface::MAX_DEPTH => 1,
]);
}
$to->dinosaursMappedDeep = $deepDinosaurDtos;
$to->dinosaursMultiMappedDeep = $this->microMapper->mapMultiple($from->dinosaurs, DinosaurDto::class, [
MicroMapperInterface::MAX_DEPTH => 1,
]);
return $to;
}
}