public function itemData($item, TransformerAbstract $transformer, $options = array())
 {
     $resource = new Item($item, $transformer);
     if (isset($options['meta'])) {
         foreach ($options['meta'] as $metaKey => $metaItem) {
             $resource->setMetaValue($metaKey, $metaItem);
         }
     }
     return $resource->getData();
 }
 public function testSerializingItemResourceWithMeta()
 {
     $this->manager->parseIncludes('author');
     $bookData = array('title' => 'Foo', 'year' => '1991', '_author' => array('name' => 'Dave'));
     $resource = new Item($bookData, new GenericBookTransformer(), 'book');
     $resource->setMetaValue('foo', 'bar');
     $scope = new Scope($this->manager, $resource);
     $expected = array('book' => array(array('title' => 'Foo', 'year' => 1991)), 'linked' => array('author' => array(array('name' => 'Dave'))), 'meta' => array('foo' => 'bar'));
     $this->assertEquals($expected, $scope->toArray());
     $expectedJson = '{"book":[{"title":"Foo","year":1991}],"linked":{"author":[{"name":"Dave"}]},"meta":{"foo":"bar"}}';
     $this->assertEquals($expectedJson, $scope->toJson());
 }
 public function testSerializingItemResource()
 {
     $manager = new Manager();
     $manager->parseIncludes('author');
     $manager->setSerializer(new ArraySerializer());
     $bookData = array('title' => 'Foo', 'year' => '1991', '_author' => array('name' => 'Dave'));
     $resource = new Item($bookData, new GenericBookTransformer(), 'book');
     // Try without metadata
     $scope = new Scope($manager, $resource);
     $expected = array('title' => 'Foo', 'year' => 1991, 'author' => array('name' => 'Dave'));
     $this->assertEquals($expected, $scope->toArray());
     // Same again with meta
     $resource->setMetaValue('foo', 'bar');
     $scope = new Scope($manager, $resource);
     $expected = array('title' => 'Foo', 'year' => 1991, 'author' => array('name' => 'Dave'), 'meta' => array('foo' => 'bar'));
     $this->assertEquals($expected, $scope->toArray());
 }
Exemplo n.º 4
0
 /**
  * Response for one item
  *
  * @param $data
  * @param callable|\League\Fractal\TransformerAbstract $transformer
  * @param string $resourceKey
  * @param array $meta
  * @return mixed
  */
 public function withItem($data, $transformer, $resourceKey = null, $meta = [])
 {
     $resource = new Item($data, $transformer, $resourceKey);
     foreach ($meta as $metaKey => $metaValue) {
         $resource->setMetaValue($metaKey, $metaValue);
     }
     $rootScope = $this->manager->createData($resource);
     return $this->withArray($rootScope->toArray());
 }
Exemplo n.º 5
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);
 }