Source of file DateTimeParamConverter.php
Size: 1,713 Bytes - Last Modified: 2013-07-09T11:29:53+02:00
/home/theseer/Downloads/Symfony/vendor/sensio/framework-extra-bundle/Sensio/Bundle/FrameworkExtraBundle/Request/ParamConverter/DateTimeParamConverter.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 | <?php /* * This file is part of the Symfony framework. * * (c) Fabien Potencier <fabien@symfony.com> * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use DateTime; /** * Convert DateTime instances from request attribute variable. * * @author Benjamin Eberlei <kontakt@beberlei.de> */ class DateTimeParamConverter implements ParamConverterInterface { /** * @{inheritdoc} * * @throws NotFoundHttpException When invalid date given */ public function apply(Request $request, ConfigurationInterface $configuration) { $param = $configuration->getName(); if (!$request->attributes->has($param)) { return false; } $options = $configuration->getOptions(); $value = $request->attributes->get($param); $date = isset($options['format']) ? DateTime::createFromFormat($options['format'], $value) : new DateTime($value); if (!$date) { throw new NotFoundHttpException('Invalid date given.'); } $request->attributes->set($param, $date); return true; } /** * @{inheritdoc} */ public function supports(ConfigurationInterface $configuration) { if (null === $configuration->getClass()) { return false; } return "DateTime" === $configuration->getClass(); } } |