public function __construct(SerializerAbstract $serializer, Request $request)
 {
     $this->request = $request;
     $this->manager = new Manager();
     $this->manager->setSerializer($serializer);
     $this->parseIncludes($this->request->get('include'));
 }
 /**
  * @return Manager $fractal
  */
 public function getFractal()
 {
     if (!isset($this->fractal)) {
         $this->fractal = new Manager();
         $this->fractal->setSerializer(new ArraySerializer());
     }
     return $this->fractal;
 }
Example #3
0
 /**
  * ApiController constructor.
  *
  * @param \League\Fractal\Manager $fractal
  */
 public function __construct(Manager $fractal)
 {
     if (config('crawler.api.use_auth')) {
         $this->middleware('Hedii\\Crawler\\Api\\Middleware\\ErrorIfNotCorrectUser');
     }
     $this->fractal = $fractal;
     $this->fractal->setSerializer(new ArraySerializer());
 }
Example #4
0
 /**
  * @return $this
  */
 protected function setupSerializer()
 {
     $serializer = $this->serializer();
     if ($serializer instanceof SerializerAbstract) {
         $this->fractal->setSerializer($serializer);
     }
     return $this;
 }
Example #5
0
 /**
  * @param Manager         $fractal
  * @param ResponseFactory $response
  * @param RequestParams   $requestParams
  */
 public function __construct(Manager $fractal, RequestParams $requestParams)
 {
     $this->fractal = $fractal;
     $this->requestParams = $requestParams;
     $this->fractal->setSerializer(new JsonApiSerializer());
     if (isset($_GET['include'])) {
         $fractal->parseIncludes($_GET['include']);
     }
 }
Example #6
0
 /**
  * @return FractalFactory
  */
 protected function getFractalFactory()
 {
     if (!$this->fractal) {
         $this->fractal = new LeagueFractalFactory();
         $serializer = $this->app['config']->get('warehouse.fractal.serializer', null);
         if (!empty($serializer)) {
             $this->fractal->setSerializer($this->app->make($serializer));
         }
     }
     return $this->fractal;
 }
 /**
  * Constructor.
  *
  * @param Request $request
  */
 public function __construct(Request $request)
 {
     $this->model = $this->model();
     $this->transformer = $this->transformer();
     $this->fractal = new Manager();
     $this->request = $request;
     $this->fractal->setSerializer($this->serializer());
     if ($this->request->has('include')) {
         $this->fractal->parseIncludes(camel_case($this->request->input('include')));
     }
 }
 public function index(Manager $fractal, Request $request, ApiKeyTransformer $apiKeyTransformer)
 {
     $apiKeys = $request->user()->apiKeys;
     $collection = new Collection($apiKeys, $apiKeyTransformer);
     $data = $fractal->setSerializer(new ArraySerializer())->createData($collection)->toArray();
     return $this->respond($data);
 }
 public function show(Manager $fractal, Request $request, UserTransformer $userTransformer)
 {
     $user = $request->user();
     $item = new Item($user, $userTransformer);
     $data = $fractal->setSerializer(new ArraySerializer())->createData($item)->toArray();
     return $this->respond($data);
 }
Example #10
0
 public function __construct(array $values = [])
 {
     parent::__construct();
     $app = $this;
     $this['release.dir'] = __DIR__ . '/../releases';
     $this['finder'] = function () {
         $finder = new Finder();
         return $finder->files()->in($this['release.dir']);
     };
     $this['parser'] = function () use($app) {
         return new ReleaseParser($app['finder']);
     };
     $this['fractal'] = function () {
         $fractal = new Manager();
         $fractal->setSerializer(new PackageSerializer());
         return $fractal;
     };
     $this['transformer'] = function () use($app) {
         /** @var Request $request */
         $request = $app['request'];
         $transformer = new ReleaseTransformer();
         $transformer->setHost($request->getHttpHost());
         return $transformer;
     };
     $this->get('/', 'Rarst\\ReleaseBelt\\Controller::getHtml');
     $this->get('/packages.json', 'Rarst\\ReleaseBelt\\Controller::getJson');
     $this->get('/{vendor}/{file}', 'Rarst\\ReleaseBelt\\Controller::getFile');
     foreach ($values as $key => $value) {
         $this[$key] = $value;
     }
 }
Example #11
0
 /**
  * @param ResourceAbstract $resource
  * @return SymfonyResponse
  */
 protected function createFractalResponse(ResourceAbstract $resource)
 {
     $fractal = new Manager();
     $fractal->setSerializer(new ArraySerializer());
     $data = $fractal->createData($resource)->toArray();
     return $this->createJsonResponse($data);
 }
 /**
  * Register the service provider.
  *
  * @return void
  */
 public function register()
 {
     if ($this->isLumen()) {
         require_once 'fallback.php';
     }
     $this->registerRequiredProviders();
     $this->app->bind('datatables.html', function () {
         return $this->app->make(Html\Builder::class);
     });
     $this->app->singleton('datatables.fractal', function () {
         $fractal = new Manager();
         $config = $this->app['config'];
         $request = $this->app['request'];
         $includesKey = $config->get('datatables.fractal.includes', 'include');
         if ($request->get($includesKey)) {
             $fractal->parseIncludes($request->get($includesKey));
         }
         $serializer = $config->get('datatables.fractal.serializer', DataArraySerializer::class);
         $fractal->setSerializer(new $serializer());
         return $fractal;
     });
     $this->app->singleton('datatables', function () {
         return new Datatables($this->app->make(Request::class));
     });
     $this->registerAliases();
 }
Example #13
0
 /**
  * @param $resource
  * @return mixed
  */
 public function responseWithTransformer($resource, $code)
 {
     $manager = new Manager();
     $manager->setSerializer(new DataArraySerializer());
     $manager->parseIncludes(request()->get('includes', []));
     return response()->json($manager->createData($resource)->toArray(), $code);
 }
 /**
  * Register the service provider.
  *
  * @return void
  */
 public function register()
 {
     $source_config = __DIR__ . '/../../config/fractal.php';
     $this->mergeConfigFrom($source_config, 'fractal');
     $this->app->singleton('fractal', function ($app) {
         // retrieves configurations
         $autoload = $app['config']->get('fractal.autoload');
         $input_key = $app['config']->get('fractal.input_key');
         $exclude_key = $app['config']->get('fractal.exclude_key');
         $serializer = $app['config']->get('fractal.serializer');
         // creating fractal manager instance
         $manager = new Manager();
         $factalNamespace = 'League\\Fractal\\Serializer\\';
         $loadSerializer = class_exists($factalNamespace . $serializer) ? $factalNamespace . $serializer : $serializer;
         $manager->setSerializer(new $loadSerializer());
         if ($autoload === true and $includes = $app['request']->input($input_key)) {
             $manager->parseIncludes($includes);
         }
         if ($app['request']->has($exclude_key)) {
             $manager->parseExcludes($app['request']->input($exclude_key));
         }
         return new FractalServices($manager, $app['app']);
     });
     $this->app->alias('fractal', FractalServices::class);
     // register our command here
     $this->app['command.transformer.generate'] = $this->app->share(function ($app) {
         return new TransformerGeneratorCommand($app['config'], $app['view'], $app['files'], $app);
     });
     $this->commands('command.transformer.generate');
 }
Example #15
0
 public function createResponseArray($data, $entity, Request $request)
 {
     $this->request = $request;
     $fractal = new Manager();
     $fractal->parseIncludes($request->query->get('include', []));
     $fractal->setSerializer(new ArraySerializer());
     $transformer = $this->transformerFactory->get($entity);
     if ($data instanceof Pagerfanta) {
         $pager = $data;
         $pager->setMaxPerPage($request->query->get('limit', 10));
         $pager->setCurrentPage($request->query->get('page', 1));
         $results = $pager->getCurrentPageResults();
         $resource = new Collection($results, $transformer);
         $resource->setPaginator(new PagerfantaPaginatorAdapter($pager, [$this, 'paginationRouter']));
     } elseif ($data instanceof DoctrineQuery) {
         $ormAdapter = new DoctrineORMAdapter($data);
         $pager = new Pagerfanta($ormAdapter);
         $pager->setMaxPerPage($request->query->get('limit', 10));
         $pager->setCurrentPage($request->query->get('page', 1));
         $results = $pager->getCurrentPageResults();
         $resource = new Collection($results, $transformer);
         $resource->setPaginator(new PagerfantaPaginatorAdapter($pager, [$this, 'paginationRouter']));
     } elseif (is_array($data)) {
         $resource = new Collection($data, $transformer);
     } else {
         $resource = new Item($data, $transformer);
     }
     $data = $fractal->createData($resource)->toArray();
     return $data;
 }
Example #16
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();
}
 /**
  * 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');
     }
 }
 /**
  * Returns the requested elements as JSON
  *
  * @param callable|null $configFactory A function for generating the config
  * @param array|null    $config        The API endpoint configuration
  *
  * @throws Exception
  * @throws HttpException
  */
 public function actionGetElements($configFactory = null, array $config = null)
 {
     if ($configFactory !== null) {
         $params = craft()->urlManager->getRouteParams();
         $variables = isset($params['variables']) ? $params['variables'] : null;
         $config = $this->_callWithParams($configFactory, $variables);
     }
     // Merge in default config options
     $config = array_merge(['paginate' => true, 'pageParam' => 'page', 'elementsPerPage' => 100, 'first' => false, 'transformer' => 'Craft\\ElementApi_ElementTransformer'], craft()->config->get('defaults', 'elementapi'), $config);
     if ($config['pageParam'] == 'p') {
         throw new Exception('The pageParam setting cannot be set to "p" because that’s the parameter Craft uses to check the requested path.');
     }
     if (!isset($config['elementType'])) {
         throw new Exception('Element API configs must specify the elementType.');
     }
     /** @var ElementCriteriaModel $criteria */
     $criteria = craft()->elements->getCriteria($config['elementType'], ['limit' => null]);
     if (!empty($config['criteria'])) {
         $criteria->setAttributes($config['criteria']);
     }
     // Load Fractal
     $pluginPath = craft()->path->getPluginsPath() . 'elementapi/';
     require $pluginPath . 'vendor/autoload.php';
     $fractal = new Manager();
     $fractal->setSerializer(new ArraySerializer());
     // Define the transformer
     if (is_callable($config['transformer']) || $config['transformer'] instanceof TransformerAbstract) {
         $transformer = $config['transformer'];
     } else {
         Craft::import('plugins.elementapi.ElementApi_ElementTransformer');
         $transformer = Craft::createComponent($config['transformer']);
     }
     if ($config['first']) {
         $element = $criteria->first();
         if (!$element) {
             throw new HttpException(404);
         }
         $resource = new Item($element, $transformer);
     } else {
         if ($config['paginate']) {
             // Create the paginator
             require $pluginPath . 'ElementApi_PaginatorAdapter.php';
             $paginator = new ElementApi_PaginatorAdapter($config['elementsPerPage'], $criteria->total(), $config['pageParam']);
             // Fetch this page's elements
             $criteria->offset = $config['elementsPerPage'] * ($paginator->getCurrentPage() - 1);
             $criteria->limit = $config['elementsPerPage'];
             $elements = $criteria->find();
             $paginator->setCount(count($elements));
             $resource = new Collection($elements, $transformer);
             $resource->setPaginator($paginator);
         } else {
             $resource = new Collection($criteria, $transformer);
         }
     }
     JsonHelper::sendJsonHeaders();
     echo $fractal->createData($resource)->toJson();
     // End the request
     craft()->end();
 }
 public function getAll(Manager $fractal)
 {
     $fractal->setSerializer(new JsonApiSerializer());
     $categoriaTecido = $this->repository->with(['tecidos'])->all();
     $collection = new Collection($categoriaTecido, $this->categoriaTecidoTransformer);
     $data = $fractal->createData($collection)->toArray();
     return $this->respondWithCORS($data);
 }
Example #20
0
 /**
  * @param Manager $fractal
  * @param SilkTransformer $silkTransformer
  * @return mixed
  */
 public function index(Manager $fractal, SilkTransformer $silkTransformer)
 {
     $fractal->setSerializer(new JsonApiSerializer());
     $projects = $this->repository->with(['camisetas'])->all();
     $collection = new Collection($projects, $silkTransformer);
     $data = $fractal->createData($collection)->toArray();
     return $this->respondWithCORS($data);
 }
 /**
  * Register the service provider.
  *
  * @return void
  */
 public function register()
 {
     $this->app->bindShared('NuWave\\Serializers\\SerializerManager', function ($app) {
         $manager = new Manager();
         $manager->setSerializer(new EmberSerializer());
         return new SerializerManager($manager, new SerializerTranslator());
     });
 }
 /**
  * Creates the scope for this builder.
  *
  * @return Scope
  */
 protected function makeScope()
 {
     $resource = $this->makeResource();
     if (isset($this->serializer)) {
         $this->fractal->setSerializer($this->serializer);
     }
     return $this->fractal->createData($resource);
 }
Example #23
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);
 }
Example #24
0
 /**
  * Use the register method to register items with the container via the
  * protected $this->container property or the `getContainer` method
  * from the ContainerAwareTrait.
  *
  * @return void
  */
 public function register()
 {
     $manager = new Manager();
     $manager->setSerializer(new JsonApiSerializer());
     if (isset($_GET['include'])) {
         $manager->parseIncludes($_GET['include']);
     }
     $this->getContainer()->add(Manager::class, $manager);
 }
 /**
  * @param Manager $fractal
  * @return mixed
  */
 public function index(Manager $fractal)
 {
     $fractal->setSerializer(new JsonApiSerializer());
     $faq = $this->repository->with(['posts'])->all();
     $collection = new Collection($faq, $this->categoriaBlogTransformer);
     $data = $fractal->createData($collection)->toArray();
     //        return $this->respondWithCORS($data);
     return response()->json($data);
 }
Example #26
0
 /**
  * @param Paginator $data
  * @param TransformerAbstract $transformer
  * @param array $headers
  * @return \Illuminate\Http\JsonResponse
  */
 public function respondWithPaginator(LengthAwarePaginator $data, TransformerAbstract $transformer, $headers = [])
 {
     $manager = new Manager();
     $manager->setSerializer(new ArraySerializer());
     $resource = new Collection($data->getCollection(), $transformer);
     $resource->setPaginator(new IlluminatePaginatorAdapter($data));
     $response = $manager->createData($resource)->toArray();
     return $this->respond(['post' => $this->_request->all(), 'data' => $response['data'], 'meta' => $response['meta'], 'error' => ['global' => '']], $headers);
 }
Example #27
0
 /**
  * For Fractal transformer
  * @param $resource
  * @param null $includes
  * @return array
  */
 public function transform($resource, $includes = null)
 {
     $manager = new Manager();
     $manager->setSerializer(new DataArraySerializer());
     if ($includes) {
         $manager->parseIncludes($includes);
     }
     return $manager->createData($resource)->toArray();
 }
Example #28
0
 /**
  * Register the service provider.
  *
  * @return void
  */
 public function register()
 {
     $this->app->singleton(Fractal::class, function ($app) {
         $fractal = new Fractal();
         $fractal->setSerializer(app($app['config']['fractal']['serializer']));
         return $fractal;
     });
     $this->app->alias(Fractal::class, 'api.provider');
     $this->app->bind('api.response', Response::class);
 }
Example #29
0
 /**
  * @param $id
  * @param Manager $fractal
  * @param TecidoTransformer $tecidoTransformer
  * @return mixed
  */
 public function show($id, Manager $fractal, TecidoTransformer $tecidoTransformer)
 {
     $fractal->setSerializer(new JsonApiSerializer());
     $project = $this->tecido->find($id);
     if (!$project) {
         return Response::json(['error' => ['message' => 'Tecido não foi encontrado, favor procurar outro nome']], 404);
     }
     $item = new Item($project, $tecidoTransformer);
     $data = $fractal->createData($item)->toArray();
     return $this->respond($data);
 }
 /**
  * Constructor.
  *
  * @param Request $request
  */
 public function __construct(Request $request)
 {
     $this->model = $this->model();
     $this->transformer = $this->transformer();
     $this->fractal = new Manager();
     $this->fractal->setSerializer($this->serializer());
     if ($includes = $this->transformer->getAvailableIncludes()) {
         $this->fractal->parseIncludes($includes);
     }
     $this->request = $request;
 }