Ejemplo n.º 1
0
 /**
  * @author LAHAXE Arnaud
  *
  * @param        \Generator $patchs
  * @param string $targetDocument
  *
  * @return mixed
  */
 public function compile($patchs, $targetDocument = '{}')
 {
     foreach ($patchs as $patch) {
         try {
             $patch = new Patch($targetDocument, $patch);
             $targetDocument = $patch->apply();
         } catch (InvalidPatchDocumentJsonException $e) {
         } catch (InvalidTargetDocumentJsonException $e) {
         } catch (InvalidOperationException $e) {
         } catch (FailedTestException $e) {
         }
     }
     return json_decode($targetDocument);
 }
Ejemplo n.º 2
0
 * @apiError (404) PageDoesNotExists If page does not exists
 * @apiError (404) PageException If exception
 */
$this->patch('/{slug:.+}', function ($req, $res, $args) use($app, $pages) {
    $parsedBody = $req->getParsedBody();
    $bodyJson = json_encode($req->getParsedBody());
    $slug = $args['slug'];
    if (!isset($req->getQueryParams()['parse'])) {
        $pages->setRenderer($app['pageRendererRAW']);
    }
    $page = $pages->read($slug);
    if (!$page) {
        return $this->api->json($res, ['error' => 'PageDoesNotExists', 'message' => 'Page does not exists'], 404);
    }
    try {
        $patch = new Patch($page, $bodyJson);
        $patchedDocument = $patch->apply();
        $newPage = Page::pageFactory(json_decode($patchedDocument, true));
        $pages->update($slug, $newPage);
    } catch (InvalidPatchDocumentJsonException $e) {
        // Will be thrown when using invalid JSON in a patch document
        return $this->api->json($res, ['error' => 'InvalidPatchDocumentJsonException', 'message' => $e->getMessage()], 400);
    } catch (InvalidTargetDocumentJsonException $e) {
        // Will be thrown when using invalid JSON in a target document
        return $this->api->json($res, ['error' => 'InvalidTargetDocumentJsonException', 'message' => $e->getMessage()], 400);
    } catch (InvalidOperationException $e) {
        // Will be thrown when using an invalid JSON Pointer operation (i.e. missing property)
        return $this->api->json($res, ['error' => 'InvalidOperationException', 'message' => $e->getMessage()], 400);
    } catch (Exception $e) {
        return $this->api->json($res, ['error' => 'InvalidOperationException', 'message' => $e->getMessage()], 500);
    }
Ejemplo n.º 3
0
 /**
  * @param \Staffim\DTOBundle\Model\ModelInterface $model
  * @param string $patchOperations
  * @return string
  * @throws \Rs\Json\Patch\FailedTestException
  */
 protected function applyPatch(ModelInterface $model, $patchOperations)
 {
     try {
         $document = $this->serializer->serialize($this->mapper->map($model), 'json', $this->serializationContext);
         $patch = new Patch($document, $patchOperations);
         return $patch->apply();
     } catch (Patch\FailedTestException $exception) {
         throw new Exception($exception->getMessage(), 417, $exception);
     } catch (Patch\Exception $exception) {
         throw new Exception($exception->getMessage(), 400, $exception);
     }
 }
Ejemplo n.º 4
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;
 }
Ejemplo n.º 5
0
<?php

use Rs\Json\Patch;
require '../vendor/autoload.php';
$tension = '{
    "role": {
        "0": {},
        "1": {}
    }
}';
/*$patchDocument  = '[
    { "op": "add", "path": "/", "value": {"path": "/role", "action": "add"}},
    { "op": "update", "path": "/", "value": {"path": "/role", "action": "add"}}
]';*/
$patchDocument = '[
    { "op": "add", "path": "/role", "value": {"0": {},"1" :{}}},
    { "op": "add", "path": "/role/0/name", "value": "titi"},
    { "op": "add", "path": "/role/1/name", "value": "toto"},
    { "op": "add", "path": "/role/stringid/name", "value": "toto"},
    { "op": "replace", "path": "/role/0", "value": {"name": "tutu"}}
]';
$test = new Patch($tension, $patchDocument);
echo $test->apply();
Ejemplo n.º 6
0
 /**
  * @param \Staffim\DTOBundle\Model\ModelInterface $model
  * @param string $patchOperations
  * @return string
  * @throws \Rs\Json\Patch\FailedTestException
  */
 protected function applyPatch(ModelInterface $model, $patchOperations)
 {
     $document = $this->serializer->serialize($this->mapper->map($model), 'json', $this->serializationContext);
     $patch = new Patch($document, $patchOperations);
     return $patch->apply();
 }