Example #1
0
 /**
  * Respond with a paginator, and a transformer.
  *
  * @param LengthAwarePaginator $paginator
  * @param callable|\League\Fractal\TransformerAbstract $transformer
  * @param string $resourceKey
  * @param array $meta
  * @return ResponseFactory
  */
 public function withPaginator(LengthAwarePaginator $paginator, $transformer, $resourceKey = null, $meta = [])
 {
     $resource = new Collection($paginator->items(), $transformer, $resourceKey);
     $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
     foreach ($meta as $metaKey => $metaValue) {
         $resource->setMetaValue($metaKey, $metaValue);
     }
     $rootScope = $this->manager->createData($resource);
     return $this->withArray($rootScope->toArray());
 }
 public function testSerializingCollectionResourceWithMeta()
 {
     $this->manager->parseIncludes('author');
     $booksData = array(array('title' => 'Foo', 'year' => '1991', '_author' => array('name' => 'Dave')), array('title' => 'Bar', 'year' => '1997', '_author' => array('name' => 'Bob')));
     $resource = new Collection($booksData, new GenericBookTransformer(), 'book');
     $resource->setMetaValue('foo', 'bar');
     $scope = new Scope($this->manager, $resource);
     $expected = array('book' => array(array('title' => 'Foo', 'year' => 1991), array('title' => 'Bar', 'year' => 1997)), 'linked' => array('author' => array(array('name' => 'Dave'), array('name' => 'Bob'))), 'meta' => array('foo' => 'bar'));
     $this->assertEquals($expected, $scope->toArray());
     $expectedJson = '{"book":[{"title":"Foo","year":1991},{"title":"Bar","year":1997}],"linked":{"author":[{"name":"Dave"},{"name":"Bob"}]},"meta":{"foo":"bar"}}';
     $this->assertEquals($expectedJson, $scope->toJson());
 }
 public function collection($rootname, $items, TransformerAbstract $transformer, $options = array())
 {
     $resources = new Collection($items, $transformer, $rootname);
     if (isset($options['meta'])) {
         foreach ($options['meta'] as $metaKey => $metaItem) {
             $resources->setMetaValue($metaKey, $metaItem);
         }
     }
     if (array_key_exists('cursor', $options)) {
         $resources->setCursor($options['cursor']);
     }
     if (array_key_exists('callback', $options)) {
         call_user_func($options['callback'], $resources);
     }
     if ($items instanceof IlluminatePaginator) {
         $paginatorInterface = array_key_exists('paginatorInterface', $options) ? $options['paginatorInterface'] : null;
         $this->paginateCollection($resources, $items, $paginatorInterface);
     }
     return isset($options['raw']) ? $resources : $this->buildResponse($resources);
 }
 public function testSerializingCollectionResource()
 {
     $manager = new Manager();
     $manager->parseIncludes('author');
     $manager->setSerializer(new DataArraySerializer());
     $booksData = array(array('title' => 'Foo', 'year' => '1991', '_author' => array('name' => 'Dave')), array('title' => 'Bar', 'year' => '1997', '_author' => array('name' => 'Bob')));
     // Try without metadata
     $resource = new Collection($booksData, new GenericBookTransformer(), 'book');
     $scope = new Scope($manager, $resource);
     $expected = array('data' => array(array('title' => 'Foo', 'year' => 1991, 'author' => array('data' => array('name' => 'Dave'))), array('title' => 'Bar', 'year' => 1997, 'author' => array('data' => array('name' => 'Bob')))));
     $this->assertEquals($expected, $scope->toArray());
     $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"data":{"name":"Dave"}}},{"title":"Bar","year":1997,"author":{"data":{"name":"Bob"}}}]}';
     $this->assertEquals($expectedJson, $scope->toJson());
     // Same again with meta
     $resource = new Collection($booksData, new GenericBookTransformer(), 'book');
     $resource->setMetaValue('foo', 'bar');
     $scope = new Scope($manager, $resource);
     $expected = array('meta' => array('foo' => 'bar'), 'data' => array(array('title' => 'Foo', 'year' => 1991, 'author' => array('data' => array('name' => 'Dave'))), array('title' => 'Bar', 'year' => 1997, 'author' => array('data' => array('name' => 'Bob')))));
     $this->assertEquals($expected, $scope->toArray());
     $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"data":{"name":"Dave"}}},{"title":"Bar","year":1997,"author":{"data":{"name":"Bob"}}}],"meta":{"foo":"bar"}}';
     $this->assertEquals($expectedJson, $scope->toJson());
 }
 public function testSerializingCollectionResource()
 {
     $manager = new Manager();
     $manager->parseIncludes('author');
     $manager->setSerializer(new ArraySerializer());
     $booksData = array(array('title' => 'Foo', 'year' => '1991', '_author' => array('name' => 'Dave')), array('title' => 'Bar', 'year' => '1997', '_author' => array('name' => 'Bob')));
     $resource = new Collection($booksData, new GenericBookTransformer(), 'book');
     // Try without metadata
     $scope = new Scope($manager, $resource);
     $expected = array(array('title' => 'Foo', 'year' => 1991, 'author' => array('name' => 'Dave')), array('title' => 'Bar', 'year' => 1997, 'author' => array('name' => 'Bob')));
     $this->assertEquals($expected, $scope->toArray());
     // JSON array of JSON objects
     $expectedJson = '[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}]';
     $this->assertEquals($expectedJson, $scope->toJson());
     // Same again with metadata
     $resource->setMetaValue('foo', 'bar');
     $scope = new Scope($manager, $resource);
     $expected = array(array('title' => 'Foo', 'year' => 1991, 'author' => array('name' => 'Dave')), array('title' => 'Bar', 'year' => 1997, 'author' => array('name' => 'Bob')), 'meta' => array('foo' => 'bar'));
     $this->assertEquals($expected, $scope->toArray());
     // This JSON sucks, because when you add a string key then it has to string up all the other keys. Using meta in Array is shit
     $expectedJson = '{"0":{"title":"Foo","year":1991,"author":{"name":"Dave"}},"1":{"title":"Bar","year":1997,"author":{"name":"Bob"}},"meta":{"foo":"bar"}}';
     $this->assertEquals($expectedJson, $scope->toJson());
 }
Example #6
0
 /**
  * Response for collection of items
  *
  * @param $data
  * @param callable|\League\Fractal\TransformerAbstract $transformer
  * @param string $resourceKey
  * @param Cursor $cursor
  * @param array $meta
  * @return mixed
  */
 public function withCollection($data, $transformer, $resourceKey = null, Cursor $cursor = null, $meta = [])
 {
     $resource = new Collection($data, $transformer, $resourceKey);
     foreach ($meta as $metaKey => $metaValue) {
         $resource->setMetaValue($metaKey, $metaValue);
     }
     if (!is_null($cursor)) {
         $resource->setCursor($cursor);
     }
     $rootScope = $this->manager->createData($resource);
     return $this->withArray($rootScope->toArray());
 }
Example #7
0
 /**
  * Prepare root scope and set some meta information.
  *
  * @param Item|Collection $resource
  *
  * @return \League\Fractal\Scope
  */
 protected function prepareRootScope($resource)
 {
     $resource->setMetaValue('available_includes', $this->transformer->getAvailableIncludes());
     $resource->setMetaValue('default_includes', $this->transformer->getDefaultIncludes());
     return $this->fractal->createData($resource);
 }