private function updatePost($id) { // Abort request if ID is not present or content type is not supported if (empty($id) || !$this->request->isContentTypeSupported()) { return $this->response->sendJson(Response::BAD_REQUEST_CODE); } $validator = new PostValidator(); // Check if validation succeeds // Retrieved post by ID $mapper = new PostMapper($this->container['adapter']); $post = $mapper->findById($id); // If post does not exist, throw not found if (empty($post)) { return $this->response->sendJson(Response::NOT_FOUND_CODE, "No post with such ID is found."); } // Get json data $data = $this->request->getParams(); // Check if data is invalid if (empty($data)) { return $this->response->sendJson(Response::BAD_REQUEST_CODE); } // Set data to post entity $post->setData($data); if ($validator->validate($post)) { // Check if data persists if ($mapper->save($post)) { return $this->response->sendJson(Response::OK_CODE, "Submitted data is updated."); } // If could not persist data return $this->response->sendJson(Response::INTERNAL_SERVER_ERROR_CODE); } // Validation fails return $this->response->sendJson(Response::BAD_REQUEST_CODE, "Submitted data is invalid.", $validator->getValidationErrors()); }