123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- namespace Symfony\Component\Form\ValueTransformer;
- /*
- * This file is part of the Symfony framework.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- use Symfony\Component\Form\Exception\UnexpectedTypeException;
- /**
- * Transforms between a timestamp and a DateTime object
- *
- * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
- * @author Florian Eckerstorfer <florian@eckerstorfer.org>
- */
- class DateTimeToTimestampTransformer extends BaseValueTransformer
- {
- /**
- * {@inheritDoc}
- */
- protected function configure()
- {
- $this->addOption('input_timezone', 'UTC');
- $this->addOption('output_timezone', 'UTC');
- parent::configure();
- }
- /**
- * Transforms a DateTime object into a timestamp in the configured timezone
- *
- * @param DateTime $value A DateTime object
- * @return integer A timestamp
- */
- public function transform($value)
- {
- if (null === $value) {
- return null;
- }
- if (!$value instanceof \DateTime) {
- throw new UnexpectedTypeException($value, '\DateTime');
- }
- $value->setTimezone(new \DateTimeZone($this->getOption('output_timezone')));
- return (int)$value->format('U');
- }
- /**
- * Transforms a timestamp in the configured timezone into a DateTime object
- *
- * @param string $value A value as produced by PHP's date() function
- * @return DateTime A DateTime object
- */
- public function reverseTransform($value, $originalValue)
- {
- if (null === $value) {
- return null;
- }
- if (!is_numeric($value)) {
- throw new UnexpectedTypeException($value, 'numeric');
- }
- $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 timestamp. ' . $e->getMessage(), 0, $e);
- }
- }
- }
|