Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #25582 [Form] Support \DateTimeImmutable (vudaltsov)
This PR was squashed before being merged into the 4.1-dev branch (closes #25582). Discussion ---------- [Form] Support \DateTimeImmutable | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #9508 | License | MIT | Doc PR | symfony/symfony-docs#8920 This PR implements `input=datetime_immutable`. Replaces #25273. Commits ------- 034f8b2 [Form] Support \DateTimeImmutable
- Loading branch information
Showing
9 changed files
with
255 additions
and
3 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
67 changes: 67 additions & 0 deletions
67
.../Component/Form/Extension/Core/DataTransformer/DateTimeImmutableToDateTimeTransformer.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,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Extension\Core\DataTransformer; | ||
|
||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
|
||
/** | ||
* Transforms between a DateTimeImmutable object and a DateTime object. | ||
* | ||
* @author Valentin Udaltsov <udaltsov.valentin@gmail.com> | ||
*/ | ||
final class DateTimeImmutableToDateTimeTransformer implements DataTransformerInterface | ||
{ | ||
/** | ||
* Transforms a DateTimeImmutable into a DateTime object. | ||
* | ||
* @param \DateTimeImmutable|null $value A DateTimeImmutable object | ||
* | ||
* @return \DateTime|null A \DateTime object | ||
* | ||
* @throws TransformationFailedException If the given value is not a \DateTimeImmutable | ||
*/ | ||
public function transform($value) | ||
{ | ||
if (null === $value) { | ||
return null; | ||
} | ||
|
||
if (!$value instanceof \DateTimeImmutable) { | ||
throw new TransformationFailedException('Expected a \DateTimeImmutable.'); | ||
} | ||
|
||
return \DateTime::createFromFormat(\DateTime::RFC3339, $value->format(\DateTime::RFC3339)); | ||
} | ||
|
||
/** | ||
* Transforms a DateTime object into a DateTimeImmutable object. | ||
* | ||
* @param \DateTime|null $value A DateTime object | ||
* | ||
* @return \DateTimeImmutable|null A DateTimeImmutable object | ||
* | ||
* @throws TransformationFailedException If the given value is not a \DateTime | ||
*/ | ||
public function reverseTransform($value) | ||
{ | ||
if (null === $value) { | ||
return null; | ||
} | ||
|
||
if (!$value instanceof \DateTime) { | ||
throw new TransformationFailedException('Expected a \DateTime.'); | ||
} | ||
|
||
return \DateTimeImmutable::createFromMutable($value); | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
.../Form/Tests/Extension/Core/DataTransformer/DateTimeImmutableToDateTimeTransformerTest.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,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Tests\Extension\Core\DataTransformer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeImmutableToDateTimeTransformer; | ||
|
||
class DateTimeImmutableToDateTimeTransformerTest extends TestCase | ||
{ | ||
public function testTransform() | ||
{ | ||
$transformer = new DateTimeImmutableToDateTimeTransformer(); | ||
|
||
$input = new \DateTimeImmutable('2010-02-03 04:05:06 UTC'); | ||
$expectedOutput = new \DateTime('2010-02-03 04:05:06 UTC'); | ||
$actualOutput = $transformer->transform($input); | ||
|
||
$this->assertInstanceOf(\DateTime::class, $actualOutput); | ||
$this->assertEquals($expectedOutput, $actualOutput); | ||
} | ||
|
||
public function testTransformEmpty() | ||
{ | ||
$transformer = new DateTimeImmutableToDateTimeTransformer(); | ||
|
||
$this->assertNull($transformer->transform(null)); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
* @expectedExceptionMessage Expected a \DateTimeImmutable. | ||
*/ | ||
public function testTransformFail() | ||
{ | ||
$transformer = new DateTimeImmutableToDateTimeTransformer(); | ||
$transformer->transform(new \DateTime()); | ||
} | ||
|
||
public function testReverseTransform() | ||
{ | ||
$transformer = new DateTimeImmutableToDateTimeTransformer(); | ||
|
||
$input = new \DateTime('2010-02-03 04:05:06 UTC'); | ||
$expectedOutput = new \DateTimeImmutable('2010-02-03 04:05:06 UTC'); | ||
$actualOutput = $transformer->reverseTransform($input); | ||
|
||
$this->assertInstanceOf(\DateTimeImmutable::class, $actualOutput); | ||
$this->assertEquals($expectedOutput, $actualOutput); | ||
} | ||
|
||
public function testReverseTransformEmpty() | ||
{ | ||
$transformer = new DateTimeImmutableToDateTimeTransformer(); | ||
|
||
$this->assertNull($transformer->reverseTransform(null)); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
* @expectedExceptionMessage Expected a \DateTime. | ||
*/ | ||
public function testReverseTransformFail() | ||
{ | ||
$transformer = new DateTimeImmutableToDateTimeTransformer(); | ||
$transformer->reverseTransform(new \DateTimeImmutable()); | ||
} | ||
} |
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