Skip to content

Commit

Permalink
Fixed toArray (#686)
Browse files Browse the repository at this point in the history
Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia committed Jul 15, 2024
1 parent 317eaa3 commit dafe7db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/validated-dto/src/SimpleDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,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/ValidatedDTO/Unit/SimpleDTOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact [email protected]
*/
use FriendsOfHyperf\Tests\ValidatedDTO\Datasets\AttributesDTO;
use FriendsOfHyperf\Tests\ValidatedDTO\Datasets\CallableCastingDTOInstance;
use FriendsOfHyperf\Tests\ValidatedDTO\Datasets\SimpleDTOInstance;
use FriendsOfHyperf\Tests\ValidatedDTO\Datasets\SimpleMapBeforeExportDTO;
Expand Down Expand Up @@ -284,3 +285,15 @@ public function __construct(protected string $subject_name)
->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 dafe7db

Please sign in to comment.