コード例 #1
0
 public function setUp()
 {
     $this->included = new Included();
     $this->included->add('employee');
     $this->included->add('employee.name');
     $this->included->add('order.employee');
 }
コード例 #2
0
 /**
  *
  */
 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)));
 }
コード例 #3
0
ファイル: Request.php プロジェクト: nilportugues/json-api
 /**
  * @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;
 }
コード例 #4
0
    /**
     *
     */
    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));
    }
コード例 #5
0
 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);
 }