Skip to content

Commit 22d55a2

Browse files
committed
Moved this to it's own package
Everything’s working!
1 parent 89c7c22 commit 22d55a2

File tree

6 files changed

+266
-25
lines changed

6 files changed

+266
-25
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ Via Composer
1717
composer require lykegenes/laravel-api-response
1818
```
1919

20+
Then, add this to your Service Providers :
21+
``` php
22+
Lykegenes\ApiResponse\ServiceProvider::class,
23+
```
24+
25+
...and this to your Aliases :
26+
``` php
27+
'ApiResponse' => Lykegenes\ApiResponse\Facades\ApiResponse::class,
28+
```
29+
30+
Optionally, you can publish and edit the configuration file :
31+
``` bash
32+
php artisan vendor:publish --provider="Lykegenes\ApiResponse\ServiceProvider" --tag=config
33+
```
34+
2035
## Usage
2136

2237
``` php

config/config.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
return [
4+
5+
'api' => [
6+
'parameters' => [
7+
'page' => 'page',
8+
'per_page' => 'per_page',
9+
'sort' => 'sort',
10+
'order' => 'order',
11+
'search' => 'search',
12+
'include' => 'include',
13+
],
14+
],
15+
16+
];

src/ApiResponse.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
}

src/Facades/ApiResponse.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace Lykegenes\ApiResponse\Facades;
3+
4+
use Illuminate\Support\Facades\Facade;
5+
6+
class ApiResponse extends Facade
7+
{
8+
public static function getFacadeAccessor()
9+
{
10+
return \Lykegenes\ApiResponse\ApiResponse::class;
11+
}
12+
}

src/ServiceProvider.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace Lykegenes\ApiResponse;
3+
4+
class ServiceProvider extends \Illuminate\Support\ServiceProvider
5+
{
6+
7+
/**
8+
* Register the service provider.
9+
*
10+
* @return void
11+
*/
12+
public function register()
13+
{
14+
$configPath = __DIR__ . '/../config/config.php';
15+
$this->mergeConfigFrom($configPath, 'api-response');
16+
17+
/*$this->app->bindShared('datagrid-builder', function ($app) {
18+
19+
return new DatagridBuilder($app, $app['datagrid-helper']);
20+
});*/
21+
}
22+
23+
public function boot()
24+
{
25+
$configPath = __DIR__ . '/../config/config.php';
26+
27+
$this->publishes([$configPath => $this->getConfigPath()], 'config');
28+
}
29+
30+
/**
31+
* @return string[]
32+
*/
33+
public function provides()
34+
{
35+
return ['api-response'];
36+
}
37+
38+
/**
39+
* Get the config path
40+
*
41+
* @return string
42+
*/
43+
protected function getConfigPath()
44+
{
45+
return config_path('api-response.php');
46+
}
47+
48+
}

src/SkeletonClass.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)