This repository has been archived by the owner on Apr 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequestFactory.php
92 lines (83 loc) · 2.23 KB
/
RequestFactory.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
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Transaction\Factory;
use Ulrack\Transaction\Common\MethodEnum;
use Ulrack\Transaction\Component\Request;
use Ulrack\Transaction\Common\RequestInterface;
use Ulrack\Transaction\Exception\InvalidRequestException;
class RequestFactory
{
/**
* Creates a Web request.
*
* @param array $server The server parameters.
* @param array $get The get parameters.
* @param array $post The post payload.
*
* @return RequestInterface
*
* @throws InvalidRequestException When a invalid request method is detected.
*/
public static function create(
array $server,
array $get = [],
array $post = []
): RequestInterface {
$methods = MethodEnum::getOptions();
if (!isset($methods[$server['REQUEST_METHOD']])) {
throw new InvalidRequestException('Invalid request method.');
}
return new Request(
new MethodEnum($methods[$server['REQUEST_METHOD']]),
strtok($server['REQUEST_URI'], '?'),
$get,
$post,
static::parseHeaders($server)
);
}
/**
* Parse the headers to normalized key value pairs.
*
* @param array $server
*
* @return array
*/
private static function parseHeaders(array $server): array
{
$headers = [];
foreach ($server as $key => $header) {
if (substr($key, 0, 5) === 'HTTP_') {
$headers[static::normalizeKey(substr($key, 5))] = $header;
continue;
}
$headers[static::normalizeKey($key)] = $header;
}
return $headers;
}
/**
* Normalize key name to standard format.
*
* @param string $key
*
* @return string
*/
private static function normalizeKey(string $key): string
{
return str_replace(
' ',
'-',
ucwords(
strtolower(
str_replace(
'_',
' ',
$key
)
)
)
);
}
}