Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Form] DateTimeType now handles RFC 3339 dates as provided by HTML5
- Loading branch information
Showing
8 changed files
with
274 additions
and
25 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
80 changes: 80 additions & 0 deletions
80
src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToRfc3339Transformer.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,80 @@ | ||
<?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\Exception\UnexpectedTypeException; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
|
||
/** | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
*/ | ||
class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function transform($dateTime) | ||
{ | ||
if (null === $dateTime) { | ||
return ''; | ||
} | ||
|
||
if (!$dateTime instanceof \DateTime) { | ||
throw new UnexpectedTypeException($dateTime, '\DateTime'); | ||
} | ||
|
||
if ($this->inputTimezone !== $this->outputTimezone) { | ||
$dateTime = clone $dateTime; | ||
$dateTime->setTimezone(new \DateTimeZone($this->outputTimezone)); | ||
} | ||
|
||
return preg_replace('/\+00:00$/', 'Z', $dateTime->format('c')); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function reverseTransform($rfc3339) | ||
{ | ||
if (!is_string($rfc3339)) { | ||
throw new UnexpectedTypeException($rfc3339, 'string'); | ||
} | ||
|
||
if ('' === $rfc3339) { | ||
return null; | ||
} | ||
|
||
|
||
$dateTime = new \DateTime($rfc3339); | ||
|
||
if ($this->outputTimezone !== $this->inputTimezone) { | ||
try { | ||
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone)); | ||
} catch (\Exception $e) { | ||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', $rfc3339, $matches)) { | ||
if (!checkdate($matches[2], $matches[3], $matches[1])) { | ||
throw new TransformationFailedException(sprintf( | ||
'The date "%s-%s-%s" is not a valid date.', | ||
$matches[1], | ||
$matches[2], | ||
$matches[3] | ||
)); | ||
} | ||
} | ||
|
||
return $dateTime; | ||
} | ||
} |
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
122 changes: 122 additions & 0 deletions
122
.../Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToRfc3339TransformerTest.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,122 @@ | ||
<?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 Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToRfc3339Transformer; | ||
|
||
class DateTimeToRfc3339TransformerTest extends DateTimeTestCase | ||
{ | ||
protected $dateTime; | ||
protected $dateTimeWithoutSeconds; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->dateTime = new \DateTime('2010-02-03 04:05:06 UTC'); | ||
$this->dateTimeWithoutSeconds = new \DateTime('2010-02-03 04:05:00 UTC'); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
$this->dateTime = null; | ||
$this->dateTimeWithoutSeconds = null; | ||
} | ||
|
||
public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE) | ||
{ | ||
if ($expected instanceof \DateTime && $actual instanceof \DateTime) { | ||
$expected = $expected->format('c'); | ||
$actual = $actual->format('c'); | ||
} | ||
|
||
parent::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase); | ||
} | ||
|
||
public function allProvider() | ||
{ | ||
return array( | ||
array('UTC', 'UTC', '2010-02-03 04:05:06 UTC', '2010-02-03T04:05:06Z'), | ||
array('UTC', 'UTC', null, ''), | ||
array('America/New_York', 'Asia/Hong_Kong', '2010-02-03 04:05:06 America/New_York', '2010-02-03T17:05:06+08:00'), | ||
array('America/New_York', 'Asia/Hong_Kong', null, ''), | ||
array('UTC', 'Asia/Hong_Kong', '2010-02-03 04:05:06 UTC', '2010-02-03T12:05:06+08:00'), | ||
array('America/New_York', 'UTC', '2010-02-03 04:05:06 America/New_York', '2010-02-03T09:05:06Z'), | ||
); | ||
} | ||
|
||
public function transformProvider() | ||
{ | ||
return $this->allProvider(); | ||
} | ||
|
||
public function reverseTransformProvider() | ||
{ | ||
return array_merge($this->allProvider(), array( | ||
// format without seconds, as appears in some browsers | ||
array('UTC', 'UTC', '2010-02-03 04:05:00 UTC', '2010-02-03T04:05Z'), | ||
array('America/New_York', 'Asia/Hong_Kong', '2010-02-03 04:05:00 America/New_York', '2010-02-03T17:05+08:00'), | ||
)); | ||
} | ||
|
||
/** | ||
* @dataProvider transformProvider | ||
*/ | ||
public function testTransform($fromTz, $toTz, $from, $to) | ||
{ | ||
$transformer = new DateTimeToRfc3339Transformer($fromTz, $toTz); | ||
|
||
$this->assertSame($to, $transformer->transform(null !== $from ? new \DateTime($from) : null)); | ||
} | ||
|
||
/** | ||
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException | ||
*/ | ||
public function testTransformRequiresValidDateTime() | ||
{ | ||
$transformer = new DateTimeToRfc3339Transformer(); | ||
$transformer->transform('2010-01-01'); | ||
} | ||
|
||
/** | ||
* @dataProvider reverseTransformProvider | ||
*/ | ||
public function testReverseTransform($toTz, $fromTz, $to, $from) | ||
{ | ||
$transformer = new DateTimeToRfc3339Transformer($toTz, $fromTz); | ||
|
||
if (null !== $to) { | ||
$this->assertDateTimeEquals(new \DateTime($to), $transformer->reverseTransform($from)); | ||
} else { | ||
$this->assertSame($to, $transformer->reverseTransform($from)); | ||
} | ||
} | ||
|
||
/** | ||
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException | ||
*/ | ||
public function testReverseTransformRequiresString() | ||
{ | ||
$transformer = new DateTimeToRfc3339Transformer(); | ||
$transformer->reverseTransform(12345); | ||
} | ||
|
||
/** | ||
* @expectedException Symfony\Component\Form\Exception\TransformationFailedException | ||
*/ | ||
public function testReverseTransformWithNonExistingDate() | ||
{ | ||
$transformer = new DateTimeToRfc3339Transformer('UTC', 'UTC'); | ||
|
||
var_dump($transformer->reverseTransform('2010-04-31T04:05Z')); | ||
} | ||
} |
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