-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix mappings involving DateTimeInterface type (#26)
* create unit tests for all of the possible DateTime mappings * add tests for nullable DateTime fields * Expect TypeError instead of Throwable * Use createFromInterface for more reliable DateTime mapping * php-cs-fixer run * Added change log entry
- Loading branch information
Showing
16 changed files
with
235 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,28 +9,24 @@ | |
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Scalar\String_; | ||
|
||
/** | ||
* Transform DateTimeImmutable to DateTime. | ||
* Transform DateTimeInterface to DateTimeImmutable. | ||
* | ||
* @author Joel Wurtz <[email protected]> | ||
*/ | ||
final class DateTimeImmutableToMutableTransformer implements TransformerInterface | ||
final class DateTimeInterfaceToImmutableTransformer implements TransformerInterface | ||
{ | ||
public function transform(Expr $input, Expr $target, PropertyMapping $propertyMapping, UniqueVariableScope $uniqueVariableScope): array | ||
{ | ||
/* | ||
* In case of immutable source we clone the value by using format into a new mutable DateTime. | ||
* Handles all DateTime instance types using createFromInterface. | ||
* | ||
* \DateTime::createFromFormat(\DateTime::RFC3339, $input->format(\DateTime::RFC3339)); | ||
* \DateTimeImmutable::createFromInterface($input); | ||
*/ | ||
return [ | ||
new Expr\StaticCall(new Name\FullyQualified(\DateTime::class), 'createFromFormat', [ | ||
new Arg(new String_(\DateTime::RFC3339)), | ||
new Arg(new Expr\MethodCall($input, 'format', [ | ||
new Arg(new String_(\DateTime::RFC3339)), | ||
])), | ||
new Expr\StaticCall(new Name\FullyQualified(\DateTimeImmutable::class), 'createFromInterface', [ | ||
new Arg($input), | ||
]), | ||
[], | ||
]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,21 +11,21 @@ | |
use PhpParser\Node\Name; | ||
|
||
/** | ||
* Transform DateTime to DateTimeImmutable. | ||
* Transform DateTimeInterface to DateTime. | ||
* | ||
* @author Joel Wurtz <[email protected]> | ||
*/ | ||
final class DateTimeMutableToImmutableTransformer implements TransformerInterface | ||
final class DateTimeInterfaceToMutableTransformer implements TransformerInterface | ||
{ | ||
public function transform(Expr $input, Expr $target, PropertyMapping $propertyMapping, UniqueVariableScope $uniqueVariableScope): array | ||
{ | ||
/* | ||
* In case of mutable source we create the immutable value by using createFromMutable. | ||
* Handles all DateTime instance types using createFromInterface. | ||
* | ||
* \DateTimeImmutable::createFromMutable($input); | ||
* \DateTimeImmutable::createFromInterface($input); | ||
*/ | ||
return [ | ||
new Expr\StaticCall(new Name\FullyQualified(\DateTimeImmutable::class), 'createFromMutable', [ | ||
new Expr\StaticCall(new Name\FullyQualified(\DateTime::class), 'createFromInterface', [ | ||
new Arg($input), | ||
]), | ||
[], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AutoMapper\Tests\Fixtures; | ||
|
||
final class HasDateTime | ||
{ | ||
public \DateTime $dateTime; | ||
|
||
public static function create(): self | ||
{ | ||
$self = new self(); | ||
$self->dateTime = new \DateTime('2024-01-01 00:00:00'); | ||
|
||
return $self; | ||
} | ||
|
||
public function getString(): ?string | ||
{ | ||
return $this->dateTime->format(\DateTime::ATOM); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AutoMapper\Tests\Fixtures; | ||
|
||
final class HasDateTimeImmutable | ||
{ | ||
public \DateTimeImmutable $dateTime; | ||
|
||
public static function create(): self | ||
{ | ||
$self = new self(); | ||
$self->dateTime = new \DateTimeImmutable('2024-01-01 00:00:00'); | ||
|
||
return $self; | ||
} | ||
|
||
public function getString(): ?string | ||
{ | ||
return $this->dateTime->format(\DateTime::ATOM); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AutoMapper\Tests\Fixtures; | ||
|
||
final class HasDateTimeImmutableWithNullValue | ||
{ | ||
public ?\DateTimeImmutable $dateTime = null; | ||
|
||
public static function create(): self | ||
{ | ||
return new self(); | ||
} | ||
|
||
public function getString(): ?string | ||
{ | ||
return $this->dateTime?->format(\DateTime::ATOM); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/Fixtures/HasDateTimeInterfaceWithImmutableInstance.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AutoMapper\Tests\Fixtures; | ||
|
||
final class HasDateTimeInterfaceWithImmutableInstance | ||
{ | ||
public \DateTimeInterface $dateTime; | ||
|
||
public static function create(): self | ||
{ | ||
$self = new self(); | ||
$self->dateTime = new \DateTimeImmutable('2024-01-01 00:00:00'); | ||
|
||
return $self; | ||
} | ||
|
||
public function getString(): ?string | ||
{ | ||
return $this->dateTime->format(\DateTime::ATOM); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/Fixtures/HasDateTimeInterfaceWithMutableInstance.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AutoMapper\Tests\Fixtures; | ||
|
||
final class HasDateTimeInterfaceWithMutableInstance | ||
{ | ||
public \DateTimeInterface $dateTime; | ||
|
||
public static function create(): self | ||
{ | ||
$self = new self(); | ||
$self->dateTime = new \DateTime('2024-01-01 00:00:00'); | ||
|
||
return $self; | ||
} | ||
|
||
public function getString(): ?string | ||
{ | ||
return $this->dateTime->format(\DateTime::ATOM); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AutoMapper\Tests\Fixtures; | ||
|
||
final class HasDateTimeInterfaceWithNullValue | ||
{ | ||
public ?\DateTimeInterface $dateTime = null; | ||
|
||
public static function create(): self | ||
{ | ||
return new self(); | ||
} | ||
|
||
public function getString(): ?string | ||
{ | ||
return $this->dateTime?->format(\DateTime::ATOM); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AutoMapper\Tests\Fixtures; | ||
|
||
final class HasDateTimeWithNullValue | ||
{ | ||
public ?\DateTime $dateTime = null; | ||
|
||
public static function create(): self | ||
{ | ||
return new self(); | ||
} | ||
|
||
public function getString(): ?string | ||
{ | ||
return $this->dateTime?->format(\DateTime::ATOM); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ class User | |
public $addresses = []; | ||
|
||
/** | ||
* @var \DateTime | ||
* @var \DateTime|null | ||
*/ | ||
public $createdAt; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.