Skip to content

Commit edd8618

Browse files
committed
add method toModelsCollection and extend interface
1 parent 2fc2b5c commit edd8618

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/PTS/DataTransformer/DataTransformer.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,22 @@ public function toModel(array $dto, string $class, string $mapName = 'dto')
2424
return $this->hydratorService->hydrate($dto, $class, $rules);
2525
}
2626

27+
public function toModelsCollection(array $dtoCollection, string $class, string $mapName = 'dto'): array
28+
{
29+
$rules = $this->mapsManager->getMap($class, $mapName);
30+
31+
$models = [];
32+
foreach ($dtoCollection as $dto) {
33+
$dto = $this->resolveRefHydrate($dto, $rules);
34+
$models[] = $this->hydratorService->hydrate($dto, $class, $rules);
35+
}
36+
37+
return $models;
38+
}
39+
2740
public function fillModel(array $dto, $model, string $mapName = 'dto'): void
2841
{
29-
$rules = $this->mapsManager->getMap(get_class($model), $mapName);
42+
$rules = $this->mapsManager->getMap(\get_class($model), $mapName);
3043
$dto = $this->resolveRefHydrate($dto, $rules);
3144
$this->hydratorService->hydrateModel($dto, $model, $rules);
3245
}

src/PTS/DataTransformer/DataTransformerInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
interface DataTransformerInterface
77
{
88
public function toDTO($model, string $mapType = 'dto', array $excludeFields = []): array;
9+
public function toDtoCollection(array $models, string $mapName = 'dto', array $excludeFields = []): array;
910

10-
public function toModel(array $data, string $model, string $mapType = 'dto');
11+
public function toModel(array $dto, string $model, string $mapType = 'dto');
12+
public function toModelsCollection(array $dtoCollection, string $model, string $mapType = 'dto'): array;
1113

1214
public function fillModel(array $data, $model, string $mapType = 'dto');
1315
}

test/unit/DataTransformerTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function testToDTOWithExcludeFields(): void
119119
], $dto);
120120
}
121121

122-
public function testToDtoCollection()
122+
public function testToDtoCollection(): void
123123
{
124124
$model1 = $this->createUser();
125125
$model2 = $this->createUser();
@@ -147,4 +147,36 @@ public function testToDtoCollection()
147147
], $dtoCollection[1]);
148148
}
149149

150+
public function testToModelsCollection(): void
151+
{
152+
$dtoCollection = [
153+
[
154+
'id' => 1,
155+
'creAt' => new \DateTime,
156+
'name' => 'Alex',
157+
'active' => true,
158+
],
159+
[
160+
'id' => 2,
161+
'creAt' => new \DateTime,
162+
'name' => 'Bob',
163+
'active' => false,
164+
]
165+
];
166+
167+
/** @var UserModel[] $models */
168+
$models = $this->dataTransformer->toModelsCollection($dtoCollection, UserModel::class);
169+
$this->assertCount(2, $models);
170+
171+
$this->assertInstanceOf(UserModel::class, $models[0]);
172+
$this->assertEquals(1, $models[0]->getId());
173+
$this->assertEquals('Alex', $models[0]->getName());
174+
$this->assertEquals(true, $models[0]->isActive());
175+
176+
$this->assertInstanceOf(UserModel::class, $models[1]);
177+
$this->assertEquals(2, $models[1]->getId());
178+
$this->assertEquals('Bob', $models[1]->getName());
179+
$this->assertEquals(false, $models[1]->isActive());
180+
}
181+
150182
}

0 commit comments

Comments
 (0)