Skip to content

Commit d37454d

Browse files
committed
Initial release
0 parents  commit d37454d

File tree

9 files changed

+759
-0
lines changed

9 files changed

+759
-0
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# OS
2+
[Tt]humbs.db
3+
*.DS_Store
4+
5+
# PHPStorm
6+
*.idea
7+
8+
# Composer
9+
vendor/*
10+
11+
# Local tests
12+
tests/*

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
- `Added` for new features.
9+
- `Changed` for changes in existing functionality.
10+
- `Deprecated` for soon-to-be removed features.
11+
- `Removed` for now removed features.
12+
- `Fixed` for any bug fixes.
13+
- `Security` in case of vulnerabilities
14+
15+
## [1.0.0] - 2020.08.10
16+
17+
### Added
18+
19+
- Initial release.

FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Funding
2+
3+
patreon: bayfrontmedia

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Bayfront Media
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
## PHP HTTP response
2+
3+
Easily send HTTP responses.
4+
5+
- [License](#license)
6+
- [Author](#author)
7+
- [Requirements](#requirements)
8+
- [Installation](#installation)
9+
- [Usage](#usage)
10+
11+
## License
12+
13+
This project is open source and available under the [MIT License](https://github.com/bayfrontmedia/php-array-helpers/blob/master/LICENSE).
14+
15+
## Author
16+
17+
John Robinson, [Bayfront Media](https://www.bayfrontmedia.com)
18+
19+
## Requirements
20+
21+
- PHP >= 7.1.0
22+
23+
## Installation
24+
25+
```
26+
composer require bayfrontmedia/php-http-response
27+
```
28+
29+
## Usage
30+
31+
- [setStatusCode](#setstatuscode)
32+
- [getStatusCode](#getstatuscode)
33+
- [setHeaders](#setheaders)
34+
- [getHeaders](#getheaders)
35+
- [setBody](#setbody)
36+
- [getBody](#getbody)
37+
- [send](#send)
38+
- [sendJson](#sendjson)
39+
- [redirect](#redirect)
40+
41+
<hr />
42+
43+
### setStatusCode
44+
45+
**Description:**
46+
47+
Sets status code to be sent with response.
48+
49+
**Parameters:**
50+
51+
- `$status` (int)
52+
53+
**Returns:**
54+
55+
- (self)
56+
57+
**Throws:**
58+
59+
- `Bayfront\HttpResponse\InvalidStatusCodeException`
60+
61+
**Example:**
62+
63+
```
64+
use Bayfront\HttpResponse\InvalidStatusCodeException;
65+
use Bayfront\HttpResponse\Response;
66+
67+
$response = new Response();
68+
69+
try {
70+
71+
$response->setStatusCode(429);
72+
73+
} catch (InvalidStatusCodeException $e) {
74+
die($e->getMessage());
75+
}
76+
```
77+
78+
<hr />
79+
80+
### getStatusCode
81+
82+
**Description:**
83+
84+
Returns the status code and associated phrase to be sent with response.
85+
86+
**Parameters:**
87+
88+
- None
89+
90+
**Returns:**
91+
92+
- (array)
93+
94+
**Example:**
95+
96+
```
97+
print_r($response->getStatusCode());
98+
```
99+
100+
<hr />
101+
102+
### setHeaders
103+
104+
**Description:**
105+
106+
Sets header value(s) to be sent with the response.
107+
108+
**Parameters:**
109+
110+
- `$headers` (array)
111+
112+
**Returns:**
113+
114+
- (self)
115+
116+
**Example:**
117+
118+
```
119+
$response->setHeaders([
120+
'X-Rate-Limit-Limit' => 100,
121+
'X-Rate-Limit-Remaining' => 99
122+
]);
123+
```
124+
125+
<hr />
126+
127+
### getHeaders
128+
129+
**Description:**
130+
131+
Returns array of headers to be sent with the response.
132+
133+
**Parameters:**
134+
135+
- None
136+
137+
**Returns:**
138+
139+
- (array)
140+
141+
**Example:**
142+
143+
```
144+
print_r($response->getHeaders());
145+
```
146+
147+
<hr />
148+
149+
### setBody
150+
151+
**Description:**
152+
153+
Sets body to be sent with the response.
154+
155+
**Parameters:**
156+
157+
- `$body` (string)
158+
159+
**Returns:**
160+
161+
- (self)
162+
163+
**Example:**
164+
165+
```
166+
$response->setBody('This is the response body.');
167+
```
168+
169+
<hr />
170+
171+
### getBody
172+
173+
**Description:**
174+
175+
Returns body to be sent with the response, or `null` if body has not been set.
176+
177+
**Parameters:**
178+
179+
- None
180+
181+
**Returns:**
182+
183+
- (string|null)
184+
185+
**Example:**
186+
187+
```
188+
echo $response->getBody();
189+
```
190+
191+
<hr />
192+
193+
### send
194+
195+
**Description:**
196+
197+
Sends response.
198+
199+
**Parameters:**
200+
201+
- None
202+
203+
**Returns:**
204+
205+
- (void)
206+
207+
**Example:**
208+
209+
```
210+
$response->send();
211+
```
212+
213+
<hr />
214+
215+
### sendJson
216+
217+
**Description:**
218+
219+
Sets Content-Type as `application/vnd.api+json`, and converts the given array to the JSON encoded body.
220+
221+
**Parameters:**
222+
223+
- `$array` (array)
224+
225+
**Returns:**
226+
227+
- (void)
228+
229+
**Example:**
230+
231+
```
232+
$response->sendJson([
233+
'results' => [
234+
'user_id' => 5,
235+
'username' => 'some_username'
236+
],
237+
'status' => 'OK'
238+
]);
239+
```
240+
241+
<hr />
242+
243+
### redirect
244+
245+
**Description:**
246+
247+
Redirects to a given URL using a given status code.
248+
249+
**Parameters:**
250+
251+
- `$url` (string)
252+
- `$status = 302` (int): HTTP status code to return
253+
254+
**Returns:**
255+
256+
- (void)
257+
258+
**Throws:**
259+
260+
- `Bayfront\HttpResponse\InvalidStatusCodeException`
261+
262+
**Example:**
263+
264+
```
265+
use Bayfront\HttpResponse\InvalidStatusCodeException;
266+
use Bayfront\HttpResponse\Response;
267+
268+
$response = new Response();
269+
270+
try {
271+
272+
$response->redirect('https://www.google.com', 301);
273+
274+
} catch (InvalidStatusCodeException $e) {
275+
die($e->getMessage());
276+
}
277+
```

composer.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "bayfrontmedia/php-http-response",
3+
"description": "Easily send HTTP responses.",
4+
"homepage": "https://github.com/bayfrontmedia/php-http-response",
5+
"license" : "MIT",
6+
"version": "1.0.0",
7+
"type": "library",
8+
"keywords": [
9+
"php",
10+
"http",
11+
"delete",
12+
"get",
13+
"head",
14+
"patch",
15+
"post",
16+
"put",
17+
"server",
18+
"header",
19+
"method",
20+
"response",
21+
"url"
22+
],
23+
"authors": [
24+
{
25+
"name": "John Robinson",
26+
"email": "[email protected]",
27+
"homepage": "https://www.bayfrontmedia.com",
28+
"role": "Developer"
29+
}
30+
],
31+
"funding": [
32+
{
33+
"type": "patreon",
34+
"url": "https://www.patreon.com/bayfrontmedia"
35+
}
36+
],
37+
"require": {
38+
"php": ">=7.1.0"
39+
},
40+
"autoload": {
41+
"psr-4": {
42+
"Bayfront\\HttpResponse\\": "src/"
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)