Example #1
0
 /**
  * Get the fractal item object
  * 
  * @param   Model $model
  * @return  FractalItem
  */
 protected function getFractalItem($model)
 {
     $transformer = $this->getTransformer();
     $item = new FractalItem($model, $transformer, $this->getResourceKey());
     $item->setMeta($transformer->getMeta($model));
     return $item;
 }
Example #2
0
 /**
  * Manage and finalize the data transformation.
  *
  * @param  \League\Fractal\Resource\Item|\League\Fractal\Resource\Collection  $data
  * @param  int  $code
  * @param  array  $meta
  * @return \Illuminate\Http\JsonResponse
  */
 public function transform($data, $code = 200, $meta = [])
 {
     $data->setMeta($meta);
     $manager = new Manager();
     $manager->setSerializer(new DataArraySerializer());
     $response = $manager->createData($data)->toArray();
     return response()->json($response, 200, [], JSON_UNESCAPED_SLASHES);
 }
 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 testGetTransformer()
 {
     $resource = new Item($this->simpleItem, function () {
     });
     $this->assertTrue(is_callable($resource->getTransformer()));
     $transformer = function () {
     };
     $resource = new Item($this->simpleItem, $transformer);
     $this->assertEquals($resource->getTransformer(), $transformer);
 }
Example #5
0
 /**
  * Handle domain logic for an action.
  *
  * @param  array $input
  * @return PayloadInterface
  */
 public function __invoke(array $input)
 {
     //Check that user is authorized to view this resource
     $this->authorizeUser($input[AuthHandler::TOKEN_ATTRIBUTE]->getMetadata('entity'), 'view', 'users');
     //Validate input
     $inputValidator = v::key('id', v::intVal());
     $inputValidator->assert($input);
     //Get user from repository and transform into resource
     $user = $this->userRepository->getOneByIdOrFail($input['id']);
     $this->item->setData($user)->setTransformer($this->userTransformer);
     return $this->payload->withStatus(PayloadInterface::OK)->withOutput($this->fractal->createData($this->item)->toArray());
 }
 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());
 }
Example #7
0
 /**
  * Handle domain logic for an action.
  *
  * @param array $input
  * @return PayloadInterface
  */
 public function __invoke(array $input)
 {
     //Ensure that the use has permission to create shifts
     $user = $input[AuthHandler::TOKEN_ATTRIBUTE]->getMetadata('entity');
     $this->authorizeUser($user, 'create', 'shifts');
     //If no manager_id is specified in request, default to user creating shift
     if (!array_key_exists('manager_id', $input)) {
         $input['manager_id'] = $user->getId();
     }
     //Validate input
     $inputValidator = v::key('break', v::floatVal())->key('start_time', v::date())->key('end_time', v::date()->min($input['start_time']))->key('manager_id', v::intVal());
     $inputValidator->assert($input);
     //Execute command to create shift
     $shift = $this->commandBus->handle(new CreateShift($input['manager_id'], $input['employee_id'], $input['break'], $input['start_time'], $input['end_time']));
     $this->item->setData($shift)->setTransformer($this->shiftTransformer);
     return $this->payload->withStatus(PayloadInterface::OK)->withOutput($this->fractal->parseIncludes(['manager', 'employee'])->createData($this->item)->toArray());
 }
 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());
 }
Example #9
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());
 }
Example #10
0
 /**
  * Initialize with empty data.
  */
 function __construct()
 {
     parent::__construct(array(), function () {
     });
 }
Example #11
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);
 }
Example #12
0
 /**
  * Create FractalItem payload.
  *
  * @param \Illuminate\Database\Eloquent\Model $model
  * @param null                                $transformer
  * @param string|null                         $resourceKey
  * @return mixed
  */
 public function getItem(EloquentModel $model, $transformer = null, $resourceKey = null)
 {
     $resource = new FractalItem($model, $this->getTransformer($transformer), $this->getResourceKey($resourceKey));
     if ($meta = $this->getMeta()) {
         $resource->setMeta($meta);
         $this->meta = [];
     }
     if ($includes = $this->getIncludes()) {
         $this->fractal->parseIncludes($this->request->input('include'));
         $this->includes = null;
     }
     return $this->fractal->createData($resource)->toArray();
 }
Example #13
0
 public function __construct($data, $transformer, $resourceKey = null)
 {
     // $data = [$data];
     parent::__construct($data, $transformer, $resourceKey);
 }
Example #14
0
 /**
  * Create FractalItem payload.
  *
  * @param \Illuminate\Database\Eloquent\Model $model
  * @param null                                $transformer
  * @param string|null                         $resourceKey
  * @return mixed
  */
 public function getItem(EloquentModel $model, $transformer = null, $resourceKey = null)
 {
     $resource = new FractalItem($model, $this->getTransformer($transformer), $this->getResourceKey($resourceKey));
     if ($meta = $this->getMeta()) {
         $resource->setMeta($meta);
         $this->meta = [];
     }
     return $this->fractal->createData($resource)->toArray();
 }