|
| 1 | +<?php |
| 2 | +namespace Lykegenes\ApiResponse; |
| 3 | + |
| 4 | +use Illuminate\Database\Eloquent\Builder; |
| 5 | +use Illuminate\Database\Eloquent\Model; |
| 6 | +use Illuminate\Http\JsonResponse; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use League\Fractal\Manager; |
| 9 | +use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
| 10 | +use League\Fractal\Resource\Collection; |
| 11 | +use League\Fractal\Resource\Item; |
| 12 | + |
| 13 | +class ApiResponse |
| 14 | +{ |
| 15 | + |
| 16 | + /** |
| 17 | + * The Request instance |
| 18 | + * @var Illuminate\Http\Request |
| 19 | + */ |
| 20 | + protected $request; |
| 21 | + |
| 22 | + /** |
| 23 | + * The Fractal Manager instance |
| 24 | + * @var League\Fractal\Manager |
| 25 | + */ |
| 26 | + protected $fractal; |
| 27 | + |
| 28 | + /** |
| 29 | + * Various request parameters |
| 30 | + * @var String |
| 31 | + */ |
| 32 | + protected $page; |
| 33 | + protected $perPage; |
| 34 | + protected $sort; |
| 35 | + protected $order; |
| 36 | + protected $search; |
| 37 | + protected $include; |
| 38 | + |
| 39 | + /** |
| 40 | + * Construct a new ApiResponse instance |
| 41 | + * @param \Illuminate\Http\Request $request The current request instance |
| 42 | + * @param \League\Fractal\Manager $fractal A Fractal manager instance |
| 43 | + */ |
| 44 | + public function __construct(Request $request, Manager $fractal) |
| 45 | + { |
| 46 | + $this->request = $request; |
| 47 | + $this->fractal = $fractal; |
| 48 | + |
| 49 | + $this->parseRequest(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Parse the request parameters |
| 54 | + */ |
| 55 | + protected function parseRequest() |
| 56 | + { |
| 57 | + $this->page = $this->getRequestParameterValue('page', 1); |
| 58 | + $this->perPage = $this->getRequestParameterValue('per_page', 10); |
| 59 | + $this->sort = $this->getRequestParameterValue('sort', null); |
| 60 | + $this->order = $this->getRequestParameterValue('order', 'asc'); |
| 61 | + $this->search = $this->getRequestParameterValue('search', null); |
| 62 | + $this->include = $this->getRequestParameterValue('include', null); |
| 63 | + } |
| 64 | + |
| 65 | + public function make($stuff, $transformer) |
| 66 | + { |
| 67 | + return JsonResponse::create($this->makeArray($stuff, $transformer)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Try to guess what to do with this stuff |
| 72 | + * @param mixed $stuff A Class, a Model instance or a Query Builder instance |
| 73 | + * @param mixed $transformer The Fractal transformer to use |
| 74 | + * @return array [description] |
| 75 | + */ |
| 76 | + public function makeArray($stuff, $transformer) |
| 77 | + { |
| 78 | + if ($stuff instanceof Model) { |
| 79 | + $data = $this->fromModelInstance($stuff, $transformer); |
| 80 | + } elseif ($stuff instanceof Builder) { |
| 81 | + $data = $this->fromQuery($stuff, $transformer); |
| 82 | + } elseif (is_subclass_of($stuff, Model::class)) { |
| 83 | + $data = $this->fromModelClass($stuff, $transformer); |
| 84 | + } elseif (method_exists($stuff, 'toArray')) { |
| 85 | + $data = $stuff->toArray(); |
| 86 | + } else { |
| 87 | + $data = array($stuff); |
| 88 | + } |
| 89 | + |
| 90 | + return $data; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Transform and serialize multiple models from its class |
| 95 | + * @param mixed $model An Eloquent Model class |
| 96 | + * @param mixed $transformer A Fractal Transformer class |
| 97 | + * @return array The transformed and serialized models collection |
| 98 | + */ |
| 99 | + public function fromModelClass($model, $transformer) |
| 100 | + { |
| 101 | + if (is_subclass_of($model, Model::class)) { |
| 102 | + return $this->fromQuery($model::query(), $transformer); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Transform and serialize a single model instance |
| 108 | + * @param Model $model An Eloquent Model instance |
| 109 | + * @param mixed $transformer A Fractal Transformer class |
| 110 | + * @return array The transformed and serialized model |
| 111 | + */ |
| 112 | + public function fromModelInstance($model, $transformer) |
| 113 | + { |
| 114 | + $resource = new Item($model, new $transformer); |
| 115 | + |
| 116 | + if ($this->include != null) { |
| 117 | + $this->fractal->parseIncludes($this->include); |
| 118 | + } |
| 119 | + |
| 120 | + return $this->fractal->createData($resource)->toArray(); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Transform and serialize the results of an Eloquent query |
| 125 | + * @param Builder $query An Eloquent Query instance |
| 126 | + * @param mixed $transformer A Fractal Transformer class |
| 127 | + * @return array The transformed and serialized query results |
| 128 | + */ |
| 129 | + public function fromQuery(Builder $query, $transformer) |
| 130 | + { |
| 131 | + if ($this->search != null && method_exists($query, 'search')) { |
| 132 | + // apply search query |
| 133 | + $query->search($this->search); |
| 134 | + } |
| 135 | + |
| 136 | + if ($this->sort != null) { |
| 137 | + // apply sorting and ordering |
| 138 | + $query->orderBy($this->sort, $this->order); |
| 139 | + } |
| 140 | + |
| 141 | + // paginate the results |
| 142 | + $paginator = $query->paginate($this->perPage); |
| 143 | + |
| 144 | + if ($this->include != null) { |
| 145 | + $this->fractal->parseIncludes($this->include); |
| 146 | + } |
| 147 | + |
| 148 | + $resource = new Collection($paginator->getCollection(), new $transformer); |
| 149 | + $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
| 150 | + |
| 151 | + return $this->fractal->createData($resource)->toArray(); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Get the name of a request parameter from the user's configuration |
| 156 | + * @param String $key This parameter's key |
| 157 | + * @return String This parameter's name |
| 158 | + */ |
| 159 | + private function getRequestParameterName($key) |
| 160 | + { |
| 161 | + return config('api-response.api.parameters.' . $key, $key); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Get the value of a request parameter |
| 166 | + * @param String $key This parameter's key |
| 167 | + * @param mixed $default The default value to return |
| 168 | + * @return mixed This paramter's value |
| 169 | + */ |
| 170 | + private function getRequestParameterValue($key, $default = null) |
| 171 | + { |
| 172 | + return $this->request->input($this->getRequestParameterName($key), $default); |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments