Completed
Push — dev ( a33aa1...a2731a )
by James Ekow Abaka
01:33
created

Input::decode()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 11
cp 0
rs 9.7666
c 0
b 0
f 0
cc 4
nc 6
nop 2
crap 20
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
    /**
50
     * Constant for INPUT request.
51
     */
52
    const REQUEST = INPUT_REQUEST;
53
54
    const REQUEST_DECODER_INTERNAL = "Input::decodeInternal";
55
56
    const REQUEST_DECODER_PHP = "Input::decodePhp";
57
58
    private static $decoder = self::REQUEST_DECODER_PHP;
59
60
    /**
61
     * Cache or arrays which hold decoded query strings.
62
     *
63
     * @var array
64
     */
65
    private static $arrays = [];
66
    
67
//    /**
68
//     * Decodes and returns the value of a given item in an HTTP query.
69
//     *
70
//     * Although PHP decodes query strings automatically, it converts periods to underscores. This method makes it
71
//     * possible to decode query strings that contain periods.
72
//     * Based on code from http://stackoverflow.com/a/14432765
73
//     *
74
//     * @param string $method The HTTP method of the query (GET, POST ...)
75
//     * @param string $key The key of the item in the query to retrieve.
76
//     * @return mixed
77
//     */
78
//    private static function decode(string $method, string $key = null)
79
//    {
80
//        if(!isset(self::$arrays[$method])) {
81
//            $query = $method == self::GET
82
//                ? filter_input(INPUT_SERVER, 'QUERY_STRING')
83
//                : file_get_contents('php://input');
84
//            $query = preg_replace_callback('/(^|(?<=&))[^=[&]+/',
85
//                function($match) {
86
//                    return bin2hex($match[0]);
87
//                }, $query);
88
//            parse_str($query, $data);
89
//            self::$arrays[$method] = array_combine(array_map('hex2bin', array_keys($data)), $data);
90
//        }
91
//        return $key ? (self::$arrays[$method][$key] ?? null) : (self::$arrays[$method] ?? null);
92
//    }
93
    
94
    /**
95
     * Does the actual work of calling either the filter_input of 
96
     * filter_input_array. Calls the filter_input when a data key is provided
97
     * and callse the filte_input_array when a data key is absent.
98
     * 
99
     * @param string $input Input type
100
     * @param string $key The data key
101
     * @return string|array The value.
102
     */
103
    private static function getVariable(string $input, string $key = null)
104
    {
105
        if ($key === null) {
106
            if (!isset(self::$arrays[$input])) {
107
                self::$arrays[$input] = filter_input_array($input);
108
            }
109
            $return = self::$arrays[$input];
110
        } else {
111
            $return = filter_input($input, $key);
112
        }
113
114
        if ($return === null && $key === null) {
115
            $return = array();
116
        }
117
118
        return $return;
119
    }
120
    
121
    /**
122
     * Retrieves GET request variables.
123
     * 
124
     * @param string $key
125
     * @return string|array
126
     */
127
    public static function get(string $key = null)
128
    {
129
        return self::getVariable(INPUT_GET, $key);
130
    }
131
132
    /**
133
     * Retrieves post request variables.
134
     * 
135
     * @param string $key
136
     * @return string|array
137
     */
138
    public static function post(string $key = null)
139
    {
140
        return self::getVariable(INPUT_POST, $key);
141
    }
142
143
    /**
144
     * Retrieves server variables.
145
     * 
146
     * @param string $key
147
     * @return string|array
148
     */
149
    public static function server(string $key = null)
150
    {
151
        return self::getVariable(INPUT_SERVER, $key);
152
    }
153
154
    /**
155
     * Retrieves cookie variables.
156
     * 
157
     * @param string $key
158
     * @return string|array
159
     */
160
    public static function cookie(string $key = null)
161
    {
162
        return self::getVariable(INPUT_COOKIE, $key);
163
    }
164
165
    /**
166
     * Checks if a particular key exists in a given request query.
167
     *
168
     * @param string $input
169
     * @param string $key
170
     * @return bool
171
     */
172
    public static function exists(string $input, string $key) : bool
173
    {
174
        return isset(self::getVariable($input, null)[$key]);
175
    }
176
177
    /**
178
     * Retrieves uploaded files as instances of UploadedFile or an array of UploadedFile if multiples exist.
179
     *
180
     * @param string $key
181
     * @return array|filesystem\UploadedFile|null
182
     */
183
    public static function files(string $key = null)
184
    {
185
        if (!isset($_FILES[$key])) {
186
            return null;
187
        }
188
        if (is_array($_FILES[$key]['name'])) {
189
            return self::getFileObjects($key);
190
        } else {
191
            return new filesystem\UploadedFile($_FILES);
192
        }
193
    }
194
195
    private static function getFileObjects($key)
196
    {
197
        $files = [];
198
        $numFiles = count($_FILES[$key]['name']);
199
        for ($i = 0; $i < $numFiles; $i++) {
200
            $files[] = new filesystem\UploadedFile([
201
                'name' => $_FILES[$key]['name'][$i],
202
                'type' => $_FILES[$key]['type'][$i],
203
                'tmp_name' => $_FILES[$key]['tmp_name'][$i],
204
                'error' => $_FILES[$key]['error'][$i],
205
                'size' => $_FILES[$key]['size'][$i],
206
            ]);
207
        }
208
        return $files;
209
    }
210
211
}
212