Example #1
0
 /**
  * Patch a record
  *
  * @param Number  $id      ID of record
  * @param Request $request Current http request
  *
  * @throws MalformedInputException
  *
  * @return Response $response Result of action with data (if successful)
  */
 public function patchAction($id, Request $request)
 {
     $response = $this->getResponse();
     $this->formValidator->checkJsonRequest($request, $response);
     // Check JSON Patch request
     $this->formValidator->checkJsonPatchRequest(json_decode($request->getContent(), 1));
     // Find record && apply $ref converter
     $record = $this->findRecord($id);
     $jsonDocument = $this->serialize($record);
     // Check/validate JSON Patch
     if (!$this->jsonPatchValidator->validate($jsonDocument, $request->getContent())) {
         throw new InvalidJsonPatchException($this->jsonPatchValidator->getException()->getMessage());
     }
     try {
         // Apply JSON patches
         $patch = new Patch($jsonDocument, $request->getContent());
         $patchedDocument = $patch->apply();
     } catch (InvalidPatchDocumentJsonException $e) {
         throw new InvalidJsonPatchException($e->getMessage());
     } catch (InvalidTargetDocumentJsonException $e) {
         throw new InvalidJsonPatchException($e->getMessage());
     } catch (InvalidOperationException $e) {
         throw new InvalidJsonPatchException($e->getMessage());
     } catch (FailedTestException $e) {
         throw new InvalidJsonPatchException($e->getMessage());
     }
     // Validate result object
     $model = $this->getModel();
     $record = $this->formValidator->checkForm($this->formValidator->getForm($request, $model), $model, $this->formDataMapper, $patchedDocument);
     // Update object
     $this->getModel()->updateRecord($id, $record);
     // Set status code
     $response->setStatusCode(Response::HTTP_OK);
     // Set Content-Location header
     $response->headers->set('Content-Location', $this->getRouter()->generate($this->getRouteName($request), array('id' => $record->getId())));
     return $response;
 }