app = $app; $this->request = $request; $this->api_version = $api->get_version(); $this->response_time = $date->format(DATE_ATOM); $this->response = new stdClass(); return $this; } /** * Set datas to the response * If no datas provided (aka empty array), a stdClass if set, * so the serialized datas will be objects * * @param array $datas * @return API_V1_result */ public function set_datas(array $datas) { if (count($datas) === 0) { $datas = new stdClass (); } $this->response = $datas; return $this; } /** * Return response data * * @return array */ public function get_datas() { return (array) $this->response; } /** * Format the data and return serialized string * * @return string */ public function format() { $ret = array( 'meta' => array( 'api_version' => $this->api_version , 'request' => sprintf('%s %s', $this->request->getMethod(), $this->request->getBasePath() . $this->request->getPathInfo() ) , 'response_time' => $this->response_time , 'http_code' => $this->http_code , 'error_type' => $this->error_type , 'error_message' => $this->error_message , 'error_details' => $this->error_details , 'charset' => 'UTF-8' ) , 'response' => $this->response ); $this->app['dispatcher']->dispatch(PhraseaEvents::API_RESULT, new ApiResultEvent()); $conf = $this->app['phraseanet.configuration']; if (isset($conf['main']['api-timers']) && true === $conf['main']['api-timers']) { $ret['timers'] = $this->app['api.timers']->toArray(); } $return_value = false; switch ($this->request->getRequestFormat(self::FORMAT_JSON)) { case self::FORMAT_JSON: case self::FORMAT_JSON_EXTENDED: default: $return_value = \p4string::jsonencode($ret); break; case self::FORMAT_YAML: case self::FORMAT_YAML_EXTENDED: if ($ret['response'] instanceof \stdClass) { $ret['response'] = array(); } $dumper = new Dumper(); $return_value = $dumper->dump($ret, 8); break; case self::FORMAT_JSONP: case self::FORMAT_JSONP_EXTENDED: $callback = trim($this->request->get('callback')); $return_value = $callback . '(' . \p4string::jsonencode($ret) . ')'; break; } return $return_value; } /** * Set the API_V1_result http_code, error_type, error_message and error_details * with the appropriate datas * * @param string $const * @param string $message * * @return API_V1_result */ public function set_error_message($const, $message) { $this->error_details = $message; switch ($const) { case self::ERROR_BAD_REQUEST: $this->http_code = 400; $this->error_type = $const; $this->error_message = API_V1_exception_badrequest::get_details(); break; case self::ERROR_UNAUTHORIZED: $this->http_code = 401; $this->error_type = $const; $this->error_message = API_V1_exception_unauthorized::get_details(); break; case self::ERROR_FORBIDDEN: $this->http_code = 403; $this->error_type = $const; $this->error_message = API_V1_exception_forbidden::get_details(); break; case self::ERROR_NOTFOUND: $this->http_code = 404; $this->error_type = $const; $this->error_message = API_V1_exception_notfound::get_details(); break; case self::ERROR_METHODNOTALLOWED: $this->http_code = 405; $this->error_type = $const; $this->error_message = API_V1_exception_methodnotallowed::get_details(); break; case self::ERROR_INTERNALSERVERERROR: $this->http_code = 500; $this->error_type = $const; $this->error_message = API_V1_exception_internalservererror::get_details(); break; case self::ERROR_MAINTENANCE: $this->http_code = 503; $this->error_type = $const; $this->error_message = API_V1_exception_maintenance::get_details(); break; case self::ERROR_UNACCEPTABLE: $this->http_code = 406; $this->error_type = $const; break; case OAUTH2_ERROR_INVALID_REQUEST: $this->error_type = $const; break; } return $this; } /** * Set the API_V1_result http_code, error_message and error_details * with the appropriate datas * * @param integer $code * * @return API_V1_result */ public function set_error_code($code) { switch ($code = (int) $code) { case 400: $this->http_code = $code; $this->error_type = self::ERROR_BAD_REQUEST; $this->error_message = API_V1_exception_badrequest::get_details(); break; case 401: $this->http_code = $code; $this->error_type = self::ERROR_UNAUTHORIZED; $this->error_message = API_V1_exception_unauthorized::get_details(); break; case 403: $this->http_code = $code; $this->error_type = self::ERROR_FORBIDDEN; $this->error_message = API_V1_exception_forbidden::get_details(); break; case 404: $this->http_code = $code; $this->error_type = self::ERROR_NOTFOUND; $this->error_message = API_V1_exception_notfound::get_details(); break; case 405: $this->http_code = $code; $this->error_type = self::ERROR_METHODNOTALLOWED; $this->error_message = API_V1_exception_methodnotallowed::get_details(); break; case 406: $this->http_code = $code; $this->error_type = self::ERROR_UNACCEPTABLE; break; case 500: $this->http_code = $code; $this->error_type = self::ERROR_INTERNALSERVERERROR; $this->error_message = API_V1_exception_internalservererror::get_details(); break; } return $this; } /** * Returns the correct http code depending on the errors * * @return int */ public function get_http_code() { return $this->http_code; } /** * * @param int $code */ public function set_http_code($code) { $this->http_code = (int) $code; } /** * Return a Symfony Response * * @return \Symfony\Component\HttpFoundation\Response */ public function get_response() { $response = new Response($this->format(), $this->get_http_code()); $response->setCharset('UTF-8'); return $response; } }