Test Failed
Push — develop ( 990383...34f5fa )
by nguereza
04:21
created

RequestData::setAutoEscape()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 4
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   https://www.platine-php.com
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
     * The request body or post data
61
     * @var array<string, mixed>
62
     */
63
    protected array $posts = [];
64
65
    /**
66
     * The request get data
67
     * @var array<string, mixed>
68
     */
69
    protected array $gets = [];
70
71
    /**
72
     * The request servers environment data
73
     * @var array<string, mixed>
74
     */
75
    protected array $servers = [];
76
77
    /**
78
     * The request cookies data
79
     * @var array<string, mixed>
80
     */
81
    protected array $cookies = [];
82
83
    /**
84
     * The request files data
85
     * @var array<string|int, mixed|UploadedFileInterface>
86
     */
87
    protected array $files = [];
88
89
    /**
90
     * Create new instance
91
     * @param ServerRequestInterface $request
92
     */
93
    public function __construct(ServerRequestInterface $request)
94
    {
95
        $this->posts = (array) $request->getParsedBody();
96
        $this->gets = $request->getQueryParams();
97
        $this->servers = $request->getServerParams();
98
        $this->cookies = $request->getCookieParams();
99
        $this->files = $request->getUploadedFiles();
100
    }
101
102
    /**
103
     * Return the post data
104
     * @param bool $autoEscape whether to apply XSS rule on inputs
105
     * @return array<string, mixed>
106
     */
107
    public function posts(bool $autoEscape = true): array
108
    {
109
        return $this->applyInputClean($this->posts, $autoEscape);
110
    }
111
112
    /**
113
     * Return the get data
114
     * @param bool $autoEscape whether to apply XSS rule on inputs
115
     * @return array<string, mixed>
116
     */
117
    public function gets(bool $autoEscape = true): array
118
    {
119
        return $this->applyInputClean($this->gets, $autoEscape);
120
    }
121
122
    /**
123
     * Return the files data
124
     * @return array<string|int, mixed|UploadedFileInterface>
125
     */
126
    public function files(): array
127
    {
128
        return $this->files;
129
    }
130
131
    /**
132
     * Return the server data
133
     * @param bool $autoEscape whether to apply XSS rule on inputs
134
     * @return array<string, mixed>
135
     */
136
    public function servers(bool $autoEscape = true): array
137
    {
138
        return $this->applyInputClean($this->servers, $autoEscape);
139
    }
140
141
    /**
142
     * Return the cookie data
143
     * @param bool $autoEscape whether to apply XSS rule on inputs
144
     * @return array<string, mixed>
145
     */
146
    public function cookies(bool $autoEscape = true): array
147
    {
148
        return $this->applyInputClean($this->cookies, $autoEscape);
149
    }
150
151
    /**
152
     * Return the request query value for the given key
153
     * @param string $key the key to fetch also support for dot notation
154
     * @param mixed $default
155
     * @param bool $autoEscape whether to apply XSS rule on inputs
156
     *
157
     * @return mixed
158
     */
159
    public function get(
160
        string $key,
161
        mixed $default = null,
162
        bool $autoEscape = true
163
    ): mixed {
164
        $gets = $this->applyInputClean($this->gets, $autoEscape);
165
        return Arr::get($gets, $key, $default);
166
    }
167
168
    /**
169
     * Return the request body or post value for the given key
170
     * @param string $key the key to fetch also support for dot notation
171
     * @param mixed $default
172
     * @param bool $autoEscape whether to apply XSS rule on inputs
173
     *
174
     * @return mixed
175
     */
176
    public function post(
177
        string $key,
178
        mixed $default = null,
179
        bool $autoEscape = true
180
    ): mixed {
181
        $posts = $this->applyInputClean($this->posts, $autoEscape);
182
        return Arr::get($posts, $key, $default);
183
    }
184
185
    /**
186
     * Return the request server value for the given key
187
     * @param string $key the key to fetch also support for dot notation
188
     * @param mixed $default
189
     * @param bool $autoEscape whether to apply XSS rule on inputs
190
     *
191
     * @return mixed
192
     */
193
    public function server(
194
        string $key,
195
        mixed $default = null,
196
        bool $autoEscape = true
197
    ): mixed {
198
        $servers = $this->applyInputClean($this->servers, $autoEscape);
199
        return Arr::get($servers, $key, $default);
200
    }
201
202
    /**
203
     * Return the request cookie value for the given key
204
     * @param string $key the key to fetch also support for dot notation
205
     * @param mixed $default
206
     * @param bool $autoEscape whether to apply XSS rule on inputs
207
     *
208
     * @return mixed
209
     */
210
    public function cookie(
211
        string $key,
212
        mixed $default = null,
213
        bool $autoEscape = true
214
    ): mixed {
215
        $cookies = $this->applyInputClean($this->cookies, $autoEscape);
216
        return Arr::get($cookies, $key, $default);
217
    }
218
219
    /**
220
     * Return the request uploaded file for the given key
221
     * @param string $key the key to fetch also support for dot notation
222
     *
223
     * @return mixed
224
     */
225
    public function file(string $key): mixed
226
    {
227
        $files = $this->files;
228
        return Arr::get($files, $key, null);
229
    }
230
231
    /**
232
     * Apply the input cleanup based on the status
233
     * @param array<mixed> $data
234
     * @param bool $autoEscape whether to apply XSS rule on inputs
235
     * @return array<mixed>
236
     */
237
    protected function applyInputClean(array $data, bool $autoEscape = true): array
238
    {
239
        if ($autoEscape) {
240
            $data = (new InputClean())->clean($data);
241
        }
242
243
        return $data;
244
    }
245
}
246