Skip to content

Commit aa8b5a6

Browse files
author
Raphael
committed
initial commit
1 parent 9c86b85 commit aa8b5a6

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

composer.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "uglymanfirst/laravel-api_responses",
3+
"description": "It's a Laravel format for an API responses.",
4+
"type": "library",
5+
"license": "GNU General Public License v3.0",
6+
"authors": [
7+
{
8+
"name": "Raphael",
9+
"email": "[email protected]",
10+
"homepage": "https://github.com/uglymanfirst",
11+
"role": "Developer"
12+
}
13+
],
14+
"minimum-stability": "dev",
15+
"require": {
16+
"php": ">=5.3.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Uglymanfirst\\LaravelApiResponses": "src/"
21+
}
22+
}
23+
}
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace Uglymanfirst\LaravelApiResponses;
4+
5+
use Illuminate\Contracts\Routing\ResponseFactory;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class LaravelApiResponsesServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* Bootstrap the application services.
12+
*/
13+
public function boot(ResponseFactory $factory)
14+
{
15+
$factory->macro('success', function ($data, $status = 200) use ($factory) {
16+
$successFormat = ['data' => $data,];
17+
return $factory->make(json_encode($successFormat), $status, ['Content-Type' => 'application/json']);
18+
});
19+
20+
$factory->macro('paginate', function ($data, $status = 200) use ($factory) {
21+
if (!is_array($data)) {
22+
$data = $data->toArray();
23+
}
24+
25+
$removedData = $data['data'];
26+
27+
unset($data['data']);
28+
29+
$paginateFormat = [
30+
'meta' => ['pagination' => $data],
31+
'data' => $removedData,
32+
];
33+
34+
return $factory->make(json_encode($paginateFormat), $status, ['Content-Type' => 'application/json']);
35+
});
36+
37+
$factory->macro('error', function ($data = [], $status = 400, $extras = []) use ($factory) {
38+
$errorFormat = ['errors' => $data];
39+
40+
if (isset($extras['Content-Type'])) {
41+
unset($extras['Content-Type']);
42+
}
43+
44+
$headers = array_merge($extras, ['Content-Type' => 'application/json']);
45+
46+
return $factory->make(json_encode($errorFormat), $status, $headers);
47+
});
48+
49+
$factory->macro('custom', function ($content = null, $status = 200, $headers = [], $headerContentType = 'application/json') use ($factory) {
50+
if (!is_null($content)) {
51+
$customFormat = $content;
52+
}
53+
54+
if (array_key_exists('Content-Type', $headers)) {
55+
$headerContentType = $headers['Content-Type'];
56+
}
57+
58+
$headers = array_merge($headers, ['Content-Type' => $headerContentType]);
59+
60+
return $factory->make($customFormat, $status, $headers);
61+
});
62+
63+
$factory->macro('defaultStatusCode', function ($status = 200, $extras = []) use ($factory) {
64+
$statusList = [
65+
// 1×× => 'Informational',
66+
102 => 'Processing',
67+
// 2×× => 'Success',
68+
200 => 'OK',
69+
201 => 'Created',
70+
202 => 'Accepted',
71+
203 => 'Non-authoritative Information',
72+
204 => '',//No Content
73+
206 => 'Partial Content',
74+
207 => 'Multi-Status',
75+
// 3×× => 'Redirection',
76+
302 => 'Found',
77+
304 => 'Not Modified',
78+
// 4×× => 'Client Error',
79+
400 => 'Bad Request',
80+
401 => 'Unauthorized',
81+
402 => 'Payment Required',
82+
403 => 'Forbidden',
83+
404 => 'Not Found',
84+
405 => 'Method Not Allowed',
85+
406 => 'Not Acceptable',
86+
409 => 'Conflict',
87+
413 => 'Payload Too Large',
88+
415 => 'Unsupported Media Type',
89+
416 => 'Requested Range Not Satisfiable',
90+
422 => 'Unprocessable Entity',
91+
423 => 'Locked',
92+
424 => 'Failed Dependency',
93+
// => '5×× Server Error',
94+
500 => 'Internal Server Error',
95+
501 => 'Not Implemented',
96+
503 => 'Service Unavailable'
97+
];
98+
99+
if (! isset($statusList[$status])) {
100+
$statusList[$status] = 'Response code not found.';
101+
}
102+
103+
if (isset($extras['Content-Type'])) {
104+
unset($extras['Content-Type']);
105+
}
106+
107+
$statusFormat = '';
108+
$headers = [];
109+
110+
if ($status >= 400) {
111+
$statusFormat = [
112+
'errors' => [$statusList[$status]]
113+
];
114+
$headers = ['Content-Type' => 'application/json'];
115+
}
116+
$headers = array_merge($extras, $headers);
117+
118+
return $factory->make($statusFormat, $status, $headers);
119+
});
120+
}
121+
122+
/**
123+
* Register the application services.
124+
*/
125+
public function register()
126+
{
127+
}
128+
}

0 commit comments

Comments
 (0)