Example #1
0
 /**
  * Sets up the default casting functions
  * 
  * @internal
  * @return void
  */
 private static function _defaultCasters()
 {
     self::$_defaultCasters = array("int" => function ($var, $currentType) {
         switch ($currentType) {
             case "boolean":
                 if ($var === true) {
                     return 1;
                 } else {
                     return 0;
                 }
                 break;
             case "integer":
                 return $var;
                 break;
             case "string":
                 if (empty($var)) {
                     return 0;
                 }
                 if (is_numeric($var)) {
                     return (int) round(\schlaus\schange\schange::castToFloat($var));
                 }
                 if (\schlaus\schange\schange::canCastTo("bool", $var)) {
                     return \schlaus\schange\schange::castToInt(\schlaus\schange\schange::castToBool($var));
                 }
                 break;
             case "array":
                 if (empty($var)) {
                     return 0;
                 }
                 if (count($var) == 1) {
                     return \schlaus\schange\schange::castToInt(reset($var));
                 }
                 break;
             case "double":
                 return (int) round($var);
                 break;
             case "object":
                 if (null !== ($tmp = \schlaus\schange\schange::castToStr($var))) {
                     return \schlaus\schange\schange::castToInt($tmp);
                 }
                 if (null !== ($tmp = \schlaus\schange\schange::castToArr($var))) {
                     return \schlaus\schange\schange::castToInt($tmp);
                 }
                 return null;
                 break;
             case "NULL":
                 return 0;
                 break;
         }
         return null;
     }, "float" => function ($var, $currentType) {
         switch ($currentType) {
             case "boolean":
                 if ($var === true) {
                     return 1.0;
                 } else {
                     return 0.0;
                 }
                 break;
             case "integer":
                 return floatval($var);
                 break;
             case "string":
                 if (empty($var)) {
                     return 0.0;
                 }
                 if (is_numeric($var)) {
                     return floatval($var);
                 }
                 if (\schlaus\schange\schange::canCastTo("bool", $var)) {
                     return \schlaus\schange\schange::castToFloat(\schlaus\schange\schange::castToBool($var));
                 }
                 return null;
                 break;
             case "array":
                 if (empty($var)) {
                     return 0.0;
                 }
                 if (count($var) == 1) {
                     return \schlaus\schange\schange::castToFloat(reset($var));
                 }
                 return null;
                 break;
             case "double":
                 return $var;
                 break;
             case "object":
                 if (null !== ($tmp = \schlaus\schange\schange::castToStr($var))) {
                     return \schlaus\schange\schange::castToFloat($tmp);
                 }
                 if (null !== ($tmp = \schlaus\schange\schange::castToArr($var))) {
                     return \schlaus\schange\schange::castToFloat($tmp);
                 }
                 return null;
                 break;
             case "NULL":
                 return 0.0;
                 break;
         }
         return null;
     }, "str" => function ($var, $currentType) {
         switch ($currentType) {
             case "boolean":
                 if ($var === true) {
                     return "true";
                 } else {
                     return "false";
                 }
                 break;
             case "integer":
                 return (string) $var;
                 break;
             case "string":
                 return $var;
                 break;
             case "array":
                 if (empty($var)) {
                     return "";
                 }
                 if (count($var) == 1) {
                     return \schlaus\schange\schange::castToStr(reset($var));
                 }
                 $parsedStr = "";
                 foreach ($var as $val) {
                     if (null === ($parsedStr .= \schlaus\schange\schange::castToStr($val))) {
                         return null;
                     }
                 }
                 return $parsedStr;
                 break;
             case "double":
                 return (string) $var;
                 break;
             case "object":
                 if (method_exists($var, "__toString")) {
                     return (string) $var;
                 }
                 if (null !== ($tmp = \schlaus\schange\schange::castToArr($var))) {
                     return \schlaus\schange\schange::castToStr($tmp);
                 }
                 return null;
                 break;
             case "NULL":
                 return "";
                 break;
         }
         return null;
     }, "bool" => function ($var, $currentType) {
         switch ($currentType) {
             case "boolean":
                 return $var;
                 break;
             case "integer":
                 return (bool) $var;
                 break;
             case "string":
                 if (strtolower($var) === "true") {
                     return true;
                 }
                 if (strtolower($var) === "false") {
                     return false;
                 }
                 if (is_numeric($var)) {
                     return (bool) \schlaus\schange\schange::castToInt($var);
                 }
                 return null;
                 break;
             case "array":
                 if (empty($var)) {
                     return false;
                 }
                 if (count($var) == 1) {
                     return \schlaus\schange\schange::castToBool(reset($var));
                 }
                 return null;
                 break;
             case "double":
                 return (bool) $var;
                 break;
             case "object":
                 if (null !== ($tmp = \schlaus\schange\schange::castToStr($var))) {
                     return \schlaus\schange\schange::castToBool($tmp);
                 }
                 if (null !== ($tmp = \schlaus\schange\schange::castToArr($var))) {
                     return \schlaus\schange\schange::castToBool($tmp);
                 }
                 return null;
                 break;
             case "NULL":
                 return false;
                 break;
         }
         return null;
     }, "arr" => function ($var, $currentType) {
         switch ($currentType) {
             case "boolean":
                 return null;
                 break;
             case "integer":
             case "double":
                 $arr = str_split(\schlaus\schange\schange::castToStr($var));
                 foreach ($arr as &$val) {
                     if ($val !== ".") {
                         $val = intval($val);
                     }
                 }
                 return $arr;
                 break;
             case "string":
                 return str_split($var);
                 break;
             case "array":
                 return $var;
                 break;
             case "object":
                 return json_decode(json_encode($var), true);
                 break;
             case "NULL":
                 return array();
                 break;
         }
         return null;
     }, "obj" => function ($var, $currentType) {
         switch ($currentType) {
             case "boolean":
             case "integer":
             case "string":
             case "double":
                 if (null !== ($arr = \schlaus\schange\schange::castToArr($var))) {
                     return \schlaus\schange\schange::castToObj($arr);
                 }
                 return null;
                 break;
             case "array":
                 $obj = json_decode(json_encode($var), false);
                 if (is_object($obj)) {
                     return $obj;
                 }
                 return null;
                 break;
             case "object":
                 return $var;
                 break;
             case "NULL":
                 return new stdClass();
                 break;
         }
         return null;
     });
 }
Example #2
0
 public function testCastableList()
 {
     $var = "test";
     $list = array("str", "arr");
     $this->assertSame(schange::castable($var), $list);
 }