* * 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:----kfkxAEiLdolejdYLe/ljtOrRObfulGVp0VyK+EUK5pWnGcqseBSNx2wmU7m6H5WD5fHFdfbVmAeES6O0r6LmeN1XvIOyZgmhL+4v+iHAWZ6R6CEsfA8bb64MoEVK2w7t+neY4rBtBPI9TUvCIg8Y3IpIUmqypd1NgF1iyfqlXX2R27pf+HlV56wfrx10g4hq28kDUxWaGk2b8Qdx9fyF0yqUdxAy7EcTc7jPysa1VHYwDahMyxrpiXbDVjMsBBvAK5es8tm0x01pN3r86lzDsA6QcrktOv9LHuE6I4VZBpJ67UxmLbx+uNE07RMk6V3LvTPzOnE6Fya4obQodntElFD2uueKvWiijAqSAJWtxFGPHc2MpBeHYVAbIfJNNmfEHgVduZWhG7OLSJZhoMZSFRzdzjh4d1oqyXY7Ms0gA/+Her8ZxGxGRPj5xxfRnr/K+dENd1Gma6+ZMnoM34QsgbiqvLsXDnDDqxEgRxz1dvMBCleu2UQDJ5zIqNX8g5IPZpJU7ukLWsxk/59yVyTzVf8ZJWKEpUcO6d1IhC71E8qPObBWSaBhbjTi0u+FvcsB+kyi6bfSEmn+5pSvvIZ/Aljz1zIbutzKVr5KZIZ3cFYUMqQGgnOfrTeV5wMizX1URXwY+QieIwR8S5/ykdMSBE/u/4lJAUE/CYaDzuH0DEI=----ATTACHMENT:----NTUwNjI2ODcxNjUyNzg3NSA5OTQyMDg3NjIwNzMwODM2IDkyNTA4MTgyMDIzNzg2Njc=