Completed
Push — master ( d6da28...e00423 )
by Chris
05:01 queued 01:50
created

utils::get_session_value_int()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
/*
3
Plugin Name: Vendi Shared Utility Class
4
Description: Helper class shared across all Vendi-controlled properties.
5
Version: 3.0.4
6
Author: Vendi
7
*/
8
9
namespace Vendi\Shared;
10
11
class utils
12
{
13
    public static $CUSTOM_POST = null;
14
15
    public static $CUSTOM_GET = null;
16
17
    public static $CUSTOM_COOKIE = null;
18
19
    public static $CUSTOM_SERVER = null;
20
21
    public static $CUSTOM_SESSION = null;
22
23
    /**
24
     * Reset the internal arrays to default values.
25
     */
26 1
    public static function reset_all_custom_arrays()
27
    {
28 1
        self::$CUSTOM_POST = null;
29 1
        self::$CUSTOM_GET = null;
30 1
        self::$CUSTOM_COOKIE = null;
31 1
        self::$CUSTOM_SERVER = null;
32 1
        self::$CUSTOM_SESSION = null;
33 1
    }
34
35
    /**
36
     * Get the value from the HTTP POST return the $default_value.
37
     * @param  string        $key           The form field's name to search in the $_POST array for.
38
     * @param  mixed         $default_value Optional. If the $key cannot be found the value to return. Default null.
39
     * @return mixed                        The value of the HTTP POST for the given $key or the $default.
40
     */
41 1
    public static function get_post_value( $key, $default_value = '' )
42
    {
43 1
        return self::get_request_value( 'POST', $key, $default_value );
44
    }
45
46
    /**
47
     * Get the value from the HTTP GET return the $default_value.
48
     * @param  string        $key           The form field's name to search in the $_GET array for.
49
     * @param  mixed         $default_value Optional. If the $key cannot be found the value to return. Default null.
50
     * @return mixed                        The value of the HTTP GET for the given $key or the $default.
51
     */
52 1
    public static function get_get_value( $key, $default_value = '' )
53
    {
54 1
        return self::get_request_value( 'GET', $key, $default_value );
55
    }
56
57
    /**
58
     * Get the value from the HTTP COOKIE return the $default_value.
59
     * @param  string        $key           The form field's name to search in the $_COOKIE array for.
60
     * @param  mixed         $default_value Optional. If the $key cannot be found the value to return. Default null.
61
     * @return mixed                        The value of the HTTP COOKIE for the given $key or the $default.
62
     */
63 1
    public static function get_cookie_value( $key, $default_value = '' )
64
    {
65 1
        return self::get_request_value( 'COOKIE', $key, $default_value );
66
    }
67
68
    /**
69
     * Get the value from the HTTP SERVER return the $default_value.
70
     * @param  string        $key           The form field's name to search in the $_SERVER array for.
71
     * @param  mixed         $default_value Optional. If the $key cannot be found the value to return. Default null.
72
     * @return mixed                        The value of the HTTP SERVER for the given $key or the $default.
73
     */
74 1
    public static function get_server_value( $key, $default_value = '' )
75
    {
76 1
        return self::get_request_value( 'SERVER', $key, $default_value );
77
    }
78
79
    /**
80
     * Get the value from the SESSION return the $default_value.
81
     * @param  string        $key           The form field's name to search in the $_SESSION array for.
82
     * @param  mixed         $default_value Optional. If the $key cannot be found the value to return. Default null.
83
     * @return mixed                        The value of the SESSION for the given $key or the $default.
84
     */
85
    public static function get_session_value( $key, $default_value = '' )
86
    {
87
        return self::get_request_value( 'SESSION', $key, $default_value );
88
    }
89
90
    /**
91
     * Get the first non-null value from the supplied list of sources.
92
     *
93
     * @param  string        $key           The form field's name to search in the each source array for.
94
     * @param  array         $sources       Array of sources (GET, POST, SERVER, or COOKIE) in the order to check.
95
     * @param  mixed         $default_value Optional. If the $key cannot be found the value to return. Default null.
96
     * @return mixed                        The value of the source for the given $key or the $default.
97
     */
98 3
    public static function get_value_multiple_sources( $key, array $sources, $default_value = null )
99
    {
100 3
        foreach( $sources as $source )
101
        {
102 3
            $value = self::get_request_value( $source, $key, null );
103 3
            if( null !== $value )
104
            {
105 3
                return $value;
106
            }
107
        }
108
109 1
        return $default_value;
110
    }
111
112
    /**
113
     * Get the value from the HTTP POST as an integer or return the $default_value.
114
     * @param  string        $key           The form field's name to search in the $_POST array for.
115
     * @param  integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
116
     * @return integer|mixed                The value of the HTTP POST for the given $key or the $default.
117
     */
118 1
    public static function get_post_value_int( $key, $default_value = null )
119
    {
120 1
        return self::get_request_value_int( 'POST', $key, $default_value );
121
    }
122
123
    /**
124
     * Get the value from the HTTP GET as an integer or return the $default_value.
125
     * @param  string        $key           The form field's name to search in the $_GET array for.
126
     * @param  integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
127
     * @return integer|mixed                The value of the HTTP GET for the given $key or the $default.
128
     */
129 1
    public static function get_get_value_int( $key, $default_value = null )
130
    {
131 1
        return self::get_request_value_int( 'GET', $key, $default_value );
132
    }
133
134
    /**
135
     * Get the value from the HTTP COOKIE as an integer or return the $default_value.
136
     * @param  string        $key           The form field's name to search in the $_COOKIE array for.
137
     * @param  integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
138
     * @return integer|mixed                The value of the HTTP COOKIE for the given $key or the $default.
139
     */
140 1
    public static function get_cookie_value_int( $key, $default_value = null )
141
    {
142 1
        return self::get_request_value_int( 'COOKIE', $key, $default_value );
143
    }
144
145
    /**
146
     * Get the value from the HTTP SERVER as an integer or return the $default_value.
147
     * @param  string        $key           The form field's name to search in the $_SERVER array for.
148
     * @param  integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
149
     * @return integer|mixed                The value of the HTTP SERVER for the given $key or the $default.
150
     */
151 1
    public static function get_server_value_int( $key, $default_value = null )
152
    {
153 1
        return self::get_request_value_int( 'SERVER', $key, $default_value );
154
    }
155
156
    /**
157
     * Get the value from the SESSION as an integer or return the $default_value.
158
     * @param  string        $key           The form field's name to search in the $_SESSION array for.
159
     * @param  integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
160
     * @return integer|mixed                The value of the SESSION for the given $key or the $default.
161
     */
162
    public static function get_session_value_int( $key, $default_value = null )
163
    {
164
        return self::get_request_value_int( 'SESSION', $key, $default_value );
165
    }
166
167
    /**
168
     * Get the value from the supplied request as an integer or return the $default_value.
169
     * @param  string  $request_method      The server method to use, one of GET, POST, SERVER or COOKIE.
170
     * @param  string        $key           The form field's name to search in the $_SERVER array for.
171
     * @param  integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
172
     * @return integer|mixed                The value of the request method for the given $key or the $default.
173
     */
174 4
    public static function get_request_value_int( $request_method, $key, $default_value = null )
175
    {
176 4
        $value = self::get_request_value( $request_method, $key, null );
177 4
        if( self::is_integer_like( $value ) )
178
        {
179 4
            return (int)$value;
180
        }
181
182 4
        return $default_value;
183
    }
184
185
    /**
186
     * Get the value from the supplied request or return the $default_value.
187
     * @param  string  $request_method      The server method to use, one of GET, POST, SERVER or COOKIE.
188
     * @param  string        $key           The form field's name to search in the $_SERVER array for.
189
     * @param  integer|mixed $default_value Optional. If the $key cannot be found the value to return. Default null.
190
     * @return integer|mixed                The value of the request method for the given $key or the $default.
191
     */
192 4
    public static function get_request_value( $request_method, $key, $default_value = null )
193
    {
194 4
        $request_obj = self::get_request_object( $request_method );
195
196 4
        if( null === $request_obj || ! is_array( $request_obj ) || ! array_key_exists( $key, $request_obj ) )
197
        {
198 4
            return $default_value;
199
        }
200
201 4
        $ret = $request_obj[ $key ];
202
203 4
        if( is_string( $ret ) )
204
        {
205 4
            $ret = trim( $ret );
206
        }
207
208 4
        return $ret;
209
    }
210
211
    /**
212
     * Get the current request array or mock array if set.
213
     *
214
     * @param  string    The server method to use, one of GET, POST, SERVER or COOKIE.
215
     * @return array|null The requested array or null.
216
     */
217 5
    public static function get_request_object( $request_method )
218
    {
219
        switch( $request_method )
220
        {
221 5
            case 'GET':
222 2
                if( is_array( self::$CUSTOM_GET ) )
223
                {
224 1
                    return self::$CUSTOM_GET;
225
                }
226 2
                return ( isset( $_GET ) && is_array( $_GET ) && count( $_GET ) > 0 ? $_GET : null );
227
228 4
            case 'POST':
229 2
                if( is_array( self::$CUSTOM_POST ) )
230
                {
231 1
                    return self::$CUSTOM_POST;
232
                }
233 2
                return ( isset( $_POST ) && is_array( $_POST ) && count( $_POST ) > 0 ? $_POST : null );
234
235 3
            case 'COOKIE':
236 2
                if( is_array( self::$CUSTOM_COOKIE ) )
237
                {
238 1
                    return self::$CUSTOM_COOKIE;
239
                }
240 2
                return ( isset( $_COOKIE ) && is_array( $_COOKIE ) && count( $_COOKIE ) > 0 ? $_COOKIE : null );
241
242 2
            case 'SERVER':
243 2
                if( is_array( self::$CUSTOM_SERVER ) )
244
                {
245 1
                    return self::$CUSTOM_SERVER;
246
                }
247 2
                return ( isset( $_SERVER ) && is_array( $_SERVER ) && count( $_SERVER ) > 0 ? $_SERVER : null );
248
249 1
            case 'SESSION':
250
                if( is_array( self::$CUSTOM_SESSION ) )
251
                {
252
                    return self::$CUSTOM_SESSION;
253
                }
254
                return ( isset( $_SESSION ) && is_array( $_SESSION ) && count( $_SESSION ) > 0 ? $_SESSION : null );
255
256
            default:
257 1
                return null;
258
        }
259
    }
260
261
    /**
262
     * Test if we're in a certain type of HTTP request.
263
     *
264
     * @param  string  $method The server method to test for. Generally one of GET, POST, HEAD, PUT, DELETE.
265
     * @return boolean         Returns true if the REQUEST_METHOD server variable is set to the supplied $method, otherwise false.
266
     */
267 1
    public static function is_request_method( $method )
268
    {
269 1
        return $method === self::get_server_value( 'REQUEST_METHOD' );
270
    }
271
272
    /**
273
     * Check to see if we're in a post.
274
     *
275
     * Unit tests were failing because REQUEST_METHOD wasn't always being set. This should be used
276
     * for all POST checks.
277
     *
278
     * \Vendi\Forms\utils::is_post()
279
     *
280
     * @return boolean Returns true if the REQUEST_METHOD server variable is set to POST, otherwise false.
281
     */
282 1
    public static function is_post( )
283
    {
284 1
        return self::is_request_method( 'POST' );
285
    }
286
287
    /**
288
     * Test if the given $input can be converted to an int excluding booleans.
289
     *
290
     * \Vendi\Forms\utils::is_integer_like( value )
291
     *
292
     * @param  mixed  $input The value to test.
293
     * @return boolean       True if $input is an integer or a string that contains only digits possibly starting with a dash.
294
     */
295 1
    public static function is_integer_like( $input )
296
    {
297
        return
298 1
                is_int( $input )
299
                ||
300
                (
301 1
                    is_string( $input )
302
                    &&
303 1
                    preg_match( '/^-?([0-9])+$/', $input )
304
                );
305
    }
306
307
}
308