4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Extension\Core\DataTransformer
;
14 use Symfony\Component\Form\Exception\TransformationFailedException
;
17 * @author Bernhard Schussek <bschussek@gmail.com>
19 class DateTimeToRfc3339Transformer
extends BaseDateTimeTransformer
24 public function transform($dateTime)
26 if (null === $dateTime) {
30 if (!$dateTime instanceof \DateTime
) {
31 throw new TransformationFailedException('Expected a \DateTime.');
34 if ($this->inputTimezone
!== $this->outputTimezone
) {
35 $dateTime = clone $dateTime;
36 $dateTime->setTimezone(new \
DateTimeZone($this->outputTimezone
));
39 return preg_replace('/\+00:00$/', 'Z', $dateTime->format('c'));
45 public function reverseTransform($rfc3339)
47 if (!is_string($rfc3339)) {
48 throw new TransformationFailedException('Expected a string.');
51 if ('' === $rfc3339) {
56 $dateTime = new \
DateTime($rfc3339);
57 } catch (\Exception
$e) {
58 throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
61 if ($this->outputTimezone
!== $this->inputTimezone
) {
63 $dateTime->setTimezone(new \
DateTimeZone($this->inputTimezone
));
64 } catch (\Exception
$e) {
65 throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
69 if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', $rfc3339, $matches)) {
70 if (!checkdate($matches[2], $matches[3], $matches[1])) {
71 throw new TransformationFailedException(sprintf(
72 'The date "%s-%s-%s" is not a valid date.',