/** * @param array $data * @param $className * @param callable $callable * * @return \Symfony\Component\HttpFoundation\Response */ public function get(array $data, $className, callable $callable) { try { DataObject::assertPost($data, $this->serializer, $className, $this->errorBag); $values = DataObject::getAttributes($data, $this->serializer); $model = $callable($data, $values, $this->errorBag); $response = $this->resourceCreated($this->serializer->serialize($model)); } catch (Exception $e) { $response = $this->getErrorResponse($e, $this->errorBag); } return $response; }
/** * @param string|int $id * @param string $className * @param callable $callable * * @return \Symfony\Component\HttpFoundation\Response */ public function get($id, $className, callable $callable) { try { QueryObject::assert($this->serializer, $this->fields, $this->included, new Sorting(), $this->errorBag, $className); $data = $callable(); if (empty($data)) { $mapping = $this->serializer->getTransformer()->getMappingByClassName($className); return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)])); } $response = $this->response($this->serializer->serialize($data, $this->fields, $this->included)); } catch (Exception $e) { $response = $this->getErrorResponse($e); } return $response; }
/** * @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; }
/** * @param callable $totalAmountCallable * @param callable $resultsCallable * @param string $route * @param string $className * * @return \Symfony\Component\HttpFoundation\Response */ public function get(callable $totalAmountCallable, callable $resultsCallable, $route, $className) { try { QueryObject::assert($this->serializer, $this->fields, $this->included, $this->sorting, $this->errorBag, $className); $totalAmount = $totalAmountCallable(); if ($totalAmount > 0 && $this->page->size() > 0 && $this->page->number() > ceil($totalAmount / $this->page->size())) { return $this->resourceNotFound(new ErrorBag([new OufOfBoundsError($this->page->number(), $this->page->size())])); } $links = $this->pagePaginationLinks($route, $this->page->number(), $this->page->size(), $totalAmount, $this->fields, $this->sorting, $this->included, $this->filters); $results = $resultsCallable(); $paginatedResource = new PaginatedResource($this->serializer->serialize($results), $this->page->number(), $this->page->size(), $totalAmount, $links); $response = $this->response($paginatedResource); } catch (Exception $e) { $response = $this->getErrorResponse($e); } return $response; }
public function testSecondLevelNestingEntitySerialize() { $repoCustomer = self::$entityManager->getRepository(Comment::class); $savedComment = $repoCustomer->find(2); $expected = <<<JSON { \t\t "data":{ \t\t "type":"comment", \t\t "id":"2", \t\t "attributes":{ \t\t "comment":"Comment 2", \t\t "id":2, \t\t "parent_id":null \t\t }, \t\t "links":{ \t\t "self":{ \t\t "href":"http://example.com/comment/2" \t\t } \t\t }, \t\t "relationships":{ \t\t "parentComment":{ \t\t "data":{ \t\t "type":"comment", \t\t "id":"1" \t\t } \t\t }, \t\t "post":{ \t\t "data":{ \t\t "type":"post", \t\t "id":"1" \t\t } \t\t } \t\t } \t\t }, \t\t "included":[ \t\t { \t\t "type":"customer", \t\t "id":"1", \t\t "attributes":{ \t\t "name":"Name 1", \t\t "active":true \t\t }, \t\t "links":{ \t\t "self":{ \t\t "href":"http://example.com/customer/1" \t\t } \t\t } \t\t }, \t\t { \t\t "type":"post", \t\t "id":"1", \t\t "attributes":{ \t\t "description":"Description test" \t\t }, \t\t "relationships":{ \t\t "customer":{ \t\t "data":{ \t\t "type":"customer", \t\t "id":"1" \t\t } \t\t } \t\t }, \t\t "links":{ \t\t "self":{ \t\t "href":"http://example.com/post/1" \t\t } \t\t } \t\t }, \t\t { \t\t "type":"comment", \t\t "id":"1", \t\t "attributes":{ \t\t "comment":"Comment 1", \t\t "parent_id":null, \t\t "parentComment":null \t\t }, \t\t "relationships":{ \t\t "post":{ \t\t "data":{ \t\t "type":"post", \t\t "id":"1" \t\t } \t\t } \t\t }, \t\t "links":{ \t\t "self":{ \t\t "href":"http://example.com/comment/1" \t\t } \t\t } \t\t }, \t\t { \t\t "type":"post", \t\t "id":"1", \t\t "attributes":{ \t\t "date":{ \t\t "date":"2016-07-12 16:30:12.000000", \t\t "timezone_type":3, \t\t "timezone":"Europe/Madrid" \t\t }, \t\t "description":"Description test" \t\t }, \t\t "relationships":{ \t\t "customer":{ \t\t "data":{ \t\t "type":"customer", \t\t "id":"1" \t\t } \t\t } \t\t }, \t\t "links":{ \t\t "self":{ \t\t "href":"http://example.com/post/1" \t\t } \t\t } \t\t } \t\t ], \t\t "links":{ \t\t "self":{ \t\t "href":"http://example.com/comment/2" \t\t } \t\t }, \t\t "jsonapi":{ \t\t "version":"1.0" \t\t } \t\t} JSON; $mapper = new Mapper(self::$classConfig); $transformer = new JsonApiTransformer($mapper); $serializer = new JsonApiSerializer($transformer); $customerSerialize = $serializer->serialize($savedComment); $this->assertEquals(json_decode($expected, true), json_decode($customerSerialize, true)); }