/** * Formats the data of a response * * @param Request $request * @param Response $response * @param ControllerDefinition $controller * @param ActionDefinition $action * @param mixed $result * @param object $obj */ protected function prepareResponseData(Request $request, Response $response, ControllerDefinition $controller, ActionDefinition $action, $result, $obj = null) { $model = $action->getReturnModel(); if ($action->getReturnType() === ActionDefinition::RETURN_BUILDER) { $builderAction = $result; $builderData = array(); $forceInput = false; if (is_array($result)) { $builderAction = $result['action']; $builderController = isset($result['controller']) ? $result['controller'] : $controller; $builderData = isset($result['data']) ? $result['data'] : array(); $forceInput = isset($result['force_input']) ? $result['force_input'] : false; } $schema = array_merge($this->getLimitedActionSchema($builderAction), array('input' => $this->getActionInputSchema($builderController, $builderAction), 'output' => $this->getActionOutputSchema($builderController, $builderAction))); return array('controller' => $builderController->getName(), 'action' => $builderAction->getName(), 'schema' => $schema, 'data' => $builderData, 'force_input' => $forceInput); } if ($action->getReturnType() === ActionDefinition::RETURN_DYNAMIC) { $dynamicAction = $result; $data = null; if (is_array($result)) { list($dynamicAction, $data) = $result; } return array('schema' => $this->getActionOutputSchema($controller, $dynamicAction), 'data' => $this->prepareResponseData($request, $response, $controller, $dynamicAction, $data, $obj)); } if ($action->getReturnType() === ActionDefinition::RETURN_LIST) { $data = array(); if ($result !== null) { foreach ($result as $item) { $data[] = $model->convertObjectToArray($item); } } return $data; } if ($action->getReturnType() === ActionDefinition::RETURN_FILE) { $filename = $action->getName(); $content = $result; if (is_array($result)) { list($filename, $content) = $result; } $response->headers->set('Content-Type', 'application/octet-stream'); $response->headers->set('Content-Disposition', 'attachment; filename=' . $filename); $response->headers->set('Expires', '0'); $response->headers->set('Cache-Control', 'must-revalidate, post-check=0, pre-check=0'); $response->headers->set('Pragma', 'public'); $response->headers->set('Content-Length', strlen($content)); $response->setContent($content); return $response; } if ($action->getReturnType() === ActionDefinition::RETURN_NONE || $result === null) { return null; } if ($model && !is_array($result)) { return $model->convertObjectToArray($result); } return $result; }