Input   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 0
dl 0
loc 189
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A is() 0 7 2
A isAjax() 0 4 2
A isMobile() 0 4 2
A post() 0 4 1
A get() 0 4 1
A request() 0 4 1
A server() 0 4 1
A hasPost() 0 4 1
A hasGet() 0 4 1
A hasRequest() 0 4 1
A delete() 0 8 2
A userAgent() 0 3 1
A ip() 0 9 3
A filter() 0 4 1
A getFilter() 0 17 5
1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   Kumbia
11
 * @package    Input
12
 *
13
 * @copyright  Copyright (c) 2005 - 2019 KumbiaPHP Team (http://www.kumbiaphp.com)
14
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
15
 */
16
 
17
 /** 
18
 * Clase para manejar los datos del request
19
 *
20
 * @category   Kumbia
21
 * @package    Input
22
 */
23
class Input
24
{
25
    /**
26
     * Verifica o obtiene el método de la petición
27
     *
28
     * @param string $method Http method
29
     * @return mixed
30
     */
31
    public static function is($method = '')
32
    {
33
        if($method){
34
            return $method === $_SERVER['REQUEST_METHOD'];
35
        }
36
        return $_SERVER['REQUEST_METHOD'];
37
    }
38
39
    /**
40
     * Indica si el request es AJAX
41
     *
42
     * @return boolean
43
     */
44
    public static function isAjax()
45
    {
46
        return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
47
    }    
48
    
49
    /**
50
     * Detecta si el Agente de Usuario (User Agent) es un móvil
51
     *
52
     * @return boolean
53
     */
54
    public static function isMobile()
55
    {  
56
        return strpos(mb_strtolower($_SERVER['HTTP_USER_AGENT']), 'mobile') ? TRUE : FALSE;
57
    }
58
59
    /**
60
     * Obtiene un valor del arreglo $_POST
61
     *
62
     * @param string $var
63
     * @return mixed
64
     */
65
    public static function post($var = '')
66
    {
67
        return self::getFilter($_POST, $var);
68
    }
69
70
    /**
71
     * Obtiene un valor del arreglo $_GET, aplica el filtro FILTER_SANITIZE_STRING
72
     * por defecto
73
     *
74
     * @param string $var
75
     * @return mixed
76
     */
77
    public static function get($var = '')
78
    {
79
        return self::getFilter($_GET, $var);
80
    }
81
82
    /**
83
     * Obtiene un valor del arreglo $_REQUEST
84
     *
85
     * @param string $var
86
     * @return mixed
87
     */
88
    public static function request($var = '')
89
    {
90
        return self::getFilter($_REQUEST, $var);
91
    }
92
93
94
    /**
95
     * Obtiene un valor del arreglo $_SERVER
96
     *
97
     * @param string $var
98
     * @return mixed
99
     */
100
    public static function server($var = '')
101
    {
102
        return self::getFilter($_SERVER, $var);
103
    }
104
105
    /**
106
     * Verifica si existe el elemento indicado en $_POST
107
     *
108
     * @param string $var elemento a verificar
109
     * @return boolean
110
     */
111
    public static function hasPost($var)
112
    {
113
        return (bool) self::post($var);
114
    }
115
116
    /**
117
     * Verifica si existe el elemento indicado en $_GET
118
     *
119
     * @param string $var elemento a verificar
120
     * @return boolean
121
     */
122
    public static function hasGet($var)
123
    {
124
        return (bool) self::get($var);
125
    }
126
127
    /**
128
     * Verifica si existe el elemento indicado en $_REQUEST
129
     *
130
     * @param string $var elemento a verificar
131
     * @return boolean
132
     */
133
    public static function hasRequest($var)
134
    {
135
        return (bool) self::request($var);
136
    }
137
138
    /**
139
     * Elimina elemento indicado en $_POST
140
     *
141
     * @param string $var elemento a verificar
142
     * @return boolean|null
143
     */
144
    public static function delete($var = '')
145
    {
146
        if($var){
147
            $_POST[$var] = array();
148
            return;
149
        }
150
        $_POST = array();
151
    }
152
153
    /**
154
    * Permite Obtener el Agente de Usuario (User Agent)
155
    * @return String
156
    */
157
    public static function userAgent(){
158
        return $_SERVER['HTTP_USER_AGENT'];
159
    }
160
161
    /**
162
    * Permite obtene la IP del cliente, aún cuando usa proxy
163
    * @return String
164
    */
165
    public static function ip(){
166
        if (!empty($_SERVER['HTTP_CLIENT_IP'])){
167
            return $_SERVER['HTTP_CLIENT_IP'];
168
        }
169
        if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
170
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
171
        }
172
        return $_SERVER['REMOTE_ADDR'];
173
    }
174
175
176
    /**
177
     * Obtiene y filtra un valor del arreglo $_REQUEST
178
     * Por defecto, usa SANITIZE
179
     *
180
     * @param string $var
181
     * @return mixed
182
     */
183
    public static function filter($var)
0 ignored issues
show
Unused Code introduced by joanhey
The parameter $var is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
184
    {
185
        //TODO
186
    }
187
188
    /**
189
     * Devuelve el valor dentro de un array con clave en formato uno.dos.tres
190
     * @param Array array que contiene la variable
191
     * @param string $str clave a usar
192
     * @return mixed
193
     */
194
    protected static function getFilter(Array $var, $str)
195
    {
196
        if(empty($str)) {
197
            return filter_var_array($var);
198
        }   
199
        $arr = explode('.', $str);
200
        $value = $var;
201
        foreach ($arr as $key) {
202
            if(isset($value[$key])){
203
                $value = $value[$key];
204
            } else {
205
                $value = NULL;
206
                break;
207
            }
208
        }
209
        return is_array($value) ? filter_var_array($value) : filter_var($value);
210
    }
211
}
212