1 | <?php |
||
34 | class KumbiaRest extends Controller |
||
35 | { |
||
36 | /** |
||
37 | * Formato de entrada usado para interpretar los datos |
||
38 | * enviados por el cliente. |
||
39 | * |
||
40 | * @var string MIME Type del formato |
||
41 | */ |
||
42 | protected $_fInput = null; |
||
43 | |||
44 | /** |
||
45 | * Permite definir parser personalizados por MIME TYPE |
||
46 | * Esto es necesario para interpretar las entradas |
||
47 | * Se define como un MIME type como clave y el valor debe ser un |
||
48 | * callback que devuelva los datos interpretado. |
||
49 | */ |
||
50 | protected $_inputType = array( |
||
51 | 'application/json' => array('RestController', 'parseJSON'), |
||
52 | 'application/xml' => array('RestController', 'parseXML'), |
||
53 | 'text/xml' => array('RestController', 'parseXML'), |
||
54 | 'text/csv' => array('RestController', 'parseCSV'), |
||
55 | 'application/x-www-form-urlencoded' => array('RestController', 'parseForm'), |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * Formato de salida enviada al cliente. |
||
60 | * |
||
61 | * @var string nombre del template a usar |
||
62 | */ |
||
63 | protected $_fOutput = null; |
||
64 | |||
65 | /** |
||
66 | * Permite definir las salidas disponibles, |
||
67 | * de esta manera se puede presentar la misma salida en distintos |
||
68 | * formatos a requerimientos del cliente. |
||
69 | */ |
||
70 | protected $_outputType = array( |
||
71 | 'application/json' => 'json', |
||
72 | 'application/xml' => 'xml', |
||
73 | 'text/xml' => 'xml', |
||
74 | 'text/csv' => 'csv', |
||
75 | ); |
||
76 | |||
77 | public function __construct($arg) |
||
82 | |||
83 | /** |
||
84 | * Hacer el router de la petición y envia los parametros correspondientes |
||
85 | * a la acción, adema captura formatos de entrada y salida. |
||
86 | */ |
||
87 | protected function initREST() |
||
95 | |||
96 | /** |
||
97 | * Reescribe la acción. |
||
98 | */ |
||
99 | protected function rewriteActionName() |
||
121 | |||
122 | /** |
||
123 | * Verifica si existe la acción $name existe. |
||
124 | * |
||
125 | * @param string $name nombre de la acción |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | protected function actionExist($name) |
||
137 | |||
138 | /** |
||
139 | * Retorna los parametros de la petición el función del formato de entrada |
||
140 | * de los mismos. Hace uso de los parser definidos en la clase. |
||
141 | */ |
||
142 | protected function param() |
||
156 | |||
157 | /** |
||
158 | * Envia un error al cliente junto con el mensaje. |
||
159 | * |
||
160 | * @param string $text texto del error |
||
161 | * @param int $error Número del error HTTP |
||
162 | * |
||
163 | * @return array data de error |
||
164 | */ |
||
165 | protected function error($text, $error = 400) |
||
171 | |||
172 | /** |
||
173 | * Retorna los formato aceptados por el cliente ordenados por prioridad |
||
174 | * interpretando la cabecera HTTP_ACCEPT. |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | protected static function accept() |
||
197 | |||
198 | /** |
||
199 | * Parse JSON |
||
200 | * Convierte formato JSON en array asociativo. |
||
201 | * |
||
202 | * @param string $input |
||
203 | * |
||
204 | * @return array|string |
||
205 | */ |
||
206 | protected static function parseJSON($input) |
||
215 | |||
216 | /** |
||
217 | * Parse XML. |
||
218 | * |
||
219 | * Convierte formato XML en un objeto, esto será necesario volverlo estandar |
||
220 | * si se devuelven objetos o arrays asociativos |
||
221 | * |
||
222 | * @param string $input |
||
223 | * |
||
224 | * @return \SimpleXMLElement|string |
||
225 | */ |
||
226 | protected static function parseXML($input) |
||
238 | |||
239 | /** |
||
240 | * Parse CSV. |
||
241 | * |
||
242 | * Convierte CSV en arrays numéricos, |
||
243 | * cada item es una linea |
||
244 | * |
||
245 | * @param string $input |
||
246 | * |
||
247 | * @return array |
||
248 | */ |
||
249 | protected static function parseCSV($input) |
||
262 | |||
263 | /** |
||
264 | * Realiza la conversion de formato de Formulario a array. |
||
265 | * |
||
266 | * @param string $input |
||
267 | * |
||
268 | * @return arrat |
||
269 | */ |
||
270 | protected static function parseForm($input) |
||
276 | |||
277 | /** |
||
278 | * Retorna el tipo de formato de entrada. |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | protected static function getInputFormat() |
||
292 | |||
293 | /** |
||
294 | * Devuelve le nombre del formato de salida. |
||
295 | * |
||
296 | * @param array $validOutput Array de formatos de salida soportado |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | protected function getOutputFormat(array $validOutput) |
||
312 | |||
313 | /** |
||
314 | * Retorna todas las cabeceras enviadas por el cliente. |
||
315 | * |
||
316 | * @return array |
||
317 | */ |
||
318 | protected static function getHeaders() |
||
335 | } |
||
336 |