/** * */ public function setUp() { $this->included = new Included(); $this->included->add('post.user_comment'); //will cause error $this->fields = new Fields(); $this->fields->addField('post', 'title'); $this->fields->addField('blog', 'post'); //will cause error $mappings = HelperMapping::complex(); $this->serializer = new JsonApiSerializer(new JsonApiTransformer(new Mapper($mappings))); }
/** * @return Included */ public function getIncludedRelationships() { $include = $this->getQueryParam('include', []); $included = new Included(); if (is_string($include) && strlen($include)) { $includeNames = \explode(',', $include); foreach ($includeNames as $relationship) { $included->add($relationship); } } return $included; }
/** * @param Http\Request\Parameters\Included $included */ protected function filterOutIncludedResources(Included $included) { if (false === $included->isEmpty()) { foreach ($included->get() as $resource => $includeData) { foreach ($this->serializationStrategy->getMappings() as $mapping) { $mapping->filteringIncludedResources(true); if (is_array($includeData)) { foreach ($includeData as $subResource) { $this->serializationStrategy->getMappingByAlias($subResource)->addIncludedResource($this->serializationStrategy->getMappingByAlias($resource)->getClassName()); } break; } $mapping->addIncludedResource($this->serializationStrategy->getMappingByAlias($resource)->getClassName()); } } } }
/** * @param JsonApiSerializer $serializer * @param Included $included * @param string $paramName * @param ErrorBag $errorBag */ protected static function validateIncludeParams(JsonApiSerializer $serializer, Included $included, $paramName, ErrorBag $errorBag) { $transformer = $serializer->getTransformer(); foreach ($included->get() as $resource => $data) { if (null == $transformer->getMappingByAlias($resource)) { $errorBag[] = new InvalidParameterError($resource, strtolower($paramName)); continue; } if (is_array($data)) { foreach ($data as $subResource) { if (null == $transformer->getMappingByAlias($subResource)) { $errorBag[] = new InvalidParameterError($subResource, strtolower($paramName)); } } } } }
/** * @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 testItWillSerializeToJsonApiAComplexObjectAndFilterIncludedSpecificResource() { $mappings = HelperMapping::complex(); $mapper = new Mapper($mappings); $expected = <<<JSON { "data":{ "type":"post", "id":"9", "attributes":{ "title":"Hello World", "content":"Your first post", "post_id": 9 }, "links":{ "self":{ "href":"http://example.com/posts/9" }, "comments":{ "href":"http://example.com/posts/9/comments" } }, "relationships":{ "author":{ "links":{ "self":{ "href":"http://example.com/posts/9/relationships/author" }, "related":{ "href":"http://example.com/posts/9/author" } }, "data":{ "type":"user", "id":"1" } } } }, "included":[ { "type":"user", "id":"1", "attributes":{ "name":"Post Author" }, "links":{ "self":{ "href":"http://example.com/users/1" }, "friends":{ "href":"http://example.com/users/1/friends" }, "comments":{ "href":"http://example.com/users/1/comments" } } } ], "links":{ "self":{ "href":"http://example.com/posts/9" }, "comments":{ "href":"http://example.com/posts/9/comments" } }, "meta":{ "author":{ "name":"Nil Portugués Calderó", "email":"*****@*****.**" }, "is_devel":true }, "jsonapi":{ "version":"1.0" } } JSON; $post = HelperFactory::complexPost(); $transformer = new JsonApiTransformer($mapper); $transformer->setMeta(['author' => ['name' => 'Nil Portugués Calderó', 'email' => '*****@*****.**']]); $transformer->addMeta('is_devel', true); $included = new Included(); $included->add('user'); $this->assertEquals(\json_decode($expected, true), \json_decode((new JsonApiSerializer($transformer))->serialize($post, null, $included), true)); }
/** * Build the URL. * * @param string $route * @param int $pageNumber * @param int $pageSize * @param Fields $fields * @param Sorting $sorting * @param Included $included * @param array $filters * * @return string */ protected function pagePaginatedRoute($route, $pageNumber, $pageSize, Fields $fields, Sorting $sorting, Included $included, $filters) { $fieldKeys = []; if (false === $fields->isEmpty()) { $fieldKeys = $fields->get(); foreach ($fieldKeys as &$v) { $v = implode(',', $v); } } $queryParams = urldecode(http_build_query(array_filter(['page' => array_filter(['number' => $pageNumber, 'size' => $pageSize]), 'fields' => $fieldKeys, 'filter' => $filters, 'sort' => $sorting->get(), 'include' => $included->get()]))); $expression = $route[strlen($route) - 1] === '?' || $route[strlen($route) - 1] === '&' ? '%s%s' : '%s?%s'; return sprintf($expression, $route, $queryParams); }
public function testItCanAssertAndThrowExceptionForInvalidIncludeParamsWhenIsResourceExplicit() { $fields = new Fields(); $included = new Included(); $sorting = new Sorting(); $errorBag = new ErrorBag(); $hasError = false; try { $included->add('post.superhero'); $included->add('post.comment'); QueryObject::assert($this->serializer, $fields, $included, $sorting, $errorBag, Post::class); } catch (QueryException $e) { $hasError = true; } $this->assertTrue($hasError); }
public function testIsEmpty() { $this->assertFalse($this->included->isEmpty()); }