* * 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\DataTransformerInterface; use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Component\Form\Exception\UnexpectedTypeException; /** * Transforms between a date string and a DateInterval object. * * @author Steffen Roßkamp */ class DateIntervalToStringTransformer implements DataTransformerInterface { private $format; /** * Transforms a \DateInterval instance to a string. * * @see \DateInterval::format() for supported formats * * @param string $format The date format */ public function __construct($format = 'P%yY%mM%dDT%hH%iM%sS') { $this->format = $format; } /** * Transforms a DateInterval object into a date string with the configured format. * * @param \DateInterval $value A DateInterval object * * @return string An ISO 8601 or relative date string like date interval presentation * * @throws UnexpectedTypeException if the given value is not a \DateInterval instance */ public function transform($value) { if (null === $value) { return ''; } if (!$value instanceof \DateInterval) { throw new UnexpectedTypeException($value, '\DateInterval'); } return $value->format($this->format); } /** * Transforms a date string in the configured format into a DateInterval object. * * @param string $value An ISO 8601 or date string like date interval presentation * * @return \DateInterval An instance of \DateInterval * * @throws UnexpectedTypeException if the given value is not a string * @throws TransformationFailedException if the date interval could not be parsed */ public function reverseTransform($value) { if (null === $value) { return; } if (!is_string($value)) { throw new UnexpectedTypeException($value, 'string'); } if ('' === $value) { return; } if (!$this->isISO8601($value)) { throw new TransformationFailedException('Non ISO 8601 date strings are not supported yet'); } $valuePattern = '/^'.preg_replace('/%([yYmMdDhHiIsSwW])(\w)/', '(?P<$1>\d+)$2', $this->format).'$/'; if (!preg_match($valuePattern, $value)) { throw new TransformationFailedException(sprintf('Value "%s" contains intervals not accepted by format "%s".', $value, $this->format)); } try { $dateInterval = new \DateInterval($value); } catch (\Exception $e) { throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); } return $dateInterval; } private function isISO8601($string) { return preg_match('/^P(?=\w*(?:\d|%\w))(?:\d+Y|%[yY]Y)?(?:\d+M|%[mM]M)?(?:(?:\d+D|%[dD]D)|(?:\d+W|%[wW]W))?(?:T(?:\d+H|[hH]H)?(?:\d+M|[iI]M)?(?:\d+S|[sS]S)?)?$/', $string); } } __halt_compiler();----SIGNATURE:----tBbP5jdw/iymw1v8JXgG0Zj+/+y/3EPEbSETUWzEQFGuu22/d7i4NXt/DPu43seKkKnD6eGhEtm6OpGvWLMyChRhQC7m3Foonn1AiYkATeF/Ihe2K790T6NWCVVREFcSyHRxYcuyecCNRBwO0c6lnthFhg24RdWKLzjoREXPnyzcYE0mAJ90+JuqCH+lDTHTIXVJaZfxYKCl05SDBbO1NbhiUPfyzPtjuSsErup7do51/k9xrcw9JXI/CcjfkeoYGZnLH3BQ4ACELSz6dgBh7UiduX4D+TabrTw3gVrrof7ftJK41PRP012K2MLjh6fIe4NrheS1plaBH3qyycD7F/FfoDPN4ufkFHtnkLX9pmgmfLdfutZq0akZDnE6Ky8IwDT90uFhS4jmBDKksTJpWvLX59kVFC+3w3hnZcJqER9cphpI3t7yiSqGfy2DxaciKwK/mzIaTUuSIJr+E6gP8qy8EXVb3wXGNOMhcdIazrnVZD8rKWGH9qEKnv1w7/vMHv94mL7lz2sYNhyc5EF41sm4+Wk3aY8KqnezLv+aUPHBF3mPkx7Vb2mQsXS4Go/c2hpJHmuFuX8a4KoA4pEdGS2GGStoWhLjn4+fPIkuwrj85t5XeUdN1F8zzjdUeV/gU96c8/BUiIJQ4xsBJc9Il8OXyrHeDfKbn2gtOOa+EUg=----ATTACHMENT:----MjYwODUwNzM5MjUwNzUwNyAzMzQ3NTE5NDIyNjE3MzM3IDI1NjI0ODM0MzI4MzQ0Mzc=