Skip to content

Commit 259fb74

Browse files
committed
Simples Http package
0 parents  commit 259fb74

File tree

9 files changed

+2001
-0
lines changed

9 files changed

+2001
-0
lines changed

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) 2016 Grupo de PHP da Zona da Mata
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.

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "phpzm/http",
3+
"description": "Simples Http package",
4+
"minimum-stability": "dev",
5+
"keywords": [
6+
"php",
7+
"framework",
8+
"api",
9+
"simples"
10+
],
11+
"homepage": "https://github.com/phpzm/http",
12+
"license": "MIT",
13+
"version": "1.0.0",
14+
"type": "package",
15+
"authors": [
16+
{
17+
"name": "William",
18+
"email": "[email protected]"
19+
},
20+
{
21+
"name": "Ezio",
22+
"email": "[email protected]"
23+
}
24+
],
25+
"require": {
26+
"php": ">=7.0",
27+
"phpzm/helper": "dev-master",
28+
"phpzm/kernel": "dev-master",
29+
"phpzm/route": "dev-master",
30+
"psr/http-message": "^1.0"
31+
},
32+
"autoload": {
33+
"psr-4": {
34+
"Simples\\Http\\Error\\": "src/Error/",
35+
"Simples\\Http\\": "src/"
36+
}
37+
}
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Simples\Http\Error;
4+
5+
use Simples\Error\SimplesRunTimeError;
6+
7+
/**
8+
* Class ForbiddenError
9+
* @package Simples\Http\Error
10+
*/
11+
class SimplesForbiddenError extends SimplesRunTimeError
12+
{
13+
/**
14+
* @var int
15+
*/
16+
protected $status = 403;
17+
}

src/Http/Controller.php

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
3+
namespace Simples\Http;
4+
5+
use Simples\Kernel\App;
6+
use Simples\Route\Match;
7+
8+
/**
9+
* @method Response answerContinue($data = null, $meta = []) // 100
10+
* @method Response answerSwitching Protocols($data = null, $meta = []) // 101
11+
* @method Response answerProcessing($data = null, $meta = []) // 102
12+
*
13+
* @method Response answerOK($data = null, $meta = []) // 200
14+
* @method Response answerCreated($data = null, $meta = []) // 201
15+
* @method Response answerAccepted($data = null, $meta = []) // 202
16+
* @method Response answerNonAuthoritativeInformation($data = null, $meta = []) // 203
17+
* @method Response answerNoContent($data = null, $meta = []) // 204
18+
* @method Response answerResetContent($data = null, $meta = []) // 205
19+
* @method Response answerPartialContent($data = null, $meta = []) // 206
20+
* @method Response answerMultiStatus($data = null, $meta = []) // 207
21+
* @method Response answerAlreadyReported($data = null, $meta = []) // 208
22+
*
23+
* @method Response answerMultiple Choices($data = null, $meta = []) // 300
24+
* @method Response answerMoved Permanently($data = null, $meta = []) // 301
25+
* @method Response answerFound($data = null, $meta = []) // 302
26+
* @method Response answerSeeOther($data = null, $meta = []) // 303
27+
* @method Response answerNotModified($data = null, $meta = []) // 304
28+
* @method Response answerUseProxy($data = null, $meta = []) // 305
29+
* @method Response answerTemporaryRedirect($data = null, $meta = []) // 307
30+
*
31+
* @method Response answerBadRequest($data = null, $meta = []) // 400
32+
* @method Response answerUnauthorized($data = null, $meta = []) // 401
33+
* @method Response answerPaymentRequired($data = null, $meta = []) // 402
34+
* @method Response answerForbidden($data = null, $meta = []) // 403
35+
* @method Response answerNotFound($data = null, $meta = []) // 404
36+
* @method Response answerMethodNotAllowed($data = null, $meta = []) // 405
37+
* @method Response answerNotAcceptable($data = null, $meta = []) // 406
38+
* @method Response answerProxyAuthenticationRequired($data = null, $meta = []) // 407
39+
* @method Response answerRequestTimeout($data = null, $meta = []) // 408
40+
* @method Response answerConflict($data = null, $meta = []) // 409
41+
* @method Response answerGone($data = null, $meta = []) // 410
42+
* @method Response answerLengthRequired($data = null, $meta = []) // 411
43+
* @method Response answerPreconditionFailed($data = null, $meta = []) // 412
44+
* @method Response answerRequestEntityTooLarge($data = null, $meta = []) // 413
45+
* @method Response answerRequestURITooLarge($data = null, $meta = []) // 414
46+
* @method Response answerUnsupportedMediaType($data = null, $meta = []) // 415
47+
* @method Response answerRequestedRangeNotSatisfiable($data = null, $meta = []) // 416
48+
* @method Response answerExpectationFailed($data = null, $meta = []) // 417
49+
* @method Response answerIAmATeapot($data = null, $meta = []) // 418
50+
* @method Response answerNotProcessableEntity($data = null, $meta = []) // 422
51+
* @method Response answerLocked($data = null, $meta = []) // 423
52+
* @method Response answerFailedDependency($data = null, $meta = []) // 424
53+
* @method Response answerUnorderedCollection($data = null, $meta = []) // 425
54+
* @method Response answerUpgradeRequired($data = null, $meta = []) // 426
55+
* @method Response answerPreconditionRequired($data = null, $meta = []) // 428
56+
* @method Response answerTooManyRequests($data = null, $meta = []) // 429
57+
* @method Response answerRequestHeaderFieldsTooLarge($data = null, $meta = []) // 431
58+
*
59+
* @method Response answerInternalServerErro($data = null, $meta = []) // 500
60+
* @method Response answerNotImplemented($data = null, $meta = []) // 501
61+
* @method Response answerBadGateway($data = null, $meta = []) // 502
62+
* @method Response answerServiceUnavailable($data = null, $meta = []) // 503
63+
* @method Response answerGatewayTimeout($data = null, $meta = []) // 504
64+
* @method Response answerHTTPVersionNotSupported($data = null, $meta = []) // 505
65+
* @method Response answerVariantAlsoNegotiates($data = null, $meta = []) // 506
66+
* @method Response answerInsufficientStorage($data = null, $meta = []) // 507
67+
* @method Response answerLoopDetected($data = null, $meta = []) // 508
68+
* @method Response answerNetworkAuthenticationRequired($data = null, $meta = []) // 511
69+
*
70+
* Class Controller
71+
* @package Simples\Http
72+
*/
73+
abstract class Controller
74+
{
75+
/**
76+
* @var Request
77+
*/
78+
private $request;
79+
80+
/**
81+
* @var Response
82+
*/
83+
private $response;
84+
85+
/**
86+
* @var Match
87+
*/
88+
private $match;
89+
90+
/**
91+
* @param Request $request
92+
* @param Response $response
93+
* @param Match $match
94+
* @return $this
95+
*/
96+
public final function boot(Request $request, Response $response, Match $match)
97+
{
98+
$this->request = $request;
99+
$this->response = $response;
100+
$this->match = $match;
101+
102+
return $this;
103+
}
104+
105+
/**
106+
* @param null $content
107+
* @param array $meta
108+
* @param int $code
109+
* @return Response
110+
*/
111+
abstract protected function answer($content = null, $meta = [], $code = 200) : Response;
112+
113+
/**
114+
* @return Request
115+
*/
116+
final protected function request()
117+
{
118+
return $this->request;
119+
}
120+
121+
/**
122+
* @return Response
123+
*/
124+
final protected function response()
125+
{
126+
return $this->response;
127+
}
128+
129+
/**
130+
* @return Match
131+
*/
132+
public function match()
133+
{
134+
return $this->match;
135+
}
136+
137+
/**
138+
* @param $name
139+
* @param $type
140+
* @return mixed
141+
*/
142+
public function input($name, $type = null)
143+
{
144+
$input = $this->request->getInput($name);
145+
if (!$input or !$type) {
146+
return $input;
147+
}
148+
return $input->filter($type);
149+
}
150+
151+
/**
152+
* @param $logging
153+
*/
154+
public function setLog($logging)
155+
{
156+
App::log($logging && env('TEST_MODE'));
157+
}
158+
159+
/**
160+
* @param $name
161+
* @param $arguments
162+
* @return Response
163+
*/
164+
public function __call($name, $arguments)
165+
{
166+
$httpStatusCodes = ResponseStream::HTTP_STATUS_CODE;
167+
168+
$content = off($arguments, 0, '');
169+
$meta = off($arguments, 1, []);
170+
$code = 501;
171+
172+
if (substr($name, 0, 6) === 'answer') {
173+
$reasonPhrase = substr($name, 6);
174+
foreach ($httpStatusCodes as $statusCode => $statusReasonPhrase) {
175+
if ($reasonPhrase === str_replace([' ', '-'], '', $statusReasonPhrase)) {
176+
$code = $statusCode;
177+
break;
178+
}
179+
}
180+
}
181+
182+
return $this->answer($content, $meta, $code);
183+
}
184+
}

src/Http/Input.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Simples\Http;
4+
5+
/**
6+
* Class Input
7+
* @package Simples\Http
8+
*/
9+
class Input
10+
{
11+
/**
12+
* @var mixed
13+
*/
14+
private $value;
15+
16+
/**
17+
* Input constructor.
18+
* @param mixed $value
19+
*/
20+
public function __construct($value)
21+
{
22+
$this->value = $value;
23+
}
24+
25+
/**
26+
* @return mixed
27+
*/
28+
public function number()
29+
{
30+
if (preg_match('/\d+(?:\.\d+)+/', $this->value, $matches)) {
31+
return $matches[0];
32+
}
33+
return 0;
34+
}
35+
36+
/**
37+
* @return mixed
38+
*/
39+
public function string()
40+
{
41+
if (is_scalar($this->value)) {
42+
return filter_var($this->value, FILTER_SANITIZE_STRING);
43+
}
44+
if (gettype($this->value) === TYPE_ARRAY) {
45+
return filter_var_array($this->value, FILTER_SANITIZE_STRING);
46+
}
47+
return null;
48+
}
49+
50+
/**
51+
* @param mixed $type
52+
* @return mixed
53+
*/
54+
public function filter($type)
55+
{
56+
if (method_exists($this, $type)) {
57+
return $this->$type($this->value);
58+
}
59+
return $this->value;
60+
}
61+
62+
/**
63+
* @return mixed
64+
*/
65+
public function __toString()
66+
{
67+
return $this->filterString();
68+
}
69+
}

0 commit comments

Comments
 (0)