Alfredao

DateTimeStrategy

Feb 11th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.49 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Application\Hydrator\Strategy;
  4.  
  5. use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;
  6. use Zend\Stdlib\AbstractOptions;
  7.  
  8. class Datetime extends AbstractOptions implements StrategyInterface
  9. {
  10.    
  11.     /**
  12.      *
  13.      * @var string
  14.      */
  15.     protected $format = 'd/m/Y';
  16.    
  17.     /**
  18.      *
  19.      * @var string
  20.      */
  21.     protected $timeZone = 'America/Sao_paulo';
  22.    
  23.     /**
  24.      *
  25.      * @var \DateTimeZone
  26.      */
  27.     protected $dateTimeZone;
  28.    
  29.     /**
  30.      * Retuns \Datetime without any treatment.
  31.      * @return \Datetime
  32.      */
  33.     public function extract($value)
  34.     {
  35.         return $value;
  36.     }
  37.    
  38.     /**
  39.      * Retuns Datetime object from a string value.
  40.      *
  41.      * @param string $value
  42.      * @return \Datetime
  43.      */
  44.     public function hydrate($value)
  45.     {
  46.         $dateObject = \Datetime::createFromFormat(
  47.             $this->getFormat(),
  48.             $value,
  49.             $this->getDateTimeZone()
  50.         );
  51.         return $dateObject;
  52.     }
  53.    
  54.     public function getFormat()
  55.     {
  56.         return $this->format;
  57.     }
  58.    
  59.     public function setFormat($format)
  60.     {
  61.         $this->format = $format;
  62.         return $this;
  63.     }
  64.    
  65.     public function getTimeZone()
  66.     {
  67.         return $this->timeZone;
  68.     }
  69.    
  70.     public function setTimeZone($timeZone)
  71.     {
  72.         $this->timeZone = $timeZone;
  73.         return $this;
  74.     }
  75.    
  76.     public function getDateTimeZone()
  77.     {
  78.         if (!$this->dateTimeZone) {
  79.             $this->setDateTimeZone(new \DateTimeZone($this->getTimeZone()));
  80.         }
  81.         return $this->dateTimeZone;
  82.     }
  83.    
  84.     public function setDateTimeZone($dateTimeZone)
  85.     {
  86.         $this->dateTimeZone = $dateTimeZone;
  87.         return $this;
  88.     }
  89.    
  90. }
Advertisement
Add Comment
Please, Sign In to add comment