/**
  * @param          $id
  * @param array    $data
  * @param          $className
  * @param callable $findOneCallable
  * @param callable $update
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function get($id, array $data, $className, callable $findOneCallable, callable $update)
 {
     try {
         DataObject::assertPut($data, $this->serializer, $className, $this->errorBag);
         $model = $findOneCallable();
         if (empty($model)) {
             $mapping = $this->serializer->getTransformer()->getMappingByClassName($className);
             return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)]));
         }
         $values = DataObject::getAttributes($data, $this->serializer);
         $update($model, $values, $this->errorBag);
         $response = $this->resourceUpdated($this->serializer->serialize($model));
     } catch (Exception $e) {
         $response = $this->getErrorResponse($e);
     }
     return $response;
 }
Example #2
0
 public function testAssertPutWillReturnErrors()
 {
     $errorBag = new ErrorBag();
     $data = ['type' => 'post', 'attributes' => ['content' => 'Ever heard of Lorem Ipsum?', 'author' => 1, 'comments' => []]];
     $hasErrors = false;
     try {
         DataObject::assertPut($data, $this->serializer, Post::class, $errorBag);
     } catch (DataException $e) {
         $hasErrors = true;
     }
     $this->assertTrue($hasErrors);
 }