/**
  * @param Http\Request\Parameters\Fields $fields
  */
 protected function filterOutResourceFields(Fields $fields)
 {
     if (false === $fields->isEmpty()) {
         foreach ($fields->get() as $type => $properties) {
             foreach ($this->serializationStrategy->getMappings() as $mapping) {
                 if ($mapping->getClassAlias() === $type) {
                     $mapping->setFilterKeys($properties);
                 }
             }
         }
     }
 }
 /**
  *
  */
 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)));
 }
 public function testItCanAssertAndThrowExceptionForInvalidQueryParamValues()
 {
     $fields = new Fields();
     $included = new Included();
     $sorting = new Sorting();
     $errorBag = new ErrorBag();
     $hasError = false;
     try {
         $fields->addField('superhero', 'power');
         QueryObject::assert($this->serializer, $fields, $included, $sorting, $errorBag, Post::class);
     } catch (QueryException $e) {
         $hasError = true;
     }
     $this->assertTrue($hasError);
 }
 /**
  * @param JsonApiSerializer $serializer
  * @param Fields            $fields
  * @param string            $paramName
  */
 protected function validateFieldsQueryParams($serializer, Fields $fields, $paramName)
 {
     if (false === $fields->isEmpty()) {
         $validateFields = $fields->types();
         foreach ($validateFields as $key => $field) {
             $mapping = $serializer->getTransformer()->getMappingByAlias($field);
             if (null !== $mapping) {
                 $properties = $this->getPropertiesFromMapping($mapping);
                 $invalidProperties = array_diff($fields->members($field), $properties);
                 $this->addInvalidParameterMemberErrorsToErrorBag($invalidProperties, $paramName, $field);
                 unset($validateFields[$key]);
             }
         }
         $this->addInvalidParameterErrorsToErrorBag($paramName, $validateFields);
     }
 }
 public function testItCanGetWillReturnErrorResponseBecauseOfInvalidParams()
 {
     $this->fields = new Fields();
     $this->fields->addField('superhero', 'power');
     $this->resource = new GetResource($this->serializer, $this->fields, $this->included);
     $response = $this->resource->get(10, Post::class, $this->findOneCallable);
     $this->assertInstanceOf(Response::class, $response);
     $this->assertEquals(400, $response->getStatusCode());
 }
 public function testItCanGetWillReturnErrorResponseBecauseOfInvalidParams()
 {
     $this->fields = new Fields();
     $this->fields->addField('superhero', 'power');
     $this->resource = new ListResource($this->serializer, $this->page, $this->fields, $this->sorting, $this->included, $this->filters);
     $response = $this->resource->get($this->totalAmountCallable, $this->resultsCallable, $this->routeUri, Post::class);
     $this->assertInstanceOf(Response::class, $response);
     $this->assertEquals(400, $response->getStatusCode());
 }
 /**
  * @param JsonApiSerializer $serializer
  * @param Fields            $fields
  * @param                   $paramName
  * @param ErrorBag          $errorBag
  */
 protected static function validateQueryParamsTypes(JsonApiSerializer $serializer, Fields $fields, $paramName, ErrorBag $errorBag)
 {
     if (false === $fields->isEmpty()) {
         $transformer = $serializer->getTransformer();
         $validateFields = $fields->types();
         foreach ($validateFields as $key => $type) {
             $mapping = $transformer->getMappingByAlias($type);
             if (null !== $mapping) {
                 $members = array_merge(array_combine($mapping->getProperties(), $mapping->getProperties()), $mapping->getAliasedProperties());
                 $invalidMembers = array_diff($fields->members($type), $members);
                 foreach ($invalidMembers as $extraField) {
                     $errorBag[] = new InvalidParameterMemberError($extraField, $type, strtolower($paramName));
                 }
                 unset($validateFields[$key]);
             }
         }
         if (false === empty($validateFields)) {
             foreach ($validateFields as $type) {
                 $errorBag[] = new InvalidParameterError($type, strtolower($paramName));
             }
         }
     }
 }
Beispiel #8
0
 /**
  * @return \NilPortugues\Api\JsonApi\Http\Request\Parameters\Fields
  */
 public function getFields()
 {
     $fields = (array) $this->getQueryParam('fields', null);
     $fields = array_filter($fields);
     $object = new Fields();
     foreach ($fields as $type => &$members) {
         $members = \explode(',', $members);
         $members = \array_map('trim', $members);
         foreach ($members as $member) {
             $object->addField($type, $member);
         }
     }
     return $object;
 }
    /**
     *
     */
    public function testItWillSerializeToJsonApiAComplexObjectAndFilterFields()
    {
        $mappings = HelperMapping::complex();
        $mapper = new Mapper($mappings);
        $expected = <<<JSON
{
   "data":{
      "type":"post",
      "id":"9",
      "attributes":{
         "title":"Hello World"
      },
      "links":{
         "self":{
            "href":"http://example.com/posts/9"
         },
         "comments":{
            "href":"http://example.com/posts/9/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);
        $fields = new Fields();
        $fields->addField('post', 'title');
        $this->assertEquals(\json_decode($expected, true), \json_decode((new JsonApiSerializer($transformer))->serialize($post, $fields), 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 testIsEmpty()
 {
     $this->assertFalse($this->fields->isEmpty());
 }