Skip to content

Commit 07d95d7

Browse files
Merge pull request #94 from WendellAdriel/hotfix/transform-issue
Fix issue of missing properties when transforming DTO
2 parents 17bd33c + ffa3e7d commit 07d95d7

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/SimpleDTO.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,14 @@ protected function buildDataForExport(): array
312312
...$this->dtoMapTransform,
313313
];
314314

315-
return $this->mapDTOData($mapping, $this->validatedData);
315+
$data = $this->validatedData;
316+
foreach ($this->getAcceptedProperties() as $property) {
317+
if (! array_key_exists($property, $data) && isset($this->{$property})) {
318+
$data[$property] = $this->{$property};
319+
}
320+
}
321+
322+
return $this->mapDTOData($mapping, $data);
316323
}
317324

318325
protected function buildDataForValidation(array $data): array

tests/Unit/ValidatedDTOTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ public function __invoke() {}
215215

216216
expect($validatedDTO)->toArray()
217217
->toBe($dataStructure);
218+
219+
$validatedDTO->age = 20;
220+
expect($validatedDTO)->toArray()
221+
->toBe([...$dataStructure, 'age' => 20]);
218222
});
219223

220224
it('validates that the ValidatedDTO can be converted into a JSON string', function () {
@@ -223,6 +227,10 @@ public function __invoke() {}
223227

224228
expect($validatedDTO)->toJson()
225229
->toBe(json_encode($dataStructure));
230+
231+
$validatedDTO->age = 20;
232+
expect($validatedDTO)->toJson()
233+
->toBe(json_encode([...$dataStructure, 'age' => 20]));
226234
});
227235

228236
it('validates that the ValidatedDTO can be converted into a pretty JSON string', function () {
@@ -231,6 +239,10 @@ public function __invoke() {}
231239

232240
expect($validatedDTO)->toPrettyJson()
233241
->toBe(json_encode($dataStructure, JSON_PRETTY_PRINT));
242+
243+
$validatedDTO->age = 20;
244+
expect($validatedDTO)->toPrettyJson()
245+
->toBe(json_encode([...$dataStructure, 'age' => 20], JSON_PRETTY_PRINT));
234246
});
235247

236248
it('validates that the ValidatedDTO with nested data can be converted into an array', function () {
@@ -361,15 +373,22 @@ public function __invoke() {}
361373

362374
$model = new class() extends Model
363375
{
364-
protected $fillable = ['name'];
376+
protected $fillable = ['name', 'age'];
365377
};
366378

367-
$model_instance = $validatedDTO->toModel($model::class);
379+
$modelInstance = $validatedDTO->toModel($model::class);
368380

369-
expect($model_instance)
381+
expect($modelInstance)
370382
->toBeInstanceOf(Model::class)
371383
->toArray()
372384
->toBe(['name' => $this->subject_name]);
385+
386+
$validatedDTO->age = 20;
387+
$modelInstance = $validatedDTO->toModel($model::class);
388+
expect($modelInstance)
389+
->toBeInstanceOf(Model::class)
390+
->toArray()
391+
->toBe(['name' => $this->subject_name, 'age' => 20]);
373392
});
374393

375394
it('maps data before validation', function () {

0 commit comments

Comments
 (0)