Completed
Push — master ( 96e614...1e74ba )
by Vladimir
03:46
created

Request::getParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Http;
6
7
use FondBot\Helpers\Arr;
8
use Psr\Http\Message\MessageInterface;
9
10
class Request
11
{
12
    private $parameters;
13
    private $headers;
14
15 1
    public function __construct(array $parameters, array $headers)
16
    {
17 1
        $this->parameters = $parameters;
18 1
        $this->headers = $headers;
19 1
    }
20
21
    public static function fromMessage(MessageInterface $message): Request
22
    {
23
        return new static(
24
            json_decode($message->getBody()->getContents(), true),
25
            $message->getHeaders()
26
        );
27
    }
28
29
    /**
30
     * Get request parameters.
31
     *
32
     * @return array|null
33
     */
34
    public function getParameters(): ?array
35
    {
36
        return $this->parameters;
37
    }
38
39
    /**
40
     * Get single parameter.
41
     *
42
     * @param string $key
43
     * @param mixed  $default
44
     *
45
     * @return mixed
46
     */
47
    public function getParameter(string $key, $default = null)
48
    {
49
        return Arr::get($this->parameters, $key, $default);
50
    }
51
52
    /**
53
     * Determine if request has one or more parameters.
54
     *
55
     * @param array|string $keys
56
     *
57
     * @return bool
58
     */
59
    public function hasParameters($keys): bool
60
    {
61
        return Arr::has($this->parameters, (array) $keys);
62
    }
63
64
    /**
65
     * Get request headers.
66
     *
67
     * @return array
68
     */
69
    public function getHeaders(): array
70
    {
71
        return $this->headers;
72
    }
73
74
    /**
75
     * Get single request header.
76
     *
77
     * @param string $key
78
     * @param mixed  $default
79
     *
80
     * @return mixed
81
     */
82
    public function getHeader(string $key, $default = null)
83
    {
84
        return Arr::get($this->headers, $key, $default);
85
    }
86
}
87