Exemplo n.º 1
0
 /**
  * Converts value to DateTime object.
  * @param  string key
  * @param  string format
  * @return DateTime
  */
 public function asDateTime($key, $format = NULL)
 {
     $time = $this[$key];
     if ((int) $time === 0) {
         // '', NULL, FALSE, '0000-00-00', ...
         return NULL;
     }
     $dt = new DateTime53(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
     return $format === NULL ? $dt : $dt->format($format);
 }
Exemplo n.º 2
0
 /**
  * Converts value to specified type and format.
  * @param  mixed  value
  * @param  int    type
  * @return mixed
  */
 protected function convert($value, $type)
 {
     if ($value === NULL || $value === FALSE) {
         return NULL;
     }
     switch ($type) {
         case dibi::TEXT:
             return (string) $value;
         case dibi::BINARY:
             return $this->getDriver()->unescape($value, $type);
         case dibi::INTEGER:
             return (int) $value;
         case dibi::FLOAT:
             return (double) $value;
         case dibi::DATE:
         case dibi::DATETIME:
             if ((int) $value === 0) {
                 // '', NULL, FALSE, '0000-00-00', ...
                 return NULL;
             } elseif ($this->dateFormat === '') {
                 // return DateTime object (default)
                 return new DateTime53(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value);
             } elseif ($this->dateFormat === 'U') {
                 // return timestamp
                 return is_numeric($value) ? (int) $value : strtotime($value);
             } elseif (is_numeric($value)) {
                 // formatted date
                 return date($this->dateFormat, $value);
             } else {
                 $value = new DateTime53($value);
                 return $value->format($this->dateFormat);
             }
         case dibi::BOOL:
             return (bool) $value && $value !== 'f' && $value !== 'F';
         default:
             return $value;
     }
 }