Esempio n. 1
0
 public function respondCollection($collection, $callback, $resource_key, $meta = [])
 {
     $resource = new Collection($collection, $callback, $resource_key);
     $data = $this->fractal->createData($resource)->toArray();
     $response = array_merge($data, $meta);
     return $this->respond($response);
 }
 /**
  * @param $data
  * @param \League\Fractal\TransformerAbstract $transformer
  * @param \League\Fractal\Pagination\Cursor|int $cursor
  * @param string $resourceKey
  * @return \League\Fractal\Scope
  */
 public function cursorCollection($data, $transformer = null, $cursor = null, $resourceKey = null)
 {
     $transformer = $this->getTransformer($transformer);
     $resource = new Collection($data, $transformer, $resourceKey);
     $resource->setCursor($this->makeCursor($cursor, $data));
     return $this->manager->createData($resource);
 }
 /**
  * @param array $input
  * @return PayloadInterface
  */
 public function __invoke(array $input)
 {
     $employee = $this->userRepository->getOneById($input['id']);
     $shifts = $this->shiftRepository->getByEmployee($employee);
     $shiftsCollection = new Collection($shifts, new ShiftTransformer());
     return $this->payload->withStatus(PayloadInterface::OK)->withOutput($this->fractal->createData($shiftsCollection)->toArray());
 }
 /**
  * @param mixed $resource
  * @param null|string|array $includes
  * @return array
  */
 private function convertResource($resource, $includes = null)
 {
     if ($includes !== null) {
         $this->manager->parseIncludes($includes);
     }
     return $this->manager->createData($resource)->toArray();
 }
Esempio n. 5
0
 /**
  * Returns only the lessons from the
  * current feed from Laracasts in JSON.
  */
 public function lessons()
 {
     $lessons = $this->lissandra->getLessons();
     $resource = new Collection($lessons, new LessonTransformer($lessons));
     $data = $this->fractal->createData($resource)->toArray()['data'];
     return $this->respondWithCORS($data);
 }
Esempio n. 6
0
 protected function createCollectionResponse($collection, $transformer, $resourceKey, $meta = null)
 {
     $resource = new Collection($collection, $transformer, $resourceKey);
     $data = $this->fractal->createData($resource)->toArray();
     $response = array_merge($data, $meta ? $meta : []);
     return $this->createResponse($response);
 }
Esempio n. 7
0
 /**
  * @param $collection
  * @param $callback
  * @param array|string|null $includes
  *
  * @return \Illuminate\Http\JsonResponse
  */
 protected function respondWithCollection($collection, $callback, $includes = null)
 {
     $resource = new Collection($collection, $callback);
     $this->addParseIncludes($includes);
     $rootScope = $this->fractal->createData($resource);
     return $this->respondWithArray($rootScope->toArray());
 }
Esempio n. 8
0
 public function paginatedCollection(Paginator $paginator, $transformer = null, $resourceKey = null)
 {
     $paginator->appends(\Request::query());
     $resource = new Collection($paginator->getCollection(), $this->getTransformer($transformer), $resourceKey);
     $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
     return $this->manager->createData($resource)->toArray();
 }
Esempio n. 9
0
 protected function respondWithPaginate($paginator, $callback)
 {
     $collection = $paginator->getCollection();
     $resource = new Collection($collection, $callback);
     $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
     $rootScope = $this->fractal->createData($resource);
     return $this->respondWithArray($rootScope->toArray());
 }
 public function transform(Recipe $recipe)
 {
     $fractal = new Manager();
     $ingredients = $recipe->ingredients;
     $categories = $recipe->categories;
     $instructions = $recipe->instructions;
     return ['id' => $recipe->id, 'type' => 'recipe', 'attributes' => ['title' => $recipe->title, 'preparationTime' => $recipe->preparationTime, 'cookingTime' => $recipe->cookingTime], 'relationships' => ['categories' => $fractal->createData($this->collection($categories, new CategoryTransformer()))->toArray(), 'ingredients' => $fractal->createData($this->collection($ingredients, new IngredientTransformer()))->toArray(), 'instructions' => $fractal->createData($this->collection($instructions, new InstructionTransformer()))->toArray()]];
 }
Esempio n. 11
0
 /**
  * @param $data
  *
  * @return array
  */
 public function present($data)
 {
     if ($data instanceof EloquentCollection) {
         $this->resource = $this->transformCollection($data);
     } elseif ($data instanceof AbstractPaginator) {
         $this->resource = $this->transformPaginator($data);
     } else {
         $this->resource = $this->transformItem($data);
     }
     return $this->fractal->createData($this->resource)->toArray();
 }
Esempio n. 12
0
 /**
  * @param Request $request
  * @param array $params
  * @return JsonResponse\Ok
  */
 public function find(Request $request, array $params)
 {
     $post = $this->getPost($params["id"]);
     // if the post is empty, return a 404
     if (empty($post)) {
         return new JsonResponse\NoContent();
     }
     //
     $post = new Item($this->getPost($params["id"]), new PostTransformer(), "posts");
     return new JsonResponse\Ok($this->fractal->createData($post)->toArray());
 }
 public function handle($request, Closure $next)
 {
     $response = $next($request);
     if (!isset($response->original)) {
         return $response;
     }
     if (!is_callable($response)) {
         return $response;
     }
     $response->setContent($this->fractal->createData($response())->toJson());
     return $response;
 }
Esempio n. 14
0
 /**
  * Handle domain logic for an action.
  *
  * @param  array $input
  * @return PayloadInterface
  */
 public function __invoke(array $input)
 {
     //Check that user is authorized to edit this resource
     $this->authorizeUser($input[AuthHandler::TOKEN_ATTRIBUTE]->getMetadata('entity'), 'edit', 'shifts');
     //Validate input
     $inputValidator = v::key('break', v::floatVal())->key('start_time', v::stringType())->key('end_time', v::stringType())->key('id', v::intVal());
     $inputValidator->assert($input);
     //Update shift data
     $shift = $this->commandBus->handle(new UpdateShiftCommand($input['id'], $input['break'], $input['start_time'], $input['end_time']));
     $shiftItem = new Item($shift, new ShiftTransformer());
     return $this->payload->withStatus(PayloadInterface::OK)->withOutput($this->fractal->createData($shiftItem)->toArray());
 }
 /**
  * Create Ember Data friendly response
  *
  * @param $data
  * @param $model
  * @param array $includes
  * @return array
  * @throws Exceptions\SerializerTranslatorException
  */
 public function make($data, $model, $includes = [])
 {
     if (!$data || empty($data)) {
         return [];
     }
     $transformer = app($this->translator->toTranslatorHandler($model));
     $resource = $this->createResource($data, $transformer, $model);
     if (!empty($includes)) {
         $this->manager->parseIncludes($includes);
     }
     return $this->manager->createData($resource)->toArray();
 }
Esempio n. 16
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());
 }
Esempio n. 17
0
 /**
  * Handle domain logic for an action.
  *
  * @param  array $input
  * @return PayloadInterface
  * @throws UserNotAuthorized
  */
 public function __invoke(array $input)
 {
     //Make sure requested user matches auth user
     //todo: figure out if managers can access all employees' hours
     if ($input['id'] != $input[AuthHandler::TOKEN_ATTRIBUTE]->getMetaData('id')) {
         throw new UserNotAuthorized();
     }
     //Get hours and transform to more readable collection
     $employee = $this->userRepository->getOneByIdOrFail($input['id']);
     $hours = $this->shiftRepository->getHoursCountGroupedByWeekFor($employee);
     $this->collection->setData($hours)->setTransformer($this->hoursTransformer);
     return $this->payload->withStatus(PayloadInterface::OK)->withOutput($this->fractal->createData($this->collection)->toArray());
 }
 /**
  * Transform a response with a transformer.
  *
  * @param  string|object  $response
  * @param  object  $transformer
  * @return array
  */
 public function transformResponse($response, $transformer)
 {
     $this->parseFractalIncludes();
     $resource = $this->createResource($response, $transformer);
     // If the response is a paginator then we'll create a new paginator
     // adapter for Laravel and set the paginator instance on our
     // collection resource.
     if ($response instanceof IlluminatePaginator) {
         $paginator = $this->createPaginatorAdapter($response);
         $resource->setPaginator($paginator);
     }
     return $this->fractal->createData($resource)->toArray();
 }
Esempio n. 19
0
 protected function transformData($data)
 {
     if (array_key_exists('data', $data)) {
         throw new \Exception('.');
     } else {
         $manager = new Fractal\Manager();
         $manager->setSerializer(new Fractal\Serializer\ArraySerializer());
         if (is_array($data)) {
             return $manager->createData(new Fractal\Resource\Collection($data, $this->transformer))->toArray();
         }
         return $manager->createData(new Fractal\Resource\Item($data, $this->transformer))->toArray();
     }
 }
 /**
  * Transform data to array
  * @param mixed $data
  * @return array
  */
 public function toArray($data)
 {
     $fractal = new Manager();
     $fractal->setSerializer(new ArraySerializer());
     if (is_array($data)) {
         $response = [];
         foreach ($data as $row) {
             $item = new Item($row, new ResponseTransformer());
             $response[] = $fractal->createData($item)->toArray();
         }
         return $response;
     }
     $item = new Item($data, new ResponseTransformer());
     return $fractal->createData($item)->toArray();
 }
 /**
  * @param Manager $fractal
  * @param FellowTransformer $fellowTransformer
  * @return \Illuminate\Http\JsonResponse
  */
 public function index(Manager $fractal, FellowTransformer $fellowTransformer)
 {
     $fellows = $this->fellow->with(['group.tutor'])->get();
     $collection = new Collection($fellows, $fellowTransformer);
     $data = $fractal->createData($collection)->toArray();
     return $this->respond($data);
 }
Esempio n. 22
0
 public static function listing()
 {
     $fractal = new Manager();
     $data = new Collection(parent::all(), new GenreTransformer());
     $list = $fractal->createData($data)->toArray()['data'];
     return $list;
 }
Esempio n. 23
0
/**
 * @VP:
 * I'm trying to pass a second parameter to my transformer but don't know how.
 * @param $resource
 */
function transform($resource)
{
    $manager = new Manager();
    $manager->setSerializer(new DataArraySerializer());
    $manager->parseIncludes(request()->get('includes', []));
    return $manager->createData($resource)->toArray();
}
Esempio n. 24
0
 /**
  * @param Manager $fractal
  * @param PlaceTransformer $placeTransformer
  * @param $placeId
  * @return \Illuminate\Http\JsonResponse
  */
 public function getCheckins(Manager $fractal, PlaceTransformer $placeTransformer, $placeId)
 {
     $place = $this->place->with(['checkins'])->get();
     $collection = new Collection($place, $placeTransformer);
     $data = $fractal->createData($collection)->toArray();
     return $this->respond($data);
 }
Esempio n. 25
0
 /**
  *  Serializes the original content
  *
  *  @param Fractal\ResourceAbstract $resource
  *  @return $this
  */
 protected function serialize($resource)
 {
     $manager = new Fractal\Manager();
     $data = $manager->createData($resource)->toArray()['data'];
     $serialized = json_encode($data);
     return parent::setContent($serialized);
 }
Esempio n. 26
0
 public function byLoan($id, Manager $fractal, LoancropTransformer $loancropTransformer)
 {
     $loancrops = Loancrop::where('loan_id', $id)->get();
     $collection = new Collection($loancrops, $loancropTransformer);
     $data = $fractal->createData($collection)->toArray();
     return $this->respond($data);
 }
 /**
  * Transforms content wrapped in a transform container.
  *
  * @param TransformContainerInterface $container
  * @return mixed
  */
 protected function transformContent(TransformContainerInterface $container)
 {
     if (null === $container->getTransformer()) {
         return $container->getContent();
     }
     $transformer = $this->makeTransformerInstance($container->getTransformer());
     $content = $container->getContent();
     if ($container->isCollection()) {
         // Detect pagination and convert apply it for Fractal
         $paginator = null;
         if ($content instanceof Paginator) {
             $paginator = $content;
             $content = $content->items();
         }
         /** @var FractalCollection $resource */
         $resource = app(FractalCollection::class, [$content, $transformer]);
         if ($paginator) {
             $resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
         }
     } else {
         /** @var FractalItem $resource */
         $resource = app(FractalItem::class, [$content, $transformer]);
     }
     return $this->fractalManager->createData($resource)->toArray();
 }
 /**
  * Include Content
  *
  * @param BaseElementModel $element Element
  *
  * @return League\Fractal\Resource\Item Content
  */
 public function includeContent(BaseElementModel $element)
 {
     $content = [];
     if ($this->depth > \Craft\craft()->config->get('contentRecursionLimit', 'httpMessagesRestMiddleware') - 1) {
         return;
     }
     foreach ($element->getFieldLayout()->getFields() as $fieldLayoutField) {
         $field = $fieldLayoutField->getField();
         $value = $element->getFieldValue($field->handle);
         if (get_class($field->getFieldType()) === 'Craft\\RichTextFieldType') {
             $value = $value->getRawContent();
         }
         if (is_object($value) && get_class($value) === 'Craft\\ElementCriteriaModel') {
             $class = get_class($value->getElementType());
             $element_type = $this->element_service->getElementTypeByClass($class);
             $manager = new Manager();
             $manager->parseIncludes(array_merge(['content'], explode(',', \Craft\craft()->request->getParam('include'))));
             $manager->setSerializer(new ArraySerializer());
             $transformer = $this->config_service->getTransformer($element_type);
             $value = $value->find();
             $body = new Collection($value, new $transformer($this->depth + 1));
             $value = $manager->createData($body)->toArray();
             $value = !empty($value['data']) ? $value['data'] : null;
         }
         $content[$field->handle] = $value;
     }
     if ($content) {
         return $this->item($content, new ContentTransformer($this->depth), 'content');
     }
 }
Esempio n. 29
0
 public function respondWithCollection($collection, $callback)
 {
     $fractal = new Manager();
     $resource = new Collection($collection, $callback);
     $rootScope = $fractal->createData($resource);
     return $rootScope->toArray()['data'];
 }
Esempio n. 30
0
 /**
  * @param Manager $fractal
  * @param UserTransformer $projectTransformer
  * @return mixed
  */
 public function index(Manager $fractal, UserTransformer $projectTransformer)
 {
     $projects = $this->user->with(['posts'])->get();
     $collection = new Collection($projects, $projectTransformer);
     $data = $fractal->createData($collection)->toArray();
     return $this->respondWithCORS($data);
 }