-
Notifications
You must be signed in to change notification settings - Fork 67
/
RequestCreator.php
61 lines (45 loc) · 1.52 KB
/
RequestCreator.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
<?php
namespace Unlu\Laravel\Api;
use Illuminate\Http\Request;
class RequestCreator
{
public static function createWithParameters($params = [])
{
return self::createCustomRequest($params);
}
private static function createCustomRequest($get = [], $post = [], $attrs = [], $cookies = [], $files = [], $server = [])
{
if (count($get) == 0) {
$get = $_GET;
}
if (count($post) == 0) {
$post = $_POST;
}
if (count($cookies) == 0) {
$cookies = $_COOKIE;
}
if (count($files) == 0) {
$files = $_FILES;
}
if (count($server) == 0) {
$server = $_SERVER;
}
$requestUri = $_SERVER['PATH_INFO'];
$requestQueryString = '';
if (count($get) > 0) {
$requestQueryString .= '?';
foreach ($get as $paramKey => $paramValue) {
preg_match(UriParser::getPattern(), $paramValue, $matches);
if (count($matches) == 0) {
$paramValue = sprintf('=%s', $paramValue);
}
$requestQueryString .= sprintf('%s%s', $paramKey, $paramValue) . '&';
}
}
if (substr($requestQueryString, -1) == '&') {
$requestQueryString = substr($requestQueryString, 0, strlen($requestQueryString) - 1);
}
$server['REQUEST_URI'] = $requestUri . $requestQueryString;
return new Request($get, $post, $attrs, $cookies, $files, $server);
}
}