Пример #1
0
 protected function typeCastForSet($attrName, $value)
 {
     switch ($this->getAttribute($attrName)->type()) {
         case 'datetime':
             if ($value instanceof MongoDate) {
                 list($u, $s) = explode(" ", (string) $value);
                 $u = (double) $u;
                 $s = (int) $s;
                 return Carbon::createFromFormat('U.u', sprintf('%.f', $s + $u));
             }
             break;
         case 'mongoId':
             return $value;
     }
     return parent::typeCastForSet($attrName, $value);
 }
Пример #2
0
 protected function convertStringToTime($string)
 {
     try {
         if (ctype_digit($string)) {
             return Carbon::createFromTimestamp($string);
         } else {
             return new Carbon($string);
         }
     } catch (\Exception $e) {
         return null;
     }
 }
Пример #3
0
 protected function typeCastForSet($attrName, $value)
 {
     switch ($this->getAttribute($attrName)->type()) {
         case 'integer':
             if (is_int($value)) {
                 return $value;
             } elseif (is_scalar($value)) {
                 return (int) $value;
             } else {
                 return null;
             }
         case 'float':
             if (is_float($value)) {
                 return $value;
             } elseif (is_scalar($value)) {
                 return (double) $value;
             } else {
                 return null;
             }
         case 'datetime':
         case 'timestamp':
         case 'time':
         case 'date':
             if (is_int($value) or is_string($value) && ctype_digit($value)) {
                 $value = Carbon::createFromTimestamp($value);
             } elseif (is_string($value)) {
                 try {
                     $value = new Carbon($value);
                 } catch (\Exception $e) {
                     $value = null;
                 }
             } elseif (!$value instanceof \DateTime) {
                 $value = null;
             }
             return $value;
         case 'boolean':
             return (bool) $value;
         default:
             return $value;
     }
 }