/** * @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 $id * @param $className * @param callable $findOneCallable * @param callable $deleteCallable * * @return \Symfony\Component\HttpFoundation\Response */ public function get($id, $className, callable $findOneCallable, callable $deleteCallable) { try { $data = $findOneCallable(); if (empty($data)) { $mapping = $this->serializer->getTransformer()->getMappingByClassName($className); return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)])); } $deleteCallable(); return $this->resourceDeleted(); } catch (Exception $e) { return $this->errorResponse(new ErrorBag([new Error('Bad Request', 'Request could not be served.')])); } }
/** * @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; }
/** * Extract the data from an object. * * @param mixed $value * * @return array */ protected function serializeObject($value) { $serialized = EloquentDriver::serialize($value); if ($value !== $serialized) { return $serialized; } return parent::serializeObject($value); }
/** * @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; }
/** * Extract the data from an object. * * @param mixed $value * * @return array */ protected function serializeObject($value) { $serialized = EloquentDriver::serialize($value); return $value !== $serialized ? $serialized : parent::serializeObject($value); }
/** * @param JsonApiSerializer $serializer * @param Included $included * @param string $paramName */ protected function validateIncludeQueryParamsTypes($serializer, Included $included, $paramName) { if (false === $included->isEmpty()) { $validateFields = array_keys($included->get()); foreach ($validateFields as $key => $field) { $mapping = $serializer->getTransformer()->getMappingByAlias($field); if (null !== $mapping) { $properties = $this->getPropertiesFromMapping($mapping); $invalidProperties = array_diff($included->get()[$field], $properties); $this->addInvalidParameterMemberErrorsToErrorBag($invalidProperties, $paramName, $field); unset($validateFields[$key]); } } $this->addInvalidParameterErrorsToErrorBag($paramName, $validateFields); } }
public function testGetTransformerReturnsJsonApiTransformer() { $serializer = new JsonApiSerializer(new JsonApiTransformer(new Mapper(HelperMapping::complex()))); $this->assertInstanceOf(JsonApiTransformer::class, $serializer->getTransformer()); }
/** * @param array $relationshipData * @param JsonApiSerializer $serializer * @param ErrorBag $errorBag */ protected static function relationshipDataAssert($relationshipData, JsonApiSerializer $serializer, ErrorBag $errorBag) { //Has type member. if (empty($relationshipData[JsonApiTransformer::TYPE_KEY]) || !is_string($relationshipData[JsonApiTransformer::TYPE_KEY])) { $errorBag[] = new MissingTypeError(); return; } //Provided type value is supported. if (null === $serializer->getTransformer()->getMappingByAlias($relationshipData[JsonApiTransformer::TYPE_KEY])) { $errorBag[] = new InvalidTypeError($relationshipData[JsonApiTransformer::TYPE_KEY]); return; } //Validate if attributes passed in make sense. if (!empty($relationshipData[JsonApiTransformer::ATTRIBUTES_KEY])) { $mapping = $serializer->getTransformer()->getMappingByAlias($relationshipData[JsonApiTransformer::TYPE_KEY]); $properties = str_replace(array_keys($mapping->getAliasedProperties()), array_values($mapping->getAliasedProperties()), $mapping->getProperties()); foreach (array_keys($relationshipData[JsonApiTransformer::ATTRIBUTES_KEY]) as $property) { if (false === in_array($property, $properties, true)) { $errorBag[] = new InvalidAttributeError($property, $relationshipData[JsonApiTransformer::TYPE_KEY]); } } } }
/** * @param JsonApiSerializer $serializer * @param string $className * @param Sorting $sorting * @param ErrorBag $errorBag */ protected static function validateSortParams(JsonApiSerializer $serializer, $className, Sorting $sorting, ErrorBag $errorBag) { if (false === $sorting->isEmpty()) { if ($mapping = $serializer->getTransformer()->getMappingByClassName($className)) { $aliased = (array) $mapping->getAliasedProperties(); $sortsFields = str_replace(array_values($aliased), array_keys($aliased), $sorting->fields()); $invalidProperties = array_diff($sortsFields, $mapping->getProperties()); foreach ($invalidProperties as $extraField) { $errorBag[] = new InvalidSortError($extraField); } } } }
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)); }
/** * @param JsonApiTransformer $transformer * @param Router $router */ public function __construct(JsonApiTransformer $transformer, Router $router) { $this->mapUrls($transformer, $router); parent::__construct($transformer); }
/** * @param array $data * @param JsonApiSerializer $serializer * @param ErrorBag $errorBag * * @throws DataException */ protected static function assertAttributesExists(array $data, JsonApiSerializer $serializer, ErrorBag $errorBag) { $inputAttributes = array_keys($data[JsonApiTransformer::ATTRIBUTES_KEY]); $mapping = $serializer->getTransformer()->getMappingByAlias($data[JsonApiTransformer::TYPE_KEY]); $properties = str_replace(array_keys($mapping->getAliasedProperties()), array_values($mapping->getAliasedProperties()), $mapping->getProperties()); $properties = array_diff($properties, $mapping->getIdProperties()); $properties = array_merge($properties, $mapping->getHiddenProperties()); $hasErrors = false; foreach ($inputAttributes as $property) { if (false === in_array($property, $properties)) { $hasErrors = true; $errorBag[] = new InvalidAttributeError($property, $data[JsonApiTransformer::TYPE_KEY]); } } if ($hasErrors) { throw new DataException(); } }