Passed
Pull Request — master (#244)
by Alex
22:10
created

IncomingIlluminateRequest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlgoWeb\PODataLaravel\OperationContext\Web\Illuminate;
6
7
use Illuminate\Http\Request;
8
use POData\OperationContext\HTTPRequestMethod;
9
use POData\OperationContext\IHTTPRequest;
10
11
/**
12
 * Class IncomingIlluminateRequest.
13
 * @package POData\OperationContext\Web\Illuminate
14
 */
15
class IncomingIlluminateRequest implements IHTTPRequest
16
{
17
    /**
18
     * The Illuminate request.
19
     *
20
     * @var Request
21
     */
22
    private $request;
23
24
    /**
25
     * The request headers.
26
     *
27
     * @var array
28
     */
29
    private $headers = [];
30
31
    /**
32
     * The incoming url in raw format.
33
     *
34
     * @var string
35
     */
36
    private $rawUrl = null;
37
38
    /**
39
     * The request method (GET, POST, PUT, DELETE or MERGE).
40
     *
41
     * @var HTTPRequestMethod HttpVerb
42
     */
43
    private $method;
44
45
    /**
46
     * The query options as key value.
47
     *
48
     * @var array(string, string);
49
     */
50
    private $queryOptions = [];
51
52
    /**
53
     * A collection that represents mapping between query
54
     * option and its count.
55
     *
56
     * @var array(string, int)
57
     */
58
    private $queryOptionsCount = [];
59
60
    /**
61
     * IncomingIlluminateRequest constructor.
62
     *
63
     * @param Request $request
64
     */
65
    public function __construct(Request $request)
66
    {
67
        $this->request           = $request;
68
        $this->headers           = [];
69
        $this->queryOptions      = [];
70
        $this->queryOptionsCount = [];
71
        $this->method            = new HTTPRequestMethod($this->request->getMethod());
72
    }
73
74
    /**
75
     * @return string RequestURI called by User with the value of QueryString
76
     */
77
    public function getRawUrl(): string
78
    {
79
        $this->rawUrl = $this->request->fullUrl();
80
81
        return $this->rawUrl;
82
    }
83
84
    /**
85
     * @param string $key The header name
86
     *
87
     * @return array|null|string
88
     */
89
    public function getRequestHeader(string $key)
90
    {
91
        $result = $this->request->header($key);
92
        //Zend returns false for a missing header...POData needs a null
93
        if (false === $result || '' === $result) {
94
            return null;
95
        }
96
97
        return $result;
98
    }
99
100
    /**
101
     * Returns the Query String Parameters (QSPs) as an array of KEY-VALUE pairs.  If a QSP appears twice
102
     * it will have two entries in this array.
103
     *
104
     * @return array
105
     */
106
    public function getQueryParameters(): array
107
    {
108
        //TODO: the contract is more specific than this, it requires the name and values to be decoded
109
        //not sure how to test that...
110
        //Have to convert to the stranger format known to POData that deals with multiple query strings.
111
        //this makes this request a bit non compliant as it doesn't expose duplicate keys, something POData will
112
        //check for instead whatever parameter was last in the query string is set.  IE
113
        //odata.svc/?$format=xml&$format=json the format will be json
114
        $this->queryOptions      = [];
115
        $this->queryOptionsCount = [];
116
117
        foreach ($this->request->all() as $key => $value) {
118
            $keyBitz              = explode(';', strval($key));
119
            $newKey               = strtolower($keyBitz[count($keyBitz) - 1]);
120
            $this->queryOptions[] = [$newKey => $value];
121
            if (!array_key_exists($key, $this->queryOptionsCount)) {
122
                $this->queryOptionsCount[$newKey] = 0;
123
            }
124
            $this->queryOptionsCount[$newKey]++;
125
        }
126
127
        return $this->queryOptions;
128
    }
129
130
    /**
131
     * @return HTTPRequestMethod
132
     */
133
    public function getMethod(): HTTPRequestMethod
134
    {
135
        return $this->method;
136
    }
137
138
    /**
139
     * @return resource|string|array
140
     */
141
    public function getAllInput()
142
    {
143
        $content = $this->request->all();
144
        return !empty($content) ? $content : $this->request->getContent();
145
    }
146
}
147