Skip to content

Commit 41f99d1

Browse files
committedJun 19, 2020
[bc] refactoring
1 parent 2832de7 commit 41f99d1

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-44
lines changed
 

‎composer.lock

+44-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/DataTransformer.php

+18-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace PTS\DataTransformer;
44

5-
use PTS\Hydrator\HydratorService;
5+
use PTS\Hydrator\Extractor;
6+
use PTS\Hydrator\ExtractorInterface;
7+
use PTS\Hydrator\Hydrator;
8+
use PTS\Hydrator\HydratorInterface;
69
use function get_class;
710
use function is_callable;
811

@@ -11,14 +14,16 @@ class DataTransformer implements DataTransformerInterface
1114
protected const FILTER_TYPE_POPULATE = 'populate';
1215
protected const FILTER_TYPE_EXTRACT = 'extract';
1316

14-
protected HydratorService $hydratorService;
17+
protected ExtractorInterface $extractor;
18+
protected HydratorInterface $hydrator;
1519
protected MapsManager $mapsManager;
1620

17-
public function __construct(HydratorService $hydratorService = null, MapsManager $mapsManager = null)
18-
{
19-
$this->hydratorService = $hydratorService ?? new HydratorService;
20-
$this->mapsManager = $mapsManager ?? new MapsManager;
21-
}
21+
public function __construct(ExtractorInterface $extractor = null, HydratorInterface $hydrator = null, MapsManager $mapsManager = null)
22+
{
23+
$this->extractor = $extractor ?? new Extractor;
24+
$this->hydrator = $hydrator ?? new Hydrator;
25+
$this->mapsManager = $mapsManager ?? new MapsManager;
26+
}
2227

2328
public function getMapsManager(): MapsManager
2429
{
@@ -30,18 +35,18 @@ public function toModel(string $class, array $dto, string $mapName = 'dto'): obj
3035
$map = $this->mapsManager->getMap($class, $mapName);
3136
$dto = $map['refs'] ? $this->resolveRefPopulate($dto, $map['refs']) : $dto;
3237
$dto = $map['pipe'] ? $this->applyPipes($dto, $map['pipe']) : $dto;
33-
return $this->hydratorService->hydrate($dto, $class, $map['rules']);
38+
return $this->hydrator->hydrate($dto, $class, $map['rules']);
3439
}
3540

36-
public function toModelsCollection(string $class, array $dtoCollection, string $mapName = 'dto'): array
41+
public function toModelsCollection(string $class, iterable $dtoCollection, string $mapName = 'dto'): array
3742
{
3843
$map = $this->mapsManager->getMap($class, $mapName);
3944

4045
$models = [];
4146
foreach ($dtoCollection as $dto) {
4247
$dto = $map['refs'] ? $this->resolveRefPopulate($dto, $map['refs']) : $dto;
4348
$dto = $map['pipe'] ? $this->applyPipes($dto, $map['pipe']) : $dto;
44-
$models[] = $this->hydratorService->hydrate($dto, $class, $map['rules']);
49+
$models[] = $this->hydrator->hydrate($dto, $class, $map['rules']);
4550
}
4651

4752
return $models;
@@ -52,12 +57,12 @@ public function fillModel(object $model, array $dto, string $mapName = 'dto'): o
5257
$map = $this->mapsManager->getMap(get_class($model), $mapName);
5358
$dto = $map['refs'] ? $this->resolveRefPopulate($dto, $map['refs']) : $dto;
5459
$dto = $map['pipe'] ? $this->applyPipes($dto, $map['pipe']) : $dto;
55-
$this->hydratorService->hydrateModel($dto, $model, $map['rules']);
60+
$this->hydrator->hydrateModel($dto, $model, $map['rules']);
5661

5762
return $model;
5863
}
5964

60-
public function toDtoCollection(array $models, string $mapName = 'dto', array $options = []): array
65+
public function toDtoCollection(iterable $models, string $mapName = 'dto', array $options = []): array
6166
{
6267
$collection = [];
6368
foreach ($models as $key => $model) {
@@ -77,7 +82,7 @@ public function toDTO(object $model, string $mapName = 'dto', array $options = [
7782
unset($map['pipe'][$name], $map['rules'][$name], $map['refs'][$name]);
7883
}
7984

80-
$dto = $this->hydratorService->extract($model, $map['rules']);
85+
$dto = $this->extractor->extract($model, $map['rules']);
8186
$dto = $map['pipe'] ? $this->applyPipes($dto, $map['pipe'], self::FILTER_TYPE_EXTRACT) : $dto;
8287
return $map['refs'] ? $this->resolveRefExtract($dto, $map['refs']) : $dto;
8388
}

‎src/DataTransformerInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
interface DataTransformerInterface
77
{
88
public function toDTO(object $model, string $mapType = 'dto', array $options = []): array;
9-
public function toDtoCollection(array $models, string $mapName = 'dto', array $options = []): array;
9+
public function toDtoCollection(iterable $models, string $mapName = 'dto', array $options = []): array;
1010

1111
public function toModel(string $model, array $dto, string $mapType = 'dto'): object;
12-
public function toModelsCollection(string $model, array $dtoCollection, string $mapType = 'dto'): array;
12+
public function toModelsCollection(string $model, iterable $dtoCollection, string $mapType = 'dto'): array;
1313

1414
public function fillModel(object $model, array $data, string $mapType = 'dto'): object;
1515
}

‎test/unit/RefTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ public function setUp(): void
2323
{
2424
$extractor = new Extractor(new ExtractClosure);
2525
$hydrator = new Hydrator(new HydrateClosure);
26-
$hydratorService = new HydratorService($extractor, $hydrator);
2726

2827
$mapsManager = new MapsManager;
2928
$mapsManager->setMapDir(UserModel::class, __DIR__ . '/data');
3029

31-
$this->dataTransformer = new DataTransformer($hydratorService, $mapsManager);
30+
$this->dataTransformer = new DataTransformer($extractor, $hydrator, $mapsManager);
3231

3332
$this->faker = Factory::create();
3433
}

0 commit comments

Comments
 (0)
Please sign in to comment.