Symfony\Component\Form\ValueTransformer\DateTimeToStringTransformer::reverseTransform PHP Method

reverseTransform() public method

Transforms a date string in the configured timezone into a DateTime object
public reverseTransform ( string $value, $originalValue ) : DateTime
$value string A value as produced by PHP's date() function
return DateTime A DateTime object
    public function reverseTransform($value, $originalValue)
    {
        if ($value === '') {
            return null;
        }

        $outputTimezone = $this->getOption('output_timezone');
        $inputTimezone = $this->getOption('input_timezone');

        try {
            $dateTime = new \DateTime("$value $outputTimezone");

            if ($inputTimezone != $outputTimezone) {
                $dateTime->setTimeZone(new \DateTimeZone($inputTimezone));
            }

            return $dateTime;
        } catch (\Exception $e) {
            throw new \InvalidArgumentException('Expected a valid date string. ' . $e->getMessage(), 0, $e);
        }
    }

Usage Example

 public function testReverseTransformExpectsValidDateString()
 {
     $reverseTransformer = new DateTimeToStringTransformer();
     $this->setExpectedException('\\InvalidArgumentException');
     $reverseTransformer->reverseTransform('2010-2010-2010', null);
 }