KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
rest.php
Ir a la documentación de este archivo.
1 <?php
32 class Rest
33 {
34 
35  private static $code = array(
36  201 => 'Creado ', /* Se ha creado un nuevo recuerso (INSERT) */
37  400 => 'Bad Request', /* Petición herronea */
38  401 => 'Unauthorized', /* La petición requiere loggin */
39  403 => 'Forbidden',
40  405 => 'Method Not Allowed' /* No está permitido ese metodo */
41  );
45  private static $_outputFormat = array('json', 'text', 'html', 'xml', 'cvs', 'php');
49  private static $_inputFormat = array('json', 'plain', 'x-www-form-urlencoded');
53  private static $_method = null;
57  private static $_oFormat = null;
61  private static $_iFormat = null;
62 
68  static public function accept($accept)
69  {
70  self::$_outputFormat = is_array($accept) ? $accept : explode(',', $accept);
71  }
72 
78  static public function init(Controller $controller)
79  {
80  $content = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : 'text/html';
84  self::$_iFormat = str_replace(array('text/', 'application/'), '', $content);
85 
86  /* Compruebo el método de petición */
87  self::$_method = strtolower($_SERVER['REQUEST_METHOD']);
88  $format = explode(',', $_SERVER['HTTP_ACCEPT']);
89  while (self::$_oFormat = array_shift($format)) {
90  self::$_oFormat = str_replace(array('text/', 'application/'), '', self::$_oFormat);
91  if (in_array(self::$_oFormat, self::$_outputFormat)) {
92  break;
93  }
94  }
95 
99  if (self::$_oFormat == null) {
100  return 'error';
101  } else {
102  View::response(self::$_oFormat);
103  View::select('response');
104  }
105 
109  if ( is_numeric($controller->action_name) ){
110  $controller->parameters = array($controller->action_name) + Rest::param();
111  }else{
112  $controller->parameters = Rest::param();
113  }
114 
119  $controller->action_name = self::$_method;
120  $controller->limit_params = FALSE; //no hay verificación en el numero de parametros.
121  $controller->data = array(); //variable por defecto para las vistas.
122 
123  }
124 
129  static function param()
130  {
131  $input = file_get_contents('php://input');
132  if (strncmp(self::$_iFormat, 'json', 4) == 0) {
133  return json_decode($input, true);
134  } else {
135  parse_str($input, $output);
136  return $output;
137  }
138  }
139 
140 }