Skip to content

Commit

Permalink
Set transformer and model, add traits
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Feb 22, 2017
1 parent 13184f1 commit ab3a3b1
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 18 deletions.
58 changes: 40 additions & 18 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@ public function __construct()
$this->fractal = App::make(Fractal::class);

$this->parseIncludes();

$this->resourceKey = null;
}

/**
* Sets the fractal transformer
*
* @return mixed
*/
public function setTransformer($transformer) {
$this->transformer = $transformer;
return $this;
}

/**
* Sets model builder
*
* @return mixed
*/
public function setModel($model) {
$this->model = $model;
return $this;
}

/**
* Sets resource key for fractal
*
* @return mixed
*/
public function setResourceKey($resourceKey) {
$this->resourceKey = $resourceKey;
return $this;
}

/**
Expand Down Expand Up @@ -65,12 +97,6 @@ protected function setStatusCode($statusCode)
return $this;
}

/**
* Eager load anything that needs to be included
*
* @param Eloquent Builder
* @return $builder
*/
private function prepareBuilder($builder)
{
$model = $builder ?: $this->model;
Expand All @@ -86,17 +112,16 @@ private function prepareBuilder($builder)
*
* @param $item
* @param null $callback
* @param null $resourceKey
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithItem($item, $callback = null, $resourceKey = false)
protected function respondWithItem($item, $callback = null)
{
if($callback) {
$builder = $this->prepareBuilder($item);
$item = $callback($builder);
}

$rootScope = $this->fractal->item($item, $this->transformer, $resourceKey);
$rootScope = $this->fractal->item($item, $this->transformer, is_null($this->resourceKey) ? false : $this->resourceKey);

return $this->respondWithArray($rootScope->toArray());
}
Expand All @@ -107,13 +132,12 @@ protected function respondWithItem($item, $callback = null, $resourceKey = false
*
* @param $item
* @param null $callback
* @param null $resourceKey
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithItemCreated($item, $callback = null, $resourceKey = false)
protected function respondWithItemCreated($item, $callback = null)
{
$this->setStatusCode(201);
$rootScope = $this->fractal->item($item, $callback, $resourceKey);
$rootScope = $this->fractal->item($item, $callback, $this->resourceKey);

return $this->respondWithArray($rootScope->toArray());
}
Expand All @@ -124,12 +148,11 @@ protected function respondWithItemCreated($item, $callback = null, $resourceKey
*
* @param $collection
* @param $callback
* @param null $resourceKey
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithCollection($collection, $callback, $resourceKey = false)
protected function respondWithCollection($collection, $callback)
{
$rootScope = $this->fractal->collection($collection, $callback, $resourceKey);
$rootScope = $this->fractal->collection($collection, $callback, $this->resourceKey);

return $this->respondWithArray($rootScope->toArray());
}
Expand All @@ -141,18 +164,17 @@ protected function respondWithCollection($collection, $callback, $resourceKey =
* @param $builder
* @param $callback
* @param int $perPage
* @param null $resourceKey
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithPaginatedCollection($builder = null, $perPage = 10, $resourceKey = null)
protected function respondWithPaginatedCollection($builder = null, $perPage = 10)
{
$builder = $this->prepareBuilder($builder);

$paginator = $builder->paginate($perPage);
$paginator->appends($this->getQueryParameters());

$rootScope = $this->fractal
->collection($paginator->getCollection(), $this->transformer, $resourceKey)
->collection($paginator->getCollection(), $this->transformer, $this->resourceKey)
->paginateWith(new IlluminatePaginatorAdapter($paginator));

return $this->respondWithArray($rootScope->toArray());
Expand Down
20 changes: 20 additions & 0 deletions src/Transmitters/Destroy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace NavJobs\Transmit\Transmitters;

use NavJobs\Transmit\Controller;

trait Destroy
{
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$this->model->findOrFail($id)->delete();
return $this->respondWithNoContent();
}
}
19 changes: 19 additions & 0 deletions src/Transmitters/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace NavJobs\Transmit\Transmitters;

use NavJobs\Transmit\Controller;

trait Index
{
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function index()
{
return $this->respondWithPaginatedCollection($this->model);
}
}
21 changes: 21 additions & 0 deletions src/Transmitters/Show.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace NavJobs\Transmit\Transmitters;

use NavJobs\Transmit\Controller;

trait Show
{
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
return $this->respondWithItem($this->model, function ($model) use ($id) {
return $model->findOrFail($id);
});
}
}
20 changes: 20 additions & 0 deletions src/Transmitters/Store.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace NavJobs\Transmit\Transmitters;

use NavJobs\Transmit\Controller;

trait Store
{
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store()
{
$item = $this->model->create(request()->all());
return $this->respondWithItem($item);
}
}
26 changes: 26 additions & 0 deletions src/Transmitters/Update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace NavJobs\Transmit\Transmitters;

use NavJobs\Transmit\Controller;
use Illuminate\Http\Request;

trait Update
{
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update($id, Request $request)
{
return $this->respondWithItem($this->model, function ($model) use ($id, $request) {
$item = $model->findOrFail($id);
$item->fill($request->all());
$item->save();
return $item;
});
}
}

0 comments on commit ab3a3b1

Please sign in to comment.