-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRequest.php
executable file
·125 lines (111 loc) · 2.92 KB
/
Request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Ipaas\Gapp;
use Illuminate\Http\Request as BaseRequest;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Illuminate\Http\Response;
/**
* Class Request
*
* @package Ipaas
*/
class Request extends BaseRequest
{
/**
* Request constructor.
* @param array $query
* @param array $request
* @param array $attributes
* @param array $cookies
* @param array $files
* @param array $server
* @param null $content
*/
public function __construct(
array $query = array(),
array $request = array(),
array $attributes = array(),
array $cookies = array(),
array $files = array(),
array $server = array(),
$content = null
) {
// temp store data
$request_date = request()->all();
// construct new request
parent::__construct(
request()->query->all(),
request()->request->all(),
request()->attributes->all(),
request()->cookies->all(),
request()->files->all(),
request()->server->all(),
request()->content
);
// reset temp data
$this->replace($request_date);
}
/**
* @param string $item
* @return $this
*/
public function boolify(string $item)
{
if ($this->has($item)) {
$list = $this->all();
$object = $list[$item];
if (strtolower($object) === 'true') {
$object = true;
$list[$item] = $object;
} elseif (strtolower($object) === 'false') {
$object = false;
$list[$item] = $object;
}
$this->replace($list);
}
return $this;
}
/**
* @param string $item
* @return $this
*/
public function arrify(string $item)
{
if ($this->has($item)) {
$list = $this->all();
$object = $list[$item];
if ($object !== null) {
$list[$item] = is_array($object) ? $object : explode(',', $object);
}
request()->replace($list);
}
return $this;
}
/**
* @param string $item
* @param mixed $value
* @return $this
*/
public function requestify(string $item, $value)
{
$list = $this->all();
$list[$item] = $value;
request()->replace($list);
return $this;
}
/**
* @param array $rules
* @return Request
* @throws ValidationException
* @throws \Exception
*/
public function validate(array $rules)
{
$list = $this->all();
$validator = Validator::make($list, $rules);
if ($validator->fails()) {
throw new ValidationException($validator, new Response('Invalid request', Response::HTTP_UNPROCESSABLE_ENTITY));
}
return $this;
}
}