Skip to content

Commit 283750d

Browse files
authored
Merge pull request #1 from ABGEO07/develope
Develope
2 parents 7530d8e + d456897 commit 283750d

File tree

4 files changed

+195
-2
lines changed

4 files changed

+195
-2
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
# Cherry-response Changelog
1+
# Cherry-response Changelog
2+
3+
## [v1.0.0](https://github.com/ABGEO07/cherry-response/releases/tag/v1.0.0 "v1.0.0") (2019-03-25)
4+
#### The first stable version
5+
6+
- Package available on: `composer require cherry-project/response`
7+
8+
- Class features
9+
- Set status code
10+
- Set additional headers

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,54 @@
11
# Cherry-Response
22
The Cherry-project Response wrapper
33

4+
[![GitHub license](https://img.shields.io/github/license/abgeo07/cherry-response.svg)](https://github.com/ABGEO07/cherry-response/blob/master/LICENSE)
5+
6+
[![GitHub release](https://img.shields.io/github/release/abgeo07/cherry-response.svg)](https://github.com/ABGEO07/cherry-response/releases)
7+
8+
[![Packagist Version](https://img.shields.io/packagist/v/cherry-project/response.svg "Packagist Version")](https://packagist.org/packages/cherry-project/response "Packagist Version")
9+
10+
------------
11+
12+
## Including
13+
**Install from composer** `composer require cherry-project/response`
14+
15+
**Include Autoloader in your main file** (Ex.: index.php)
16+
```php
17+
require_once __DIR__ . '/vendor/autoload.php';
18+
```
19+
20+
## Class Request
21+
Import class
22+
```php
23+
use Cherry\Response;
24+
```
25+
Crete class new object
26+
```php
27+
$response = new Response();
28+
```
29+
30+
Prepare data for transmission (Ex.: Array)
31+
```php
32+
$arr = [
33+
'test' => 'vTest',
34+
'test2' => [
35+
'test2.1' => '2.1',
36+
'test2.2' => [
37+
'test2.2.1' => '2.2.1'
38+
]
39+
]
40+
];
41+
```
42+
43+
For sending response use **sendResponse()** method
44+
45+
The method takes 3 arguments:
46+
- Content (**Required**; Mixed) - The data for returning;
47+
- Status Code (**Optional**; 200 by default; Integer) - The HTTP Status Code;
48+
- HTTP Headers (**Optional**; empty by default; Key `=>` Value Array) - The HTTP Headers;
49+
50+
```php
51+
echo $response->sendResponse(json_encode($arr), 200, ['Content-Type' => 'application/json']);
52+
```
53+
454
**2019 © Cherry-project**

examples/test.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,17 @@
55

66
use Cherry\Response;
77

8-
$request = new Response();
8+
//Send Response
9+
$response = new Response();
10+
11+
$arr = [
12+
'test' => 'vTest',
13+
'test2' => [
14+
'test2.1' => '2.1',
15+
'test2.2' => [
16+
'test2.2.1' => '2.2.1'
17+
]
18+
]
19+
];
20+
21+
echo $response->sendResponse(json_encode($arr), 200, ['Content-Type' => 'application/json']);

src/Response.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,125 @@
1010
*/
1111
class Response
1212
{
13+
/**
14+
* Get http status code full text
15+
*
16+
* @param $statusCode
17+
* @return string
18+
*/
19+
private function getStatusCodeMsg($statusCode)
20+
{
21+
$msg = array(
22+
100 => 'Continue',
23+
101 => 'Switching Protocols',
24+
102 => 'Processing',
25+
200 => 'OK',
26+
201 => 'Created',
27+
202 => 'Accepted',
28+
203 => 'Non-Authoritative Information',
29+
204 => 'No Content',
30+
205 => 'Reset Content',
31+
206 => 'Partial Content',
32+
207 => 'Multi-Status',
33+
208 => 'Already Reported',
34+
226 => 'IM Used',
35+
300 => 'Multiple Choices',
36+
301 => 'Moved Permanently',
37+
302 => 'Found',
38+
303 => 'See Other',
39+
304 => 'Not Modified',
40+
305 => 'Use Proxy',
41+
306 => 'Switch Proxy',
42+
307 => 'Temporary Redirect',
43+
308 => 'Permanent Redirect',
44+
400 => 'Bad Request',
45+
401 => 'Unauthorized',
46+
402 => 'Payment Required',
47+
403 => 'Forbidden',
48+
404 => 'Not Found',
49+
405 => 'Method Not Allowed',
50+
406 => 'Not Acceptable',
51+
407 => 'Proxy Authentication Required',
52+
408 => 'Request Timeout',
53+
409 => 'Conflict',
54+
410 => 'Gone',
55+
411 => 'Length Required',
56+
412 => 'Precondition Failed',
57+
413 => 'Request Entity Too Large',
58+
414 => 'Request-URI Too Long',
59+
415 => 'Unsupported Media Type',
60+
416 => 'Requested Range Not Satisfiable',
61+
417 => 'Expectation Failed',
62+
418 => 'I\'m a teapot',
63+
419 => 'Authentication Timeout',
64+
420 => 'Enhance Your Calm',
65+
422 => 'Unprocessable Entity',
66+
423 => 'Locked',
67+
424 => 'Failed Dependency',
68+
425 => 'Unordered Collection',
69+
426 => 'Upgrade Required',
70+
428 => 'Precondition Required',
71+
429 => 'Too Many Requests',
72+
431 => 'Request Header Fields Too Large',
73+
444 => 'No Response',
74+
449 => 'Retry With',
75+
450 => 'Blocked by Windows Parental Controls',
76+
451 => 'Unavailable For Legal Reasons',
77+
494 => 'Request Header Too Large',
78+
495 => 'Cert Error',
79+
496 => 'No Cert',
80+
497 => 'HTTP to HTTPS',
81+
499 => 'Client Closed Request',
82+
500 => 'Internal Server Error',
83+
501 => 'Not Implemented',
84+
502 => 'Bad Gateway',
85+
503 => 'Service Unavailable',
86+
504 => 'Gateway Timeout',
87+
505 => 'HTTP Version Not Supported',
88+
506 => 'Variant Also Negotiates',
89+
507 => 'Insufficient Storage',
90+
508 => 'Loop Detected',
91+
509 => 'Bandwidth Limit Exceeded',
92+
510 => 'Not Extended',
93+
511 => 'Network Authentication Required',
94+
598 => 'Network read timeout error',
95+
599 => 'Network connect timeout error',
96+
);
97+
98+
return $msg[$statusCode];
99+
}
100+
101+
/**
102+
* Set HTTP Headers
103+
*
104+
* @param $statusCode
105+
* @param $headers
106+
*/
107+
private function setHTTPHeaders($statusCode, $headers)
108+
{
109+
//Set HTTP Version and status header
110+
header("{$_SERVER['SERVER_PROTOCOL']} {$statusCode} {$this->getStatusCodeMsg($statusCode)}");
111+
//Set other headers
112+
foreach ($headers as $k => $v)
113+
header("{$k}: $v");
114+
}
115+
116+
/**
117+
* Send Response.
118+
*
119+
* @param $content
120+
* @param int $statusCode
121+
* @param array $headers
122+
*
123+
* @return mixed
124+
*/
125+
public function sendResponse($content, $statusCode = 200, $headers = [])
126+
{
127+
if ($content == '' || $content == null)
128+
die('Set response content!');
129+
130+
$this->setHTTPHeaders($statusCode, $headers);
131+
132+
return $content;
133+
}
13134
}

0 commit comments

Comments
 (0)