|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Api\V1; |
|
4
|
|
|
|
|
5
|
|
|
use App\LaravelRestCms\ApiInterface; |
|
6
|
|
|
use \Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController; |
|
7
|
|
|
use \EllipseSynergie\ApiResponse\Laravel\Response; |
|
8
|
|
|
use Illuminate\Database\Eloquent\MassAssignmentException; |
|
9
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
10
|
|
|
use Illuminate\Contracts\Validation\ValidationException; |
|
11
|
|
|
use Illuminate\Foundation\Bus\DispatchesCommands; |
|
12
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
|
13
|
|
|
use Illuminate\Http\Request; |
|
14
|
|
|
use Illuminate\Routing\Controller as BaseController; |
|
15
|
|
|
|
|
16
|
|
|
abstract class ApiController extends ApiGuardController implements ApiInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Laraponse object, which abstracts Fractal |
|
20
|
|
|
* |
|
21
|
|
|
* @var \EllipseSynergie\ApiResponse\Laravel\Response |
|
22
|
|
|
*/ |
|
23
|
|
|
public $response; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The string name of the Eloquent Model |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $modelName; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The Eloquent Model |
|
34
|
|
|
* |
|
35
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $model; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The string name of the Transformer class |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $transformerName; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The name of the collection that is returned in the JSON response |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $collectionName; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The Fractal Manager |
|
55
|
|
|
* |
|
56
|
|
|
* @var \League\Fractal\Manager |
|
57
|
|
|
*/ |
|
58
|
|
|
public $manager; |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Constructor |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->response = \Response::api(); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
if (!empty($this->modelName)) { |
|
69
|
|
|
$this->model = new $this->modelName; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// if no collection name provided, use the model's table name |
|
73
|
|
|
if (empty($this->collectionName)) { |
|
74
|
|
|
$this->collectionName = $this->model->getTable(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// parse includes |
|
78
|
|
|
if (\Input::get('include') != '') { |
|
79
|
|
|
$this->manager = new \League\Fractal\Manager; |
|
80
|
|
|
$this->manager->parseIncludes(explode(',', \Input::get('include'))); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
parent::__construct(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Creates an item |
|
88
|
|
|
* |
|
89
|
|
|
* @param \Illuminate\Http\Request $data |
|
90
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
91
|
|
|
*/ |
|
92
|
|
|
public function create(Request $data = null) |
|
93
|
|
|
{ |
|
94
|
|
|
$data = $data ?: \Input::json(); |
|
95
|
|
|
$json = $data->all(); |
|
96
|
|
|
$json = $this->addAttribution($json); |
|
97
|
|
|
|
|
98
|
|
|
try { |
|
99
|
|
|
$this->model->validate($json); |
|
100
|
|
|
$item = $this->model->create($json); |
|
101
|
|
|
return $this->showByObject($item); |
|
102
|
|
|
|
|
103
|
|
|
} catch (ValidationException $e) { |
|
104
|
|
|
return $this->response->setStatusCode(422)->withError($e->errors()->all(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
|
|
|
|
|
105
|
|
|
} catch (MassAssignmentException $e) { |
|
106
|
|
|
return $this->response->setStatusCode(422)->withError("Cannot mass assign " . $e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
107
|
|
|
} catch (\Exception $e) { |
|
108
|
|
|
return $this->response->setStatusCode(422)->withError($e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Adds created_by and updated_by to the array if the model supports it |
|
114
|
|
|
* |
|
115
|
|
|
* @param array $data |
|
116
|
|
|
* @return array $data |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function addAttribution(array $data) |
|
119
|
|
|
{ |
|
120
|
|
|
if ($this->model->attirbution) { |
|
121
|
|
|
$data['created_by'] = $this->apiKey->user_id; |
|
122
|
|
|
$data['updated_by'] = $this->apiKey->user_id; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $data; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Returns a single item |
|
130
|
|
|
* |
|
131
|
|
|
* @param mixed $id |
|
132
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
133
|
|
|
*/ |
|
134
|
|
|
public function show($id) |
|
135
|
|
|
{ |
|
136
|
|
|
try { |
|
137
|
|
|
|
|
138
|
|
|
if (is_array($id)) { |
|
139
|
|
|
return $this->response->withItem($this->model->where($id)->firstOrFail(), new $this->transformerName); |
|
140
|
|
|
} else { |
|
141
|
|
|
return $this->response->withItem($this->model->findOrFail($id), new $this->transformerName); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
} catch (ModelNotFoundException $e) { |
|
145
|
|
|
|
|
146
|
|
|
return $this->respondNotFound(); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Returns a single item |
|
152
|
|
|
* |
|
153
|
|
|
* @param \Illuminate\Database\Eloquent\Model $object |
|
154
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
155
|
|
|
*/ |
|
156
|
|
|
public function showByObject($object) |
|
157
|
|
|
{ |
|
158
|
|
|
try { |
|
159
|
|
|
return $this->response->withItem($object, new $this->transformerName); |
|
160
|
|
|
|
|
161
|
|
|
} catch (ModelNotFoundException $e) { |
|
162
|
|
|
|
|
163
|
|
|
return $this->respondNotFound(); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Returns a paginated collection |
|
169
|
|
|
* |
|
170
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
171
|
|
|
*/ |
|
172
|
|
|
public function collection() |
|
173
|
|
|
{ |
|
174
|
|
|
$limit = \Input::get('limit') ?: 10; |
|
175
|
|
|
$model = $this->model; |
|
176
|
|
|
|
|
177
|
|
|
if (\Request::has('order')) { |
|
178
|
|
|
list($orderCol, $orderBy) = explode('|', \Input::get('order')); |
|
179
|
|
|
$model = $model->orderBy($orderCol, $orderBy); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $this->response->withPaginator($model->paginate($limit), new $this->transformerName, $this->collectionName); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Handles 404 errors |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $msg |
|
189
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
190
|
|
|
*/ |
|
191
|
|
|
public static function respondNotFound($msg = 'Not found!') |
|
192
|
|
|
{ |
|
193
|
|
|
return \Response::json([ |
|
194
|
|
|
'error' => [ |
|
195
|
|
|
'message' => $msg, |
|
196
|
|
|
'status_code' => 404 |
|
197
|
|
|
] |
|
198
|
|
|
], 404); |
|
199
|
|
|
} |
|
200
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.