[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/zendframework/zend-stdlib/src/Hydrator/Strategy/ -> DateTimeFormatterStrategy.php (source)

   1  <?php
   2  /**
   3   * Zend Framework (http://framework.zend.com/)
   4   *
   5   * @link      http://github.com/zendframework/zf2 for the canonical source repository
   6   * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   7   * @license   http://framework.zend.com/license/new-bsd New BSD License
   8   */
   9  
  10  namespace Zend\Stdlib\Hydrator\Strategy;
  11  
  12  use DateTime;
  13  use DateTimeZone;
  14  
  15  final class DateTimeFormatterStrategy implements StrategyInterface
  16  {
  17      /**
  18       * @var string
  19       */
  20      private $format;
  21  
  22      /**
  23       * @var DateTimeZone|null
  24       */
  25      private $timezone;
  26  
  27      /**
  28       * Constructor
  29       *
  30       * @param string            $format
  31       * @param DateTimeZone|null $timezone
  32       */
  33      public function __construct($format = DateTime::RFC3339, DateTimeZone $timezone = null)
  34      {
  35          $this->format   = (string) $format;
  36          $this->timezone = $timezone;
  37      }
  38  
  39      /**
  40       * {@inheritDoc}
  41       *
  42       * Converts to date time string
  43       *
  44       * @param mixed|DateTime $value
  45       *
  46       * @return mixed|string
  47       */
  48      public function extract($value)
  49      {
  50          if ($value instanceof DateTime) {
  51              return $value->format($this->format);
  52          }
  53  
  54          return $value;
  55      }
  56  
  57      /**
  58       * Converts date time string to DateTime instance for injecting to object
  59       *
  60       * {@inheritDoc}
  61       *
  62       * @param mixed|string $value
  63       *
  64       * @return mixed|DateTime
  65       */
  66      public function hydrate($value)
  67      {
  68          if ($value === '' || $value === null) {
  69              return;
  70          }
  71  
  72          if ($this->timezone) {
  73              $hydrated = DateTime::createFromFormat($this->format, $value, $this->timezone);
  74          } else {
  75              $hydrated = DateTime::createFromFormat($this->format, $value);
  76          }
  77  
  78          return $hydrated ?: $value;
  79      }
  80  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1