Completed
Push — master ( a525f2...970dab )
by James Ekow Abaka
02:07
created

Input::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
126
    {
127
        $files = [];
128
        if(!isset($_FILES[$key])) {
129
            return null;
130
        }
131
        if(is_array($_FILES[$key]['name'])) {
132
            for($i = 0; $i < count($_FILES[$key]['name']); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
133
                $files[] = new filesystem\UploadedFile([
134
                    'name' => $_FILES[$key]['name'][$i],
135
                    'type' => $_FILES[$key]['type'][$i],
136
                    'tmp_name' => $_FILES[$key]['tmp_name'][$i],
137
                    'error' => $_FILES[$key]['error'][$i],
138
                    'size' => $_FILES[$key]['size'][$i],
139
                ]);
140
            }
141
            return $files;
142
        } else {
143
            return new filesystem\UploadedFile($_FILES);
144
        }
145
    }
146
}
147