/**
  * @expectedException RuntimeException
  * @expectedExceptionMessage The $resourceKey parameter must be provided when using League\Fractal\Serializer\JsonApiSerializer
  **/
 public function testResourceKeyMissing()
 {
     $this->manager->setSerializer(new JsonApiSerializer());
     $bookData = array('title' => 'Foo', 'year' => '1991');
     $resource = new Item($bookData, new GenericBookTransformer());
     $scope = new Scope($this->manager, $resource);
     $scope->toArray();
 }
Esempio n. 2
0
 /**
  * Create Data.
  *
  * Main method to kick this all off. Make a resource then pass it over, and use toArray()
  *
  * @param ResourceInterface $resource
  * @param string            $scopeIdentifier
  * @param Scope             $parentScopeInstance
  *
  * @return Scope
  */
 public function createData(ResourceInterface $resource, $scopeIdentifier = null, Scope $parentScopeInstance = null)
 {
     $scopeInstance = new Scope($this, $resource, $scopeIdentifier);
     // Update scope history
     if ($parentScopeInstance !== null) {
         // This will be the new children list of parents (parents parents, plus the parent)
         $scopeArray = $parentScopeInstance->getParentScopes();
         $scopeArray[] = $parentScopeInstance->getScopeIdentifier();
         $scopeInstance->setParentScopes($scopeArray);
     }
     return $scopeInstance;
 }
 protected function callIncludeMethod(Scope $scope, $includeName, $data)
 {
     $scopeIdentifier = $scope->getIdentifier($includeName);
     $params = $scope->getManager()->getIncludeParams($scopeIdentifier);
     // Check if the method name actually exists
     $methodName = 'include' . str_replace(' ', '', ucwords(str_replace('_', ' ', str_replace('-', '', $includeName))));
     $resource = call_user_func(array($this, $methodName), $data, $params);
     if ($resource === null) {
         return false;
     }
     if (!$resource instanceof ResourceAbstract) {
         throw new \Exception(sprintf('Invalid return value from %s::%s(). Expected %s, received %s.', __CLASS__, $methodName, 'League\\Fractal\\Resource\\ResourceAbstract', gettype($resource)));
     }
     return $resource;
 }
Esempio n. 4
0
 /**
  * Call method for retrieving a relation. This method overrides Fractal's own
  * [callIncludeMethod] method to load relations directly from your models.
  *
  * @param  Scope  $scope
  * @param  string $includeName
  * @param  mixed  $data
  * @return \League\Fractal\Resource\ResourceInterface|bool
  * @throws \Exception
  */
 protected function callIncludeMethod(Scope $scope, $includeName, $data)
 {
     if ($includeName === 'pivot') {
         return $this->includePivot($data->{$includeName});
     }
     $params = $scope->getManager()->getIncludeParams($scope->getIdentifier($includeName));
     if (method_exists($this, $includeName)) {
         $include = call_user_func([$this, $includeName], $data, $params);
         if ($include instanceof ResourceAbstract) {
             return $include;
         }
         return app(Responder::class)->transform($include)->getResource();
     } else {
         return app(Responder::class)->transform($data->{$includeName})->getResource();
     }
 }
Esempio n. 5
0
 /**
  * Fire the main transformer.
  *
  * @internal
  * @param  callable|\League\Fractal\TransformerAbstract  $transformer
  * @param  mixed  $data
  * @return array
  */
 protected function fireTransformer($transformer, $data)
 {
     if (is_string($transformer) && $this->container->has($transformer)) {
         $transformer = $this->container->get($transformer);
         if ($transformer instanceof ContainerAwareInterface) {
             $transformer->setContainer($this->container);
         }
     }
     return parent::fireTransformer($transformer, $data);
 }
 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());
 }
 /**
  * @param int $options
  *
  * @return string
  */
 public function toJson($options = 0)
 {
     return json_encode($this->scope->toArray(), $options);
 }
Esempio n. 9
0
 public function testDefaultIncludeSuccess()
 {
     $manager = new Manager();
     $manager->setSerializer(new ArraySerializer());
     // Send this stub junk, it has a specific format anyhow
     $resource = new Item(array(), new DefaultIncludeBookTransformer());
     // Try without metadata
     $scope = new Scope($manager, $resource);
     $expected = array('a' => 'b', 'author' => array('c' => 'd'));
     $this->assertEquals($expected, $scope->toArray());
 }
Esempio n. 10
0
 /**
  * Serialize a resource
  *
  * @internal
  *
  * @param SerializerAbstract $serializer
  * @param mixed              $data
  *
  * @return array
  */
 protected function serializeResource(SerializerAbstract $serializer, $data)
 {
     $serializer->setScope($this);
     if (is_array($data)) {
         return parent::serializeResource($serializer, $data);
     }
     return null;
 }
 /**
  * get the transformed json data
  * @return string
  */
 public function getJson()
 {
     return $this->scope->toJson();
 }