/**
  * Cast a value to string.
  *
  * @static
  *
  * @param mixed $value       Value to cast.
  * @param bool  $array2null  (Optional) Whether to return null for an array or to cast the
  *                           individual values within the array to the chosen type.
  * @param bool  $allow_empty (Optional) Whether to allow empty strings/arrays/objects.
  *
  * @return string|array|null
  */
 function _string($value, $array2null = true, $allow_empty = true)
 {
     if (is_string($value) && ($value !== '' || $allow_empty === true)) {
         return $value;
     } else {
         if (is_int($value) || is_float($value)) {
             return strval($value);
         } else {
             if ($array2null === false && is_array($value)) {
                 return CastToType::recurse($value, '_string', $allow_empty);
             } else {
                 if (is_object($value) && get_parent_class($value) === 'SplType') {
                     if ((string) $value == $value) {
                         return (string) $value;
                     } else {
                         return null;
                     }
                 } else {
                     if (is_object($value) && method_exists($value, '__toString')) {
                         return (string) $value;
                     }
                 }
             }
         }
     }
     return null;
 }
Exemplo n.º 2
0
 /**
  * Set maximum.
  */
 public function setMaximum($maximum)
 {
     $this->maximum = \CastToType::cast($maximum, 'integer', false, true);
 }
Exemplo n.º 3
0
 /**
  * @param mixed  $value
  * @param string $type
  *
  * @return mixed
  */
 private function cast($value, $type)
 {
     $castValue = $value;
     if ($type == 'number') {
         $type = 'num';
     }
     if ($type != 'date') {
         $castValue = \CastToType::cast($value, $type, false, true);
     } else {
         //Specific case for date
         $castValue = new \DateTime($value);
     }
     //The cast not working, parameters is probably not this $type
     if (is_null($castValue)) {
         return $value;
     }
     return $castValue;
 }