-
-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathPsrRequestToGenericRequestMapper.php
More file actions
106 lines (89 loc) · 3.25 KB
/
PsrRequestToGenericRequestMapper.php
File metadata and controls
106 lines (89 loc) · 3.25 KB
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
<?php
declare(strict_types=1);
namespace Tempest\Http\Mappers;
use Psr\Http\Message\ServerRequestInterface as PsrRequest;
use Psr\Http\Message\UploadedFileInterface;
use Tempest\Cryptography\Encryption\Encrypter;
use Tempest\Http\Cookie\Cookie;
use Tempest\Http\Cookie\CookieConfig;
use Tempest\Http\Cookie\CookieManager;
use Tempest\Http\GenericRequest;
use Tempest\Http\Method;
use Tempest\Http\RequestHeaders;
use Tempest\Http\Upload;
use Tempest\Mapper\Mapper;
use Tempest\Support\Arr;
use Throwable;
use function Tempest\Mapper\map;
use function Tempest\Support\arr;
final readonly class PsrRequestToGenericRequestMapper implements Mapper
{
public function __construct(
private Encrypter $encrypter,
private CookieManager $cookies,
private CookieConfig $cookieConfig,
) {}
public function canMap(mixed $from, mixed $to): bool
{
return false;
}
public function map(mixed $from, mixed $to): GenericRequest
{
/** @var PsrRequest $from */
$data = (array) $from->getParsedBody();
$raw = $from->getBody()->getContents();
if (arr($from->getHeader('content-type'))->hasValue('application/json') && json_validate($raw)) {
$data = [...$data, ...json_decode($raw, associative: true)];
}
$headersAsString = array_map(
fn (array $items) => implode(',', $items),
$from->getHeaders(),
);
parse_str($from->getUri()->getQuery(), $query);
$uploads = array_map(
fn (UploadedFileInterface $uploadedFile) => new Upload($uploadedFile),
$from->getUploadedFiles(),
);
return map([
'method' => $this->requestMethod($from, $data),
'uri' => (string) $from->getUri(),
'raw' => $raw,
'body' => $data,
'headers' => RequestHeaders::normalizeFromArray($headersAsString),
'path' => $from->getUri()->getPath(),
'query' => $query,
'files' => $uploads,
'cookies' => Arr\filter(Arr\map(
array: $_COOKIE,
map: function (string $rawValue, string $key) {
try {
$value = \in_array($key, $this->cookieConfig->plaintextCookies, true)
? $rawValue
: $this->encrypter->decrypt($rawValue);
return new Cookie(
key: $key,
value: $value,
);
} catch (Throwable) {
if ($this->cookieConfig->discardUnencryptedCookies) {
$this->cookies->remove($key);
}
return null;
}
},
)),
])
->to(GenericRequest::class);
}
private function requestMethod(PsrRequest $request, array $data): Method
{
$originalMethod = Method::from($request->getMethod());
if ($originalMethod !== Method::POST) {
return $originalMethod;
}
if (! isset($data['_method'])) {
return $originalMethod;
}
return Method::trySpoofingFrom($data['_method']) ?? $originalMethod;
}
}