Last active
February 29, 2016 08:02
-
-
Save jesseschalken/de6b49a8e1ce98e23b51 to your computer and use it in GitHub Desktop.
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 | |
namespace JesseSchalken; | |
/** | |
* @param mixed $x | |
* @return string | |
*/ | |
function assert_string($x) { | |
if (is_string($x)) { | |
return $x; | |
} else { | |
throw type_error('string', $x); | |
} | |
} | |
/** | |
* @param mixed $x | |
* @return int | |
*/ | |
function assert_int($x) { | |
if (is_int($x)) { | |
return $x; | |
} else { | |
throw type_error('int', $x); | |
} | |
} | |
/** | |
* @param mixed $x | |
* @return float | |
*/ | |
function assert_float($x) { | |
if (is_float($x)) { | |
return $x; | |
} else { | |
throw type_error('float', $x); | |
} | |
} | |
/** | |
* @param mixed $x | |
* @return null | |
*/ | |
function assert_null($x) { | |
if (is_null($x)) { | |
return $x; | |
} else { | |
throw type_error('null', $x); | |
} | |
} | |
/** | |
* @param mixed $x | |
* @return resource | |
*/ | |
function assert_resource($x) { | |
if (is_resource($x)) { | |
return $x; | |
} else { | |
throw type_error('resource', $x); | |
} | |
} | |
/** | |
* @param mixed $x | |
* @return object | |
*/ | |
function assert_object($x) { | |
if (is_object($x)) { | |
return $x; | |
} else { | |
throw type_error('object', $x); | |
} | |
} | |
/** | |
* @param mixed $x | |
* @return callable | |
*/ | |
function assert_callable($x) { | |
if (is_callable($x)) { | |
return $x; | |
} else { | |
throw type_error('callable', $x); | |
} | |
} | |
/** | |
* @param mixed $x | |
* @return array | |
*/ | |
function assert_array($x) { | |
if (is_array($x)) { | |
return $x; | |
} else { | |
throw type_error('array', $x); | |
} | |
} | |
/** | |
* @param mixed $x | |
* @return bool | |
*/ | |
function assert_bool($x) { | |
if (is_bool($x)) { | |
return $x; | |
} else { | |
throw type_error('bool', $x); | |
} | |
} | |
/** | |
* @param mixed $x | |
* @return string[] | |
*/ | |
function assert_strings($x) { | |
/** @var string[] $r */ | |
$r = array(); | |
foreach (assert_array($x) as $k => $v) { | |
$r[$k] = assert_string($v); | |
} | |
return $r; | |
} | |
/** | |
* @param mixed $x | |
* @return int[] | |
*/ | |
function assert_ints($x) { | |
/** @var int[] $r */ | |
$r = array(); | |
foreach (assert_array($x) as $k => $v) { | |
$r[$k] = assert_int($v); | |
} | |
return $r; | |
} | |
/** | |
* @param mixed $x | |
* @return float[] | |
*/ | |
function assert_floats($x) { | |
/** @var float[] $r */ | |
$r = array(); | |
foreach (assert_array($x) as $k => $v) { | |
$r[$k] = assert_float($v); | |
} | |
return $r; | |
} | |
/** | |
* @param mixed $x | |
* @return bool[] | |
*/ | |
function assert_bools($x) { | |
/** @var bool[] $r */ | |
$r = array(); | |
foreach (assert_array($x) as $k => $v) { | |
$r[$k] = assert_bool($v); | |
} | |
return $r; | |
} | |
/** | |
* @param mixed $x | |
* @return resource[] | |
*/ | |
function assert_resources($x) { | |
/** @var resource[] $r */ | |
$r = array(); | |
foreach (assert_array($x) as $k => $v) { | |
$r[$k] = assert_resource($v); | |
} | |
return $r; | |
} | |
/** | |
* @param mixed $x | |
* @return string | |
*/ | |
function to_string($x) { | |
if (is_scalar($x) || is_null($x) || is_resource($x)) { | |
return (string)$x; | |
} | |
if (is_object($x)) { | |
$c = array($x, '__toString'); | |
if (is_callable($c)) { | |
return assert_string($c()); | |
} | |
} | |
throw new TypeError("Cannot convert " . dump($x) . " to string"); | |
} | |
/** | |
* @param string $type | |
* @param mixed $value | |
* @return TypeError | |
*/ | |
function type_error($type, $value) { | |
return new TypeError(dump($value) . " is incompatible with $type"); | |
} | |
/** | |
* @param mixed $x | |
* @return string | |
*/ | |
function dump($x) { | |
if (is_null($x)) { | |
return 'null'; | |
} else if (is_bool($x)) { | |
return $x ? 'true' : 'false'; | |
} else if (is_scalar($x)) { | |
$s = var_export($x, true); | |
if (is_float($x) && strpos($s, '.') === false) { | |
$s .= '.0'; | |
} | |
return $s; | |
} else if (is_object($x)) { | |
return get_class($x); | |
} else if (is_array($x)) { | |
return 'array'; | |
} else if (is_resource($x)) { | |
return 'resource'; | |
} else { | |
throw new \Exception('Unhandled type: ' . gettype($x)); | |
} | |
} | |
class TypeError extends \LogicError { | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment