1 | <?php |
||
11 | class Request |
||
12 | { |
||
13 | private $parameters; |
||
14 | private $headers; |
||
15 | |||
16 | 3 | public function __construct(array $parameters, array $headers) |
|
21 | |||
22 | 2 | public static function fromMessage(MessageInterface $message): Request |
|
23 | { |
||
24 | 2 | if ($message->getHeaderLine('content-type') === 'application/json') { |
|
25 | $parameters = (array) json_decode($message->getBody()->getContents(), true); |
||
26 | 2 | } elseif ($message instanceof ServerRequestInterface) { |
|
27 | 1 | $parameters = array_merge($message->getQueryParams(), (array) $message->getParsedBody()); |
|
28 | } else { |
||
29 | 2 | $parameters = (array) json_decode($message->getBody()->getContents(), true); |
|
30 | } |
||
31 | |||
32 | 2 | return new static($parameters, $message->getHeaders()); |
|
33 | } |
||
34 | |||
35 | /** |
||
36 | * Get request parameters. |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | 1 | public function getParameters(): array |
|
44 | |||
45 | /** |
||
46 | * Get single parameter. |
||
47 | * |
||
48 | * @param string $key |
||
49 | * @param mixed $default |
||
50 | * |
||
51 | * @return mixed |
||
52 | */ |
||
53 | 1 | public function getParameter(string $key, $default = null) |
|
57 | |||
58 | /** |
||
59 | * Determine if request has one or more parameters. |
||
60 | * |
||
61 | * @param array|string $keys |
||
62 | * |
||
63 | * @return bool |
||
64 | */ |
||
65 | 1 | public function hasParameters($keys): bool |
|
69 | |||
70 | /** |
||
71 | * Get request headers. |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | 1 | public function getHeaders(): array |
|
79 | |||
80 | /** |
||
81 | * Get single request header. |
||
82 | * |
||
83 | * @param string $key |
||
84 | * @param mixed $default |
||
85 | * |
||
86 | * @return mixed |
||
87 | */ |
||
88 | 1 | public function getHeader(string $key, $default = null) |
|
92 | } |
||
93 |