Skip to content

Commit

Permalink
Loss of data for datetime fields if using a getter/setter (#44)
Browse files Browse the repository at this point in the history
* fix(datetime): name of datetime variable was unchanged from modelPath, leading to loss of data
  • Loading branch information
brutal-factories authored Mar 4, 2024
1 parent 28e6f4a commit 987b2b1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Template/Deserialization.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function renderAssignDateTimeFromFormat(bool $immutable, string $modelPat
'/([^a-zA-Z]+|\d+)([a-zA-Z])/',
static fn ($match): string => (ctype_digit($match[1]) ? $match[1] : null).mb_strtoupper($match[2]),
$modelPath
);
).'Date';

return $this->render($template, [
'modelPath' => $modelPath,
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,21 @@ class Model
* @var \DateTimeImmutable
*/
public $dateImmutable;

/**
* @Serializer\Type("DateTimeImmutable")
*
* @Serializer\Accessor(getter="getDateImmutablePrivate", setter="setDateImmutablePrivate")
*/
private $dateImmutablePrivate;

public function getDateImmutablePrivate()
{
return $this->dateImmutablePrivate;
}

public function setDateImmutablePrivate($dateImmutablePrivate): void
{
$this->dateImmutablePrivate = $dateImmutablePrivate;
}
}
2 changes: 2 additions & 0 deletions tests/Unit/DeserializerGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function testNested(): void
'date_with_multiple_deserialization_formats' => '05/16/2019',
'date_with_timezone' => '04/08/2018', // Defined timezone offset is +6 hours, so bringing it back to UTC removes a day
'date_immutable' => '2016-06-01T00:00:00+02:00',
'date_immutable_private' => '2016-06-01T00:00:00+02:00',
];

/** @var Model $model */
Expand All @@ -78,6 +79,7 @@ public function testNested(): void
self::assertSame('2018-08-03', $model->dateWithTimezone->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d'));
self::assertInstanceOf(\DateTimeImmutable::class, $model->dateImmutable);
self::assertSame('2016-06-01', $model->dateImmutable->format('Y-m-d'));
self::assertSame('2016-06-01', $model->getDateImmutablePrivate()?->format('Y-m-d'));
}

public function testLists(): void
Expand Down

0 comments on commit 987b2b1

Please sign in to comment.