diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/BaseDateTimeTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/BaseDateTimeTransformer.php index e4e8932ee9..9c1b92d741 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/BaseDateTimeTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/BaseDateTimeTransformer.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Form\Extension\Core\DataTransformer; use Symfony\Component\Form\DataTransformerInterface; +use Symfony\Component\Form\Exception\InvalidArgumentException; use Symfony\Component\Form\Exception\UnexpectedTypeException; abstract class BaseDateTimeTransformer implements DataTransformerInterface @@ -35,6 +36,7 @@ abstract class BaseDateTimeTransformer implements DataTransformerInterface * @param string $outputTimezone The name of the output timezone * * @throws UnexpectedTypeException if a timezone is not a string + * @throws InvalidArgumentException if a timezone is not valid */ public function __construct($inputTimezone = null, $outputTimezone = null) { @@ -48,5 +50,18 @@ abstract class BaseDateTimeTransformer implements DataTransformerInterface $this->inputTimezone = $inputTimezone ?: date_default_timezone_get(); $this->outputTimezone = $outputTimezone ?: date_default_timezone_get(); + + // Check if input and output timezones are valid + try { + new \DateTimeZone($this->inputTimezone); + } catch (\Exception $e) { + throw new InvalidArgumentException(sprintf('Input timezone is invalid: %s.', $this->inputTimezone), $e->getCode(), $e); + } + + try { + new \DateTimeZone($this->outputTimezone); + } catch (\Exception $e) { + throw new InvalidArgumentException(sprintf('Output timezone is invalid: %s.', $this->outputTimezone), $e->getCode(), $e); + } } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/BaseDateTimeTransformerTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/BaseDateTimeTransformerTest.php new file mode 100644 index 0000000000..8f2d16bb1b --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/BaseDateTimeTransformerTest.php @@ -0,0 +1,41 @@ + + * + * 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; + +class BaseDateTimeTransformerTest extends \PHPUnit_Framework_TestCase +{ + /** + * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException + * @expectedExceptionMessage this_timezone_does_not_exist + */ + public function testConstructFailsIfInputTimezoneIsInvalid() + { + $this->getMock( + 'Symfony\Component\Form\Extension\Core\DataTransformer\BaseDateTimeTransformer', + array(), + array('this_timezone_does_not_exist') + ); + } + + /** + * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException + * @expectedExceptionMessage that_timezone_does_not_exist + */ + public function testConstructFailsIfOutputTimezoneIsInvalid() + { + $this->getMock( + 'Symfony\Component\Form\Extension\Core\DataTransformer\BaseDateTimeTransformer', + array(), + array(null, 'that_timezone_does_not_exist') + ); + } +}