KumbiaPHP beta2-dev
Framework PHP en español
|
00001 <?php 00023 class Rest{ 00024 00025 00026 00027 private static $code = array( 00028 201 => 'Creado ', /*Se ha creado un nuevo recuerso (INSERT)*/ 00029 400 => 'Bad Request', /*Petición herronea*/ 00030 401 => 'Unauthorized', /*La petición requiere loggin*/ 00031 403 => 'Forbidden', 00032 405 => 'Method Not Allowed' /*No está permitido ese metodo*/ 00033 ); 00034 00038 private static $outputFormat = array('json', 'text', 'html', 'xml', 'cvs', 'php'); 00039 00043 private static $inputFormat = array('json', 'plain', 'x-www-form-urlencoded'); 00044 00045 00049 private static $method = null; 00050 00055 private static $oFormat =null; 00056 00060 private static $iFormat = null; 00061 00062 00066 static public function accept($accept){ 00067 self::$outputFormat = is_array($accept) ? $accept : explode(',', $accept); 00068 } 00069 00073 static public function init(){ 00074 $content = isset($_SERVER['CONTENT_TYPE'])? $_SERVER['CONTENT_TYPE']:'text/html'; 00078 self::$iFormat = str_replace(array('text/', 'application/'), '', $content); 00079 00080 /*Compruebo el método de petición*/ 00081 self::$method = strtolower($_SERVER['REQUEST_METHOD']); 00082 $format = explode(',', $_SERVER['HTTP_ACCEPT']); 00083 while(self::$oFormat = array_shift($format)){ 00084 self::$oFormat = str_replace(array('text/', 'application/'), '', self::$oFormat); 00085 if(in_array(self::$oFormat, self::$outputFormat)) 00086 break; 00087 } 00088 00092 if(self::$oFormat== null){ 00093 return 'error'; 00094 }else{ 00095 View::response(self::$oFormat); 00096 View::select('response'); 00097 return self::$method; 00098 } 00099 } 00100 00105 static function param(){ 00106 $input = file_get_contents('php://input'); 00107 if (strncmp(self::$iFormat, 'json', 4) == 0) { 00108 return json_decode($input, true); 00109 } else { 00110 parse_str($input, $output); 00111 return $output; 00112 } 00113 } 00114 }