1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Api; |
15
|
|
|
|
16
|
|
|
use Exception; |
17
|
|
|
use Override; |
|
|
|
|
18
|
|
|
use Valkyrja\Api\Constant\Status; |
|
|
|
|
19
|
|
|
use Valkyrja\Api\Contract\Api as Contract; |
20
|
|
|
use Valkyrja\Api\Model\Contract\Json; |
21
|
|
|
use Valkyrja\Api\Model\Contract\JsonData; |
22
|
|
|
use Valkyrja\Http\Message\Enum\StatusCode; |
23
|
|
|
use Valkyrja\Http\Message\Exception\HttpException; |
24
|
|
|
use Valkyrja\Http\Message\Factory\Contract\ResponseFactory; |
25
|
|
|
use Valkyrja\Http\Message\Response\Contract\JsonResponse; |
26
|
|
|
use Valkyrja\Orm\Entity\Contract\Entity; |
27
|
|
|
|
28
|
|
|
use function end; |
29
|
|
|
use function explode; |
30
|
|
|
use function strtolower; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class Api. |
34
|
|
|
* |
35
|
|
|
* @author Melech Mizrachi |
36
|
|
|
*/ |
37
|
|
|
class Api implements Contract |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Api constructor. |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
protected ResponseFactory $responseFactory, |
44
|
|
|
protected bool $debug = false |
45
|
|
|
) { |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritDoc |
50
|
|
|
*/ |
51
|
|
|
#[Override] |
52
|
|
|
public function jsonFromException(Exception $exception): Json |
53
|
|
|
{ |
54
|
|
|
$json = $this->getJsonModel(); |
55
|
|
|
|
56
|
|
|
$data = [ |
57
|
|
|
'code' => $exception->getCode(), |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
$json->setMessage($exception->getMessage()); |
61
|
|
|
$json->setStatus(Status::ERROR); |
62
|
|
|
$json->setStatusCode(StatusCode::INTERNAL_SERVER_ERROR); |
63
|
|
|
|
64
|
|
|
if ($this->debug) { |
65
|
|
|
$data['file'] = $exception->getFile(); |
66
|
|
|
$data['line'] = $exception->getLine(); |
67
|
|
|
$data['trace'] = $exception->getTrace(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$json->setData($data); |
71
|
|
|
|
72
|
|
|
if ($exception instanceof HttpException) { |
73
|
|
|
$json->setStatusCode($exception->getStatusCode()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $json; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
82
|
|
|
#[Override] |
83
|
|
|
public function jsonResponseFromException(Exception $exception): JsonResponse |
84
|
|
|
{ |
85
|
|
|
return $this->getResponseFromModel($this->jsonFromException($exception)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritDoc |
90
|
|
|
*/ |
91
|
|
|
#[Override] |
92
|
|
|
public function jsonFromObject(object $object): Json |
93
|
|
|
{ |
94
|
|
|
$json = $this->getJsonModel(); |
95
|
|
|
$jsonData = $this->getJsonDataModel(); |
96
|
|
|
|
97
|
|
|
$jsonData->setItem($object); |
98
|
|
|
$this->setItemKeysFromObject($object, $jsonData); |
99
|
|
|
$json->setData($jsonData->asArray()); |
100
|
|
|
|
101
|
|
|
return $json; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritDoc |
106
|
|
|
*/ |
107
|
|
|
#[Override] |
108
|
|
|
public function jsonResponseFromObject(object $object): JsonResponse |
109
|
|
|
{ |
110
|
|
|
return $this->getResponseFromModel($this->jsonFromObject($object)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritDoc |
115
|
|
|
*/ |
116
|
|
|
#[Override] |
117
|
|
|
public function jsonFromObjects(object ...$objects): Json |
118
|
|
|
{ |
119
|
|
|
$json = $this->getJsonModel(); |
120
|
|
|
$jsonData = $this->getJsonDataModel(); |
121
|
|
|
|
122
|
|
|
$jsonData->setItems($objects); |
123
|
|
|
|
124
|
|
|
if (! empty($objects)) { |
125
|
|
|
$this->setItemKeysFromObject($objects[0], $jsonData); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$json->setData($jsonData->asArray()); |
129
|
|
|
|
130
|
|
|
return $json; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @inheritDoc |
135
|
|
|
*/ |
136
|
|
|
#[Override] |
137
|
|
|
public function jsonResponseFromObjects(object ...$objects): JsonResponse |
138
|
|
|
{ |
139
|
|
|
return $this->getResponseFromModel($this->jsonFromObjects(...$objects)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritDoc |
144
|
|
|
*/ |
145
|
|
|
#[Override] |
146
|
|
|
public function jsonFromArray(array $array): Json |
147
|
|
|
{ |
148
|
|
|
$json = $this->getJsonModel(); |
149
|
|
|
$jsonData = $this->getJsonDataModel(); |
150
|
|
|
|
151
|
|
|
$jsonData->setData($array); |
152
|
|
|
|
153
|
|
|
$json->setData($jsonData->asArray()); |
154
|
|
|
|
155
|
|
|
return $json; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @inheritDoc |
160
|
|
|
*/ |
161
|
|
|
#[Override] |
162
|
|
|
public function jsonResponseFromArray(array $array): JsonResponse |
163
|
|
|
{ |
164
|
|
|
return $this->getResponseFromModel($this->jsonFromArray($array)); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @inheritDoc |
169
|
|
|
*/ |
170
|
|
|
#[Override] |
171
|
|
|
public function jsonFromEntity(Entity $entity): Json |
172
|
|
|
{ |
173
|
|
|
return $this->jsonFromObject($entity); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @inheritDoc |
178
|
|
|
*/ |
179
|
|
|
#[Override] |
180
|
|
|
public function jsonResponseFromEntity(Entity $entity): JsonResponse |
181
|
|
|
{ |
182
|
|
|
return $this->getResponseFromModel($this->jsonFromEntity($entity)); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @inheritDoc |
187
|
|
|
*/ |
188
|
|
|
#[Override] |
189
|
|
|
public function jsonFromEntities(Entity ...$entities): Json |
190
|
|
|
{ |
191
|
|
|
return $this->jsonFromObjects(...$entities); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @inheritDoc |
196
|
|
|
*/ |
197
|
|
|
#[Override] |
198
|
|
|
public function jsonResponseFromEntities(Entity ...$entities): JsonResponse |
199
|
|
|
{ |
200
|
|
|
return $this->getResponseFromModel($this->jsonFromEntities(...$entities)); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get a JSON response from a JSON model. |
205
|
|
|
* |
206
|
|
|
* @param Json $json The json model |
207
|
|
|
* |
208
|
|
|
* @return JsonResponse |
209
|
|
|
*/ |
210
|
|
|
protected function getResponseFromModel(Json $json): JsonResponse |
211
|
|
|
{ |
212
|
|
|
return $this->responseFactory->createJsonResponse($json->asArray()); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get JSON model. |
217
|
|
|
* |
218
|
|
|
* @return Json |
219
|
|
|
*/ |
220
|
|
|
protected function getJsonModel(): Json |
221
|
|
|
{ |
222
|
|
|
return new Model\Json(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get JSON data model. |
227
|
|
|
* |
228
|
|
|
* @return JsonData |
229
|
|
|
*/ |
230
|
|
|
protected function getJsonDataModel(): JsonData |
231
|
|
|
{ |
232
|
|
|
return new Model\JsonData(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Set item keys from object. |
237
|
|
|
* |
238
|
|
|
* @param object $object |
239
|
|
|
* @param JsonData $jsonData |
240
|
|
|
* |
241
|
|
|
* @return void |
242
|
|
|
*/ |
243
|
|
|
protected function setItemKeysFromObject(object $object, JsonData $jsonData): void |
244
|
|
|
{ |
245
|
|
|
$className = $this->getClassNameFromObject($object); |
246
|
|
|
|
247
|
|
|
$jsonData->setItemKey($className); |
248
|
|
|
$jsonData->setItemsKey($className . 's'); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Get the class name from an object. |
253
|
|
|
* |
254
|
|
|
* @param object $object |
255
|
|
|
* |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
|
|
protected function getClassNameFromObject(object $object): string |
259
|
|
|
{ |
260
|
|
|
$classNameParts = explode('\\', $object::class); |
261
|
|
|
|
262
|
|
|
return strtolower(end($classNameParts)); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths