Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SimpleDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ private function transformCollectionToArray(Collection $collection): array
private function transformDTOToArray(SimpleDTO $dto): array
{
$result = [];
foreach ($dto->validatedData as $key => $value) {
foreach ($dto->buildDataForExport() as $key => $value) {
$result[$key] = $this->isArrayable($value)
? $this->formatArrayableValue($value)
: $value;
Expand Down
29 changes: 29 additions & 0 deletions tests/Datasets/SimpleInnerDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace WendellAdriel\ValidatedDTO\Tests\Datasets;

use WendellAdriel\ValidatedDTO\Casting\IntegerCast;
use WendellAdriel\ValidatedDTO\Casting\StringCast;
use WendellAdriel\ValidatedDTO\SimpleDTO;

class SimpleInnerDTO extends SimpleDTO
{
public string $name;

public int $number;

protected function defaults(): array
{
return [];
}

protected function casts(): array
{
return [
'name' => new StringCast(),
'number' => new IntegerCast(),
];
}
}
31 changes: 31 additions & 0 deletions tests/Datasets/SimpleOuterDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace WendellAdriel\ValidatedDTO\Tests\Datasets;

use WendellAdriel\ValidatedDTO\Casting\DTOCast;
use WendellAdriel\ValidatedDTO\Casting\IntegerCast;
use WendellAdriel\ValidatedDTO\Casting\StringCast;
use WendellAdriel\ValidatedDTO\SimpleDTO;

class SimpleOuterDTO extends SimpleDTO
{
public string $name;

public SimpleInnerDTO $inner;

protected function defaults(): array
{
return [];
}

protected function casts(): array
{
return [
'name' => new StringCast(),
'number' => new IntegerCast(),
'inner' => new DTOCast(SimpleInnerDTO::class),
];
}
}
38 changes: 38 additions & 0 deletions tests/Unit/SimpleDTOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use WendellAdriel\ValidatedDTO\Tests\Datasets\SimpleMappedNameDTO;
use WendellAdriel\ValidatedDTO\Tests\Datasets\SimpleNameDTO;
use WendellAdriel\ValidatedDTO\Tests\Datasets\SimpleNullableDTO;
use WendellAdriel\ValidatedDTO\Tests\Datasets\SimpleOuterDTO;
use WendellAdriel\ValidatedDTO\Tests\Datasets\SimpleUserDTO;
use WendellAdriel\ValidatedDTO\Tests\Datasets\User;

Expand Down Expand Up @@ -314,3 +315,40 @@ public function __invoke() {}
expect($dto->age)->toBe(20)
->and($dto->toArray())->toBe(['age' => 20, 'doc' => 'test']);
});

it('checks that update for nested DTO property reflects while converting DTO to array', function () {
$dto = SimpleOuterDTO::fromArray([
'name' => 'name',
'inner' => [
'name' => 'inner name',
'number' => 2,
],
]);

$dto->inner->name = 'updated inner name';

expect($dto->inner->name)->toBe('updated inner name')
->and($dto->toArray())->toBe([
'name' => 'name',
'inner' => [
'name' => 'updated inner name',
'number' => 2,
],
]);
});

it('checks that update for nested DTO property reflects while converting DTO to json', function () {
$array = [
'name' => 'name',
'inner' => [
'name' => 'inner name',
'number' => 2,
],
];
$dto = SimpleOuterDTO::fromArray($array);

$dto->inner->name = 'updated inner name';

expect($dto->inner->name)->toBe('updated inner name')
->and($dto->toJson())->toBe(json_encode(['name' => 'name', 'inner' => ['name' => 'updated inner name', 'number' => 2]]));
});