Test Failed
Push — develop ( c753fd...f013d6 )
by nguereza
02:45
created

RequestData::servers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file RequestData.php
34
 *
35
 *  This class contains the methods to query request data
36
 *
37
 *  @package    Platine\Framework\Http
38
 *  @author Platine Developers team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   http://www.iacademy.cf
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework\Http;
49
50
use Platine\Http\ServerRequestInterface;
51
use Platine\Stdlib\Helper\Arr;
52
53
/**
54
 * @class RequestData
55
 * @package Platine\Framework\Http
56
 */
57
class RequestData
58
{
59
60
    /**
61
     * The request body or post data
62
     * @var array<string, mixed>
63
     */
64
    protected array $posts = [];
65
66
    /**
67
     * The request get data
68
     * @var array<string, mixed>
69
     */
70
    protected array $gets = [];
71
72
    /**
73
     * The request servers environment data
74
     * @var array<string, mixed>
75
     */
76
    protected array $servers = [];
77
78
    /**
79
     * The request cookies data
80
     * @var array<string, mixed>
81
     */
82
    protected array $cookies = [];
83
84
    /**
85
     * Whether to apply input cleanup
86
     * @var bool
87
     */
88
    protected bool $autoEscape = true;
89
90
    /**
91
     * Create new instance
92
     * @param ServerRequestInterface $request
93
     */
94
    public function __construct(ServerRequestInterface $request)
95
    {
96
        $this->posts = (array) $request->getParsedBody();
97
        $this->gets = $request->getQueryParams();
98
        $this->servers = $request->getServerParams();
99
        $this->cookies = $request->getCookieParams();
100
    }
101
102
    /**
103
     * Set clean input status
104
     * @param bool $autoEscape
105
     * @return $this
106
     */
107
    public function setAutoEscape(bool $autoEscape = true): self
108
    {
109
        $this->autoEscape = $autoEscape;
110
        return $this;
111
    }
112
113
114
    /**
115
     * Return the post data
116
     * @return array<string, mixed>
117
     */
118
    public function posts(): array
119
    {
120
        return $this->applyInputClean($this->posts);
121
    }
122
123
    /**
124
     * Return the get data
125
     * @return array<string, mixed>
126
     */
127
    public function gets(): array
128
    {
129
        return $this->applyInputClean($this->gets);
130
    }
131
132
    /**
133
     * Return the server data
134
     * @return array<string, mixed>
135
     */
136
    public function servers(): array
137
    {
138
        return $this->applyInputClean($this->servers);
139
    }
140
141
    /**
142
     * Return the cookie data
143
     * @return array<string, mixed>
144
     */
145
    public function cookies(): array
146
    {
147
        return $this->applyInputClean($this->cookies);
148
    }
149
150
    /**
151
     * Return the request query value for the given key
152
     * @param string $key the key to fetch also support for dot notation
153
     * @param mixed $default
154
     *
155
     * @return mixed
156
     */
157
    public function get(string $key, $default = null)
158
    {
159
        $gets = $this->applyInputClean($this->gets);
160
        return Arr::get($gets, $key, $default);
161
    }
162
163
    /**
164
     * Return the request body or post value for the given key
165
     * @param string $key the key to fetch also support for dot notation
166
     * @param mixed $default
167
     *
168
     * @return mixed
169
     */
170
    public function post(string $key, $default = null)
171
    {
172
        $posts = $this->applyInputClean($this->posts);
173
        return Arr::get($posts, $key, $default);
174
    }
175
176
    /**
177
     * Return the request server value for the given key
178
     * @param string $key the key to fetch also support for dot notation
179
     * @param mixed $default
180
     *
181
     * @return mixed
182
     */
183
    public function server(string $key, $default = null)
184
    {
185
        $servers = $this->applyInputClean($this->servers);
186
        return Arr::get($servers, $key, $default);
187
    }
188
189
    /**
190
     * Return the request cookie value for the given key
191
     * @param string $key the key to fetch also support for dot notation
192
     * @param mixed $default
193
     *
194
     * @return mixed
195
     */
196
    public function cookie(string $key, $default = null)
197
    {
198
        $cookies = $this->applyInputClean($this->cookies);
199
        return Arr::get($cookies, $key, $default);
200
    }
201
202
    /**
203
     * Clean the user input, like to prevent XSS, etc.
204
     * @param mixed $str
205
     * @return mixed
206
     */
207
    protected function cleanInput($str)
208
    {
209
        if (is_array($str)) {
210
            return array_map([$this, 'cleanInput'], $str);
211
        }
212
        if (is_object($str)) {
213
            $obj = $str;
214
            foreach ($str as $var => $value) {
215
                $obj->{$var} = $this->cleanInput($value);
216
            }
217
            return $obj;
218
        }
219
        return htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8');
220
    }
221
222
    /**
223
     * Apply the input cleanup based on the status
224
     * @param array<mixed> $data
225
     * @return array<mixed>
226
     */
227
    protected function applyInputClean(array $data): array
228
    {
229
        if ($this->autoEscape) {
230
            $data = $this->cleanInput($data);
231
        }
232
233
        return $data;
234
    }
235
}
236