Skip to content

Commit

Permalink
Add fields param for filtering attributes
Browse files Browse the repository at this point in the history
Specifically, this param takes effect after all other transformations
are complete. In other words, it's not acting on the model attributes
per se, but rather on the fields displayed in the API.

See thephpleague/fractal#226
  • Loading branch information
IllyaMoskvin committed Sep 22, 2017
1 parent 265f87a commit 082ba7b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
13 changes: 10 additions & 3 deletions app/Http/Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Closure;
Expand Down Expand Up @@ -114,7 +115,9 @@ protected function select( Request $request, Closure $callback )
return $this->respondNotFound();
}

return response()->item($item, new $this->transformer);
$fields = Input::get('fields');

return response()->item($item, new $this->transformer($fields) );

}

Expand Down Expand Up @@ -158,7 +161,9 @@ protected function collect( Request $request, Closure $callback )
// Assumes the inheriting class set model and transformer
$all = $callback( $limit, $id );

return response()->collection($all, new $this->transformer);
$fields = Input::get('fields');

return response()->collection($all, new $this->transformer($fields) );

}

Expand Down Expand Up @@ -192,7 +197,9 @@ protected function showMutliple($ids = '')

$all = $this->find($ids);

return response()->collection($all, new $this->transformer);
$fields = Input::get('fields');

return response()->collection($all, new $this->transformer($fields) );

}

Expand Down
29 changes: 28 additions & 1 deletion app/Http/Transformers/ApiTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ class ApiTransformer extends TransformerAbstract
public $excludeIdsAndTitle = false;
public $excludeDates = false;


/**
* Used for only returning a subset of fields.
* Expects a comma-separated string.
*
* @link https://github.com/thephpleague/fractal/issues/226
*
* @var string
*/
protected $fields;

public function __construct($fields = null)
{
$this->fields = $fields ? explode(',', $fields) : null;
}

/**
* Turn this item object into a generic array.
*
Expand All @@ -21,12 +37,14 @@ class ApiTransformer extends TransformerAbstract
public function transform($item)
{

return array_merge(
$data = array_merge(
$this->transformIdsAndTitle($item),
$this->transformFields($item),
$this->transformDates($item)
);

return $this->filterFields( $data );

}

protected function transformFields($item)
Expand Down Expand Up @@ -78,4 +96,13 @@ protected function transformDates($item)

}

protected function filterFields($data)
{
if (is_null($this->fields)) {
return $data;
}

return array_intersect_key($data, array_flip((array) $this->fields));
}

}

0 comments on commit 082ba7b

Please sign in to comment.