-
-
Save tleilax/6038516 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
<? | |
function words($string) | |
{ | |
return preg_split('/ /', $string, -1, PREG_SPLIT_NO_EMPTY); | |
} | |
function halt($status = 200, $headers = array(), $body = '') | |
{ | |
$args = func_get_args(); | |
$result = array(); | |
$constraints = array( | |
'status' => 'is_int', | |
'headers' => 'is_array', | |
'body' => 'is_string', | |
); | |
foreach ($constraints as $state => $constraint) { | |
if ($constraint(current($args))) { | |
$result[$state] = array_shift($args); | |
} | |
} | |
echo json_encode(func_get_args()) . "\n"; | |
echo json_encode($result) . "\n\n"; | |
} | |
header('Content-Type: text/plain'); | |
halt(); | |
// => [] | |
halt(201); | |
// => {"status":201} | |
halt("Done"); | |
// => {"body":"Done"} | |
halt(100, "Continue"); | |
// => {"status":100,"body":"Continue"} | |
halt(301, array('Location'=>'y')); | |
// => {"status":301,"headers":{"Location":"y"}} | |
halt(302, "Found"); | |
// => {"status":302,"body":"Found"} | |
halt(303, array('Location'=>'y'), 'See other'); | |
// => {"status":303,"headers":{"Location":"y"},"body":"See other"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment