Passed
Push — main ( 83db5a...4a3517 )
by James Ekow Abaka
01:19
created

Input::server()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
/*
4
 * Ntentan Framework
5
 * Copyright (c) 2008-2015 James Ekow Abaka Ainooson
6
 * 
7
 * Permission is hereby granted, free of charge, to any person obtaining
8
 * a copy of this software and associated documentation files (the
9
 * "Software"), to deal in the Software without restriction, including
10
 * without limitation the rights to use, copy, modify, merge, publish,
11
 * distribute, sublicense, and/or sell copies of the Software, and to
12
 * permit persons to whom the Software is furnished to do so, subject to
13
 * the following conditions:
14
 * 
15
 * The above copyright notice and this permission notice shall be
16
 * included in all copies or substantial portions of the Software.
17
 * 
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
25
 * 
26
 */
27
28
namespace ntentan\utils;
29
30
/**
31
 * An input filter class which is wraped around PHP's `filter_input` and
32
 * `filter_input_array` classes. This class provides methods which allow safe
33
 * and secure access to data passed to an application. 
34
 *
35
 * @author James Ainooson
36
 */
37
class Input
38
{
39
    /**
40
     * Constant for POST request.
41
     */
42
    const POST = INPUT_POST;
43
44
    /**
45
     * Constant for GET request.
46
     */
47
    const GET = INPUT_GET;
48
49
    //const REQUEST = INPUT_REQUEST;
50
51
    const REQUEST_DECODER_INTERNAL = "Input::decodeInternal";
52
53
    const REQUEST_DECODER_PHP = "Input::decodePhp";
54
55
    private static $decoder = self::REQUEST_DECODER_PHP;
0 ignored issues
show
introduced by
The private property $decoder is not used, and could be removed.
Loading history...
56
57
    /**
58
     * Cache or arrays which hold decoded query strings.
59
     *
60
     * @var array
61
     */
62
    private static $arrays = [];
63
64
    /**
65
     * Does the actual work of calling either the filter_input of 
66
     * filter_input_array. Calls the filter_input when a data key is provided
67
     * and callse the filte_input_array when a data key is absent.
68
     * 
69
     * @param string $input Input type
70
     * @param string $key The data key
71
     * @return string|array The value.
72
     */
73
    private static function getVariable(int $input, string $key = null): string | array
74
    {
75
        if ($key === null) {
76
            if (!isset(self::$arrays[$input])) {
77
                self::$arrays[$input] = filter_input_array($input) ?? [];
78
            }
79
            $return = self::$arrays[$input];
80
        } else {
81
            $return = filter_input($input, $key) ?? "";
82
        }
83
84
        if ($return === null && $key === null) {
85
            $return = array();
86
        }
87
88
        return $return;
89
    }
90
91
    /**
92
     * Retrieves GET request variables.
93
     * 
94
     * @param string $key
95
     * @return string|array
96
     */
97
    public static function get(string $key = null)
98
    {
99
        return self::getVariable(INPUT_GET, $key);
100
    }
101
102
    /**
103
     * Retrieves post request variables.
104
     * 
105
     * @param string $key
106
     * @return string|array
107
     */
108
    public static function post(string $key = null)
109
    {
110
        return self::getVariable(INPUT_POST, $key);
111
    }
112
113
    /**
114
     * Retrieves server variables.
115
     * 
116
     * @param string $key
117
     * @return string|array
118
     */
119
    // public static function server(string $key = null): string | array
120
    // {
121
    //     //return self::getVariable(INPUT_SERVER, $key);
122
    //     if ($key === null) {
123
124
    //     }
125
    // }
126
127
    /**
128
     * Retrieves cookie variables.
129
     * 
130
     * @param string $key
131
     * @return string|array
132
     */
133
    public static function cookie(string $key = null)
134
    {
135
        return self::getVariable(INPUT_COOKIE, $key);
136
    }
137
138
    /**
139
     * Checks if a particular key exists in a given request query.
140
     *
141
     * @param string $input
142
     * @param string $key
143
     * @return bool
144
     */
145
    public static function exists(string $input, string $key): bool
146
    {
147
        return isset(self::getVariable($input, null)[$key]);
0 ignored issues
show
Bug introduced by
$input of type string is incompatible with the type integer expected by parameter $input of ntentan\utils\Input::getVariable(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
        return isset(self::getVariable(/** @scrutinizer ignore-type */ $input, null)[$key]);
Loading history...
148
    }
149
150
    /**
151
     * Retrieves uploaded files as instances of UploadedFile or an array of UploadedFile if multiples exist.
152
     *
153
     * @param string $key
154
     * @return array|filesystem\UploadedFile|null
155
     */
156
    public static function files(string $key = null)
157
    {
158
        if (!isset($_FILES[$key])) {
159
            return null;
160
        }
161
        if (is_array($_FILES[$key]['name'])) {
162
            return self::getFileObjects($key);
163
        } else {
164
            return new filesystem\UploadedFile($_FILES);
165
        }
166
    }
167
168
    private static function getFileObjects($key)
169
    {
170
        $files = [];
171
        $numFiles = count($_FILES[$key]['name']);
172
        for ($i = 0; $i < $numFiles; $i++) {
173
            $files[] = new filesystem\UploadedFile([
174
                'name' => $_FILES[$key]['name'][$i],
175
                'type' => $_FILES[$key]['type'][$i],
176
                'tmp_name' => $_FILES[$key]['tmp_name'][$i],
177
                'error' => $_FILES[$key]['error'][$i],
178
                'size' => $_FILES[$key]['size'][$i],
179
            ]);
180
        }
181
        return $files;
182
    }
183
}
184