Skip to content

Commit 326494f

Browse files
committed
Initial commit
1 parent 3dbf7c7 commit 326494f

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.idea/
2+
.vscode/
3+
vendor/
4+
5+
index.php
6+
composer.lock
7+
8+
.DS_Store
9+
.env
10+
11+

composer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "ninetynine/laravel-response",
3+
"description": "A Laravel library to help standardize responses",
4+
"type": "library",
5+
"license": "LGPL-3.0-only",
6+
"keywords": [
7+
"helpers",
8+
"php",
9+
"laravel"
10+
],
11+
"homepage": "https://github.com/ninetynine/laravel-response",
12+
"support": {
13+
"issues": "https://github.com/ninetynine/laravel-response/issues",
14+
"source": "https://github.com/ninetynine/laravel-response"
15+
},
16+
"authors": [
17+
{
18+
"name": "Dexter Marks-Barber",
19+
"email": "dexter@marks-barber.co.uk"
20+
}
21+
],
22+
"require": {
23+
"php": "^7.0.0"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"NinetyNine\\": "src/NinetyNine/"
28+
}
29+
},
30+
"extra": {
31+
"branch-alias": {
32+
"dev-master": "0.x-dev"
33+
}
34+
}
35+
}

src/NinetyNine/Http/ApiResponse.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
namespace NinetyNine\Http;
3+
4+
class ApiResponse
5+
{
6+
/** @var int $status */
7+
protected $status;
8+
9+
/** @var bool $success */
10+
protected $success;
11+
12+
/** @var array|null|string $error */
13+
protected $error;
14+
15+
/** @var array|null $data */
16+
protected $data;
17+
18+
/** @var array|null $meta */
19+
protected $meta;
20+
21+
/**
22+
* ApiResponse constructor.
23+
*
24+
* @param bool $success
25+
* @param array|null|string $error
26+
* @param array|null $data
27+
* @param array|null $meta
28+
* @param int $status
29+
*/
30+
public function __construct(bool $success = true, $error = null, $data = null, $meta = null, int $status = 200)
31+
{
32+
$this->status = $status;
33+
$this->success = $success;
34+
$this->error = $error;
35+
$this->data = $data;
36+
$this->meta = $meta;
37+
}
38+
39+
/**
40+
* @param array|null $data
41+
* @param array|null $meta
42+
* @param int $status
43+
* @return ApiResponse
44+
*/
45+
public static function success($data = null, $meta = null, int $status = 200)
46+
{
47+
$response = new self;
48+
49+
$response->status = $status;
50+
$response->data = $data;
51+
$response->meta = $meta;
52+
53+
return $response;
54+
}
55+
56+
/**
57+
* @param mixed $error
58+
* @param int $status
59+
* @return ApiResponse
60+
*/
61+
public static function error($error = null, int $status = 400)
62+
{
63+
$response = new self;
64+
65+
$response->status = $status;
66+
$response->success = false;
67+
$response->error = $error;
68+
69+
return $response;
70+
}
71+
72+
/**
73+
* @param array $data
74+
* @return $this
75+
*/
76+
public function data(array $data = null)
77+
{
78+
$this->data = $data;
79+
80+
return $this;
81+
}
82+
83+
/**
84+
* @param array $meta
85+
* @return $this
86+
*/
87+
public function meta(array $meta = null)
88+
{
89+
$this->meta = $meta;
90+
91+
return $this;
92+
}
93+
94+
/**
95+
* @param int $status
96+
* @return $this
97+
*/
98+
public function status(int $status)
99+
{
100+
$this->status = $status;
101+
102+
return $this;
103+
}
104+
105+
/**
106+
* @return \Illuminate\Http\JsonResponse
107+
*/
108+
public function json()
109+
{
110+
return response()
111+
->json(get_object_vars($this))
112+
->setStatusCode($this->status);
113+
}
114+
}

0 commit comments

Comments
 (0)