Skip to content

Commit

Permalink
Merge pull request #80 from WendellAdriel/hotfix/to-array
Browse files Browse the repository at this point in the history
Fix issue with toArray
  • Loading branch information
WendellAdriel committed Jul 15, 2024
2 parents 7a33667 + 0fb4ac3 commit 93721ce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/SimpleDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ private function mapDTOData(array $mapping, array $data): array
? $mapping[$key]
: $key;

if (isset($this->{$key}) && $value !== $this->{$key}) {
$value = $this->{$key};
}

$mappedData[$property] = $this->isArrayable($value)
? $this->formatArrayableValue($value)
: $value;
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/SimpleDTOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Http\Request;
use WendellAdriel\ValidatedDTO\Exceptions\InvalidJsonException;
use WendellAdriel\ValidatedDTO\SimpleDTO;
use WendellAdriel\ValidatedDTO\Tests\Datasets\AttributesDTO;
use WendellAdriel\ValidatedDTO\Tests\Datasets\CallableCastingDTOInstance;
use WendellAdriel\ValidatedDTO\Tests\Datasets\SimpleDTOInstance;
use WendellAdriel\ValidatedDTO\Tests\Datasets\SimpleMapBeforeExportDTO;
Expand Down Expand Up @@ -301,3 +302,15 @@ public function __invoke() {}
->and($dto->age)
->toBe(30);
});

it('checks that update for property reflects while converting DTO', function () {
$dto = AttributesDTO::fromArray([
'age' => 18,
'doc' => 'test',
]);

$dto->age = 20;

expect($dto->age)->toBe(20)
->and($dto->toArray())->toBe(['age' => 20, 'doc' => 'test']);
});

0 comments on commit 93721ce

Please sign in to comment.