Skip to content

Commit f47f5bb

Browse files
committed
api
1 parent d4bf0cf commit f47f5bb

File tree

3 files changed

+324
-6
lines changed

3 files changed

+324
-6
lines changed

README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ExampleController extends Controller
7575
*
7676
* @return \Http\Request
7777
*/
78-
public function index(Request $request)
78+
public static function index(Request $request)
7979
{
8080
//
8181
}
@@ -85,7 +85,7 @@ class ExampleController extends Controller
8585
*
8686
* @param \Http\Request $request
8787
*/
88-
public function store(Request $request)
88+
public static function store(Request $request)
8989
{
9090
//
9191
}
@@ -95,7 +95,7 @@ class ExampleController extends Controller
9595
*
9696
* @param \Http\Request $request
9797
*/
98-
public function show(Request $request)
98+
public static function show(Request $request)
9999
{
100100
//
101101
}
@@ -105,7 +105,7 @@ class ExampleController extends Controller
105105
*
106106
* @param \Http\Request $request
107107
*/
108-
public function update(Request $request)
108+
public static function update(Request $request)
109109
{
110110
//
111111
}
@@ -115,11 +115,29 @@ class ExampleController extends Controller
115115
*
116116
* @param \Http\Request $request
117117
*/
118-
public function destroy(Request $request)
118+
public static function destroy(Request $request)
119119
{
120120
//
121121
}
122122
}
123+
124+
```
125+
Example Model
126+
-------
127+
```php
128+
129+
<?php
130+
131+
namespace App\Models;
132+
133+
class ExampleModel extends Model
134+
{
135+
/**
136+
* @var array
137+
*/
138+
protected $fillable = [];
139+
}
140+
123141
```
124142
## Contributing
125143
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

app/Routing/Route.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ private function request_url()
106106
return Response::json(HttpException::HttpMethodNotAllowedException());
107107
}
108108

109-
$URL = (isset($_SERVER["HTTPS"]) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
109+
$URL = preg_replace('/%20/', '', (isset($_SERVER["HTTPS"]) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
110+
110111
$URL = filter_var($URL, FILTER_SANITIZE_URL);
111112
if (!filter_var($URL, FILTER_VALIDATE_URL)) {
112113
return Response::json(HttpException::HttpBadRequestException());

test/Middleware.php

Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Countable;
6+
7+
8+
class Middleware
9+
{
10+
/**
11+
* The message to be applied to the data.
12+
*
13+
* @var array
14+
*/
15+
protected $attribute = [];
16+
17+
18+
/**
19+
* Validate the given request with the given rules.
20+
*
21+
* @param string|JSON $request
22+
* @param array $rules
23+
* @return array
24+
*/
25+
public function validate($request, array $rules = [])
26+
{
27+
28+
if (!$this->validateJson($request)) {
29+
return (object)["error" => true, "message" => 'Content must be json'];
30+
}
31+
32+
$request = json_decode($request, true);
33+
34+
if (!$this->array_matche(array_keys($request), array_keys($rules))) {
35+
return (object)["error" => true, "message" => 'Content is not specified or specified incorrectly.'];
36+
}
37+
38+
foreach ($request as $key => $_) {
39+
$this->attribute[$key] = [];
40+
foreach ($this->explodeExplicitRule($rules[$key]) as $v) {
41+
switch (trim($v)) {
42+
case '':
43+
break;
44+
case 'required':
45+
if (!$this->validateRequired($request[$key])) {
46+
array_push($this->attribute[$key], [$v => "The $key field is required."]);
47+
}
48+
break;
49+
case 'email':
50+
if (!$this->validateEmail($request[$key])) {
51+
array_push($this->attribute[$key], [$v => "Please enter a valid email address."]);
52+
}
53+
break;
54+
case 'integer':
55+
if (!$this->validateInteger($request[$key])) {
56+
array_push($this->attribute[$key], [$v => "The $key must be integer"]);
57+
}
58+
break;
59+
case 'date':
60+
if (!$this->validateDate($request[$key])) {
61+
array_push($this->attribute[$key], [$v => "The $key is not valid date"]);
62+
}
63+
case preg_match('/\w+:\d+/', $v) ? true : false:
64+
$len = explode(':', $v, 2);
65+
$attr = $len[0];
66+
$num = $len[1];
67+
switch ($attr) {
68+
case 'max':
69+
if (!$this->validateLen("max", $request[$key], $len[1])) {
70+
array_push($this->attribute[$key], [$attr => "The $key must not be greater than $num characters."]);
71+
}
72+
break;
73+
case 'min':
74+
if (!$this->validateLen("min", $request[$key], $len[1])) {
75+
array_push($this->attribute[$key], [$attr => "The $key must be at least $num characters."]);
76+
}
77+
break;
78+
}
79+
break;
80+
}
81+
}
82+
}
83+
84+
$att = array_filter($this->attribute);
85+
return (object)["error" => !empty($att), "attribute" => array_filter($att)];
86+
}
87+
88+
89+
/**
90+
* Explode the explicit rule into an array if necessary.
91+
*
92+
* @param mixed $rule
93+
* @return array
94+
*/
95+
public function explodeExplicitRule($rule): array
96+
{
97+
return is_string($rule) ? explode('|', $rule) : [];
98+
}
99+
100+
/**
101+
* Validate that an attribute is a valid e-mail address.
102+
*
103+
* @param mixed $value
104+
* @return bool
105+
*/
106+
public function validateEmail($value): bool
107+
{
108+
if (is_string($value)) {
109+
return preg_match("/[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+.[a-zA-Z]{2,4}/", $value) && filter_var($value, FILTER_VALIDATE_EMAIL);
110+
}
111+
return false;
112+
}
113+
114+
/**
115+
* Validate the attribute is a valid JSON string.
116+
*
117+
* @param mixed $value
118+
* @return bool
119+
*/
120+
public function validateJson($value): bool
121+
{
122+
if (is_array($value)) {
123+
return false;
124+
}
125+
126+
if (!is_scalar($value) && !is_null($value) && !method_exists($value, '__toString')) {
127+
return false;
128+
}
129+
130+
json_decode($value);
131+
132+
return json_last_error() === JSON_ERROR_NONE;
133+
}
134+
135+
public function array_matche($array, $_array): bool
136+
{
137+
return array_diff($array, $_array) == array_diff($_array, $array);
138+
}
139+
140+
/**
141+
* Validate that a required attribute exists.
142+
*
143+
* @param mixed $value
144+
* @return bool
145+
*/
146+
public function validateRequired($value): bool
147+
{
148+
if (is_null($value)) {
149+
return false;
150+
} elseif (is_string($value) && trim($value) === '') {
151+
return false;
152+
} elseif ((is_array($value) || $value instanceof Countable) && count($value) < 1) {
153+
return false;
154+
}
155+
return true;
156+
}
157+
158+
/**
159+
* Validate the size of an attribute is greater than a minimum value.
160+
*
161+
* @param string $attribute
162+
* @param mixed $value
163+
* @param int $len
164+
* @return bool
165+
*/
166+
public function validateLen($attribute, $value, $len): bool
167+
{
168+
if (strtolower($attribute) === 'max') {
169+
return strlen(trim($value)) <= $len;
170+
}
171+
if (strtolower($attribute) === 'min') {
172+
return strlen(trim($value)) >= $len;
173+
}
174+
}
175+
176+
/**
177+
* Validate that an attribute is an integer.
178+
*
179+
* @param mixed $value
180+
* @return bool
181+
*/
182+
public function validateInteger($value): bool
183+
{
184+
return filter_var($value, FILTER_VALIDATE_INT) !== false;
185+
}
186+
187+
/**
188+
* Validate that an attribute is a valid date.
189+
*
190+
* @param mixed $value
191+
* @return bool
192+
*/
193+
public function validateDate($value): bool
194+
{
195+
196+
if ((!is_string($value) && !is_numeric($value))) {
197+
return false;
198+
}
199+
200+
$date = date_parse($value);
201+
return checkdate($date['month'], $date['day'], $date['year']);
202+
}
203+
204+
/**
205+
* Store the uploaded file on a filesystem disk.
206+
*
207+
* @param string $name
208+
*/
209+
public function upload($name = '')
210+
{
211+
$exist = $_FILES[$name] ?? false;
212+
213+
if ($exist === false) {
214+
return $this->getInfo(false, "No file sent.");
215+
}
216+
217+
$file = (object)$_FILES[$name];
218+
219+
if ($file->error !== UPLOAD_ERR_OK) {
220+
return $this->getInfo(false, "UPLOAD_ERR_OK");
221+
}
222+
223+
if ($file->size > 1000000) {
224+
return $this->getInfo(false, "Exceeded filesize limit.");
225+
}
226+
227+
$allow = $this->validateType($this->extension($file->name));
228+
229+
if ($allow === false) {
230+
return $this->getInfo(false, "Type of files are not allowed.");
231+
}
232+
233+
$nm = $this->hashName() . $this->extension($file->name);
234+
$dir = __DIR__ . '/storage/' . $nm;
235+
236+
if (move_uploaded_file($file->tmp_name, $dir)) {
237+
return $this->getInfo(true, $nm);
238+
} else {
239+
return $this->getInfo(false, "Failed to move uploaded file.");
240+
}
241+
}
242+
243+
/**
244+
* Store the uploaded file on a filesystem disk.
245+
*
246+
* @param string $path
247+
*/
248+
public function removeUploaded($path = null)
249+
{
250+
$dir = __DIR__ . '/storage/' . $path;
251+
if (unlink($dir)) {
252+
return $this->getInfo(true, "Removed.");
253+
} else {
254+
return $this->getInfo(false, "something went wrong!");
255+
}
256+
}
257+
258+
/**
259+
* Get a filename for the file.
260+
*
261+
* @return string
262+
*/
263+
public function hashName()
264+
{
265+
return time() . '.' . uniqid() . '.';
266+
}
267+
268+
/**
269+
* Get the file's extension.
270+
* @param string $name
271+
* @return string
272+
*/
273+
public function extension($name): string
274+
{
275+
$ext = strtolower(explode('.', $name)[1]);
276+
return $ext ?? "";
277+
}
278+
279+
/**
280+
* Validate the MIME type of a file is an image MIME type.
281+
*
282+
* @param mixed $value
283+
* @return bool
284+
*/
285+
public function validateType($value): bool
286+
{
287+
return in_array($value, ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp']);
288+
}
289+
290+
public function getInfo($status, $message)
291+
{
292+
return (object)["status" => $status, "message" => $message];
293+
}
294+
295+
public function rateLimit()
296+
{
297+
//
298+
}
299+
}

0 commit comments

Comments
 (0)