/** * Adds a new CakeResponse object to the response collection. * * @param integer $tid Transaction ID * @param CakeResponse $response Cake response object * @param CakeRequest $request CakeRequest object * @param boolean $exception TRUE if the response is an exception. * @return BanchaResponseCollection */ public function addResponse($tid, CakeResponse $response, CakeRequest $request, $exception = false) { $response = array('type' => 'rpc', 'tid' => $tid, 'action' => Inflector::singularize($request->controller), 'method' => BanchaResponseTransformer::getMethod($request), 'result' => BanchaResponseTransformer::transform($response->body(), $request)); if ($request['extUpload']) { $response['extUpload'] = true; } $this->responses[] = $response; return $this; }
/** * Transform a cake response to extjs structure (associated models are not supported!) * otherwise just return the original response. * See also https://github.com/Bancha/Bancha/wiki/Supported-Controller-Method-Results * * @param $modelName The model name of the current request * @param $response The input request from Bancha * @param $controller The used controller * @return extjs formated data array */ public static function transformDataStructureToExt($modelName, $response) { // understand primitive responses if ($response === true || $response === false) { // this was an un-/successfull operation, return that to ext return array('success' => $response); } // expect a successfull operation, but check $success = isset($response['success']) ? !!$response['success'] : true; if (isset($response[$modelName])) { // this is standard cake single element structure $response = array('success' => $success, 'data' => $response[$modelName]); } else { if (isset($response['0'][$modelName])) { // this is standard cake multiple element structure $data = array(); foreach ($response as $record) { array_push($data, $record[$modelName]); } $response = array('success' => $success, 'data' => $data); } else { if (isset($response['records']) && (isset($response['records']['0'][$modelName]) || is_array($response['records']) && isset($response['count']) && $response['count'] == 0)) { // pagination with zero records // this is a paging response // the records have standard cake structure, so get them $data = BanchaResponseTransformer::transformDataStructureToExt($modelName, $response['records']); // create response including the total number of records $response = array('success' => $success, 'data' => isset($data['data']) ? $data['data'] : $data, 'total' => $response['count']); } else { if (is_array($response) && count($response) === 0) { // this is an empty array, so expect that this is just a request without any found records return array('success' => true, 'data' => array()); } } } } // else the structure could not be recognized as any transformable data // so output it as it is return $response; }
/** * Unrecognizable structures are just passed through * @param $cakeResponse cake response to transform * * @dataProvider getUnrecognizableResponses */ public function testTransformUnrecognizable($cakeResponse) { $request = new CakeRequest(); $request->addParams(array('controller' => 'Articles', 'action' => 'delete')); $this->assertEquals($cakeResponse, BanchaResponseTransformer::transform($cakeResponse, $request)); }