Passed
Push — develop ( 089cb5...597d05 )
by nguereza
05:42
created

RequestData::cleanInput()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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