-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathRouterRequest.php
127 lines (112 loc) · 2.77 KB
/
RouterRequest.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
126
127
<?php
namespace Buki\Router;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class RouterRequest
{
/**
* @var string $validMethods Valid methods for Router
*/
protected $validMethods = 'GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH|ANY|AJAX|XPOST|XPUT|XDELETE|XPATCH';
/**
* @var Request $request
*/
private $request;
/**
* @var Response $response
*/
private $response;
/**
* RouterRequest constructor.
*
* @param Request $request
* @param Response $response
*/
public function __construct(Request $request, Response $response)
{
$this->request = $request;
$this->response = $response;
}
/**
* @return Request
*/
public function symfonyRequest(): Request
{
return $this->request;
}
/**
* @return Response
*/
public function symfonyResponse(): Response
{
return $this->response;
}
/**
* @return string
*/
public function validMethods(): string
{
return $this->validMethods;
}
/**
* Request method validation
*
* @param string $data
* @param string $method
*
* @return bool
*/
public function validMethod(string $data, string $method): bool
{
$valid = false;
if (strstr($data, '|')) {
foreach (explode('|', $data) as $value) {
$valid = $this->checkMethods($value, $method);
if ($valid) {
break;
}
}
} else {
$valid = $this->checkMethods($data, $method);
}
return $valid;
}
/**
* Get the request method used, taking overrides into account
*
* @return string
*/
public function getMethod(): string
{
$method = $this->request->getMethod();
$formMethod = $this->request->request->get('_method');
if (!empty($formMethod)) {
$method = strtoupper($formMethod);
}
return $method;
}
/**
* check method valid
*
* @param string $value
* @param string $method
*
* @return bool
*/
protected function checkMethods(string $value, string $method): bool
{
if (in_array($value, explode('|', $this->validMethods))) {
if ($this->request->isXmlHttpRequest() && $value === 'AJAX') {
return true;
}
if ($this->request->isXmlHttpRequest() && strpos($value, 'X') === 0
&& $method === ltrim($value, 'X')) {
return true;
}
if (in_array($value, [$method, 'ANY'])) {
return true;
}
}
return false;
}
}