Last active
August 29, 2015 14:07
-
-
Save hikari-no-yume/19c91f800e47d53cc28c to your computer and use it in GitHub Desktop.
Safe Casting Functions RFC conversions table generator (see: https://wiki.php.net/rfc/safe_cast)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class NotStringable {} | |
class Stringable { | |
public function __toString() { | |
return "foobar"; | |
} | |
} | |
$values = [ | |
"foobar", | |
"", | |
"0", | |
0, | |
0.0, | |
"10", | |
"010", | |
"+10", | |
"-10", | |
10, | |
10.0, | |
(string)PHP_INT_MAX, | |
PHP_INT_MAX, | |
(string)PHP_INT_MIN, | |
PHP_INT_MIN, | |
"10.0", | |
"75e-5", | |
"31e+7", | |
NULL, | |
TRUE, | |
FALSE, | |
new StdClass, | |
fopen("data:text/html,foobar", "r"), | |
[], | |
1.5, | |
"1.5", | |
"10abc", | |
"abc10", | |
"100 ", | |
" 100", | |
" 100 ", | |
"0x10", | |
INF, | |
-INF, | |
NAN, | |
PHP_INT_MAX * 2, | |
PHP_INT_MIN * 2, | |
(string)(PHP_INT_MAX * 2), | |
(string)(PHP_INT_MIN * 2), | |
new Stringable, | |
new NotStringable, | |
new StdClass | |
]; | |
function var_dump_string($var) { | |
ob_start(); | |
var_dump($var); | |
$result = ob_get_clean(); | |
return preg_replace('/\s{2,}/', ' ', preg_replace('/[\n\r]/','',rtrim($result))); | |
} | |
$functions = [ | |
NULL, | |
'to_int', | |
'to_float', | |
'to_string' | |
]; | |
// functions which cast to and back to see if data lost | |
$alt_functions = [ | |
NULL, | |
function ($val) { | |
$newval = $val; | |
$type = gettype($val); | |
settype($newval, 'integer'); | |
settype($newval, $type); | |
if ($newval === $val) { | |
return (int)$val; | |
} else { | |
return NULL; | |
} | |
}, | |
function ($val) { | |
$newval = $val; | |
$type = gettype($val); | |
settype($newval, 'double'); | |
settype($newval, $type); | |
if ($newval === $val) { | |
return (float)$val; | |
} else { | |
return NULL; | |
} | |
}, | |
function ($val) { | |
$newval = $val; | |
$type = gettype($val); | |
settype($newval, 'string'); | |
settype($newval, $type); | |
if ($newval === $val) { | |
return (string)$val; | |
} else { | |
return NULL; | |
} | |
} | |
]; | |
$columnWidth = 40; | |
echo '^ '; | |
foreach ($functions as $function) { | |
if ($function === NULL) { | |
echo str_pad('value', $columnWidth); | |
} else { | |
echo str_pad($function, $columnWidth); | |
} | |
echo '^ '; | |
} | |
echo PHP_EOL; | |
foreach ($values as $value) { | |
echo '^ '; | |
foreach ($functions as $i => $function) { | |
$alt_function = $alt_functions[$i]; | |
if ($function === NULL) { | |
echo str_pad(var_dump_string($value), $columnWidth); | |
} else { | |
$result = $function($value) === NULL ? 'fail' : 'pass'; | |
//$result .= '/' . ($alt_function($value) === NULL ? 'fail' : 'pass'); | |
echo str_pad($result, $columnWidth); | |
} | |
echo '| '; | |
} | |
echo PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment