Test Failed
Push — tmp ( 15f615...89cc97 )
by Paul
10:31 queued 04:40
created

Helper::getPageNumber()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Database\Cache;
6
use GeminiLabs\SiteReviews\Helpers\Str;
7
use GeminiLabs\SiteReviews\Helpers\Url;
8
use GeminiLabs\Vectorface\Whip\Whip;
9
10
class Helper
11
{
12
    /**
13
     * @param string $name
14
     * @param string $path
15
     * @return string
16
     */
17 1
    public static function buildClassName($name, $path = '')
18
    {
19 1
        $className = Str::camelCase($name);
20 1
        $path = ltrim(str_replace(__NAMESPACE__, '', $path), '\\');
21 1
        return !empty($path)
22 1
            ? __NAMESPACE__.'\\'.$path.'\\'.$className
23 1
            : $className;
24
    }
25
26
    /**
27
     * @param string $name
28
     * @param string $prefix
29
     * @return string
30
     */
31 8
    public static function buildMethodName($name, $prefix = '')
32
    {
33 8
        return lcfirst(Str::camelCase($prefix.'-'.$name));
34
    }
35
36
    /**
37
     * @param string $name
38
     * @return string
39
     */
40 1
    public static function buildPropertyName($name)
41
    {
42 1
        return static::buildMethodName($name);
43
    }
44
45
    /**
46
     * @param string $cast
47
     * @param mixed $value
48
     * @return mixed
49
     */
50 6
    public static function castTo($cast = '', $value)
51
    {
52 6
        $method = static::buildMethodName($cast, 'castTo');
53 6
        return !empty($cast) && method_exists(__CLASS__, $method)
54
            ? static::$method($value)
55 6
            : $value;
56
    }
57
58
    /**
59
     * @param mixed $value
60
     * @return array
61
     */
62 6
    public static function castToArray($value)
63
    {
64 6
        return (array) $value;
65
    }
66
67
    /**
68
     * @param mixed $value
69
     * @return bool
70
     */
71
    public static function castToBool($value)
72
    {
73
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
74
    }
75
76
    /**
77
     * @param mixed $value
78
     * @return float
79
     */
80
    public static function castToFloat($value)
81
    {
82
        return (float) filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);
83
    }
84
85
    /**
86
     * @param mixed $value
87
     * @return int
88
     */
89
    public static function castToInt($value)
90
    {
91
        return (int) filter_var($value, FILTER_VALIDATE_INT);
92
    }
93
94
    /**
95
     * @param mixed $value
96
     * @return object
97
     */
98
    public static function castToObject($value)
99
    {
100
        return (object) (array) $value;
101
    }
102
103
    /**
104
     * @param mixed $value
105
     * @return string
106
     */
107 6
    public static function castToString($value)
108
    {
109 6
        if (is_object($value) && in_array('__toString', get_class_methods($value))) {
110
            return (string) $value->__toString();
111
        }
112 6
        if (is_array($value) || is_object($value)) {
113
            return serialize($value);
114
        }
115 6
        return (string) $value;
116
    }
117
118
    /**
119
     * @param string $key
120
     * @return mixed
121
     */
122 1
    public static function filterInput($key, array $request = [])
123
    {
124 1
        if (isset($request[$key])) {
125
            return $request[$key];
126
        }
127 1
        $variable = filter_input(INPUT_POST, $key);
128 1
        if (is_null($variable) && isset($_POST[$key])) {
129 1
            $variable = $_POST[$key];
130
        }
131 1
        return $variable;
132
    }
133
134
    /**
135
     * @param string $key
136
     * @return array
137
     */
138 1
    public static function filterInputArray($key)
139
    {
140 1
        $variable = filter_input(INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
141 1
        if (empty($variable) && !empty($_POST[$key]) && is_array($_POST[$key])) {
142 1
            $variable = $_POST[$key];
143
        }
144 1
        return (array) $variable;
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public static function getIpAddress()
151
    {
152
        $whitelist = [];
153
        $isUsingCloudflare = !empty(filter_input(INPUT_SERVER, 'CF-Connecting-IP'));
154
        if (glsr()->filterBool('whip/whitelist/cloudflare', $isUsingCloudflare)) {
155
            $cloudflareIps = glsr(Cache::class)->getCloudflareIps();
156
            $whitelist[Whip::CLOUDFLARE_HEADERS] = [Whip::IPV4 => $cloudflareIps['v4']];
157
            if (defined('AF_INET6')) {
158
                $whitelist[Whip::CLOUDFLARE_HEADERS][Whip::IPV6] = $cloudflareIps['v6'];
159
            }
160
        }
161
        $whitelist = glsr()->filterArray('whip/whitelist', $whitelist);
162
        $methods = glsr()->filterInt('whip/methods', Whip::ALL_METHODS);
163
        $whip = new Whip($methods, $whitelist);
164
        glsr()->action('whip', $whip);
165
        if (false !== ($clientAddress = $whip->getValidIpAddress())) {
166
            return (string) $clientAddress;
167
        }
168
        glsr_log()->error('Unable to detect IP address, please see the FAQ page for a possible solution.');
169
        return 'unknown';
170
    }
171
172
    /**
173
     * @param string $fromUrl
174
     * @param int $fallback
175
     * @return int
176
     */
177
    public function getPageNumber($fromUrl = null, $fallback = 1)
178
    {
179
        $pagedQueryVar = glsr()->constant('PAGED_QUERY_VAR');
180
        $pageNum = empty($fromUrl)
181
            ? filter_input(INPUT_GET, $pagedQueryVar, FILTER_VALIDATE_INT)
182
            : filter_var(Url::query($fromUrl, $pagedQueryVar), FILTER_VALIDATE_INT);
183
        if (empty($pageNum)) {
184
            $pageNum = (int) $fallback;
185
        }
186
        return max(1, $pageNum);
187
    }
188
189
    /**
190
     * @param mixed $value
191
     * @param mixed $fallback
192
     * @return bool
193
     */
194
    public static function ifEmpty($value, $fallback)
195
    {
196
        return static::isEmpty($value) ? $fallback : $value;
197
    }
198
199
    /**
200
     * @param mixed $value
201
     * @param string|int $min
202
     * @param string|int $max
203
     * @return bool
204
     */
205
    public static function inRange($value, $min, $max)
206
    {
207
        $inRange = filter_var($value, FILTER_VALIDATE_INT, ['options' => [
208
            'min_range' => intval($min),
209
            'max_range' => intval($max),
210
        ]]);
211
        return false !== $inRange;
212
    }
213
214
    /**
215
     * @param mixed $value
216
     * @return bool
217
     */
218 1
    public static function isEmpty($value)
219
    {
220 1
        return !is_numeric($value) && !is_bool($value) && empty($value);
221
    }
222
223
    /**
224
     * @param int|string $value
225
     * @param int|string $compareWithValue
226
     * @return bool
227
     */
228
    public static function isGreaterThan($value, $compareWithValue)
229
    {
230
        return version_compare($value, $compareWithValue, '>');
231
    }
232
233
    /**
234
     * @param int|string $value
235
     * @param int|string $compareWithValue
236
     * @return bool
237
     */
238
    public static function isGreaterThanOrEqual($value, $compareWithValue)
239
    {
240
        return version_compare($value, $compareWithValue, '>=');
241
    }
242
243
    /**
244
     * @param int|string $value
245
     * @param int|string $compareWithValue
246
     * @return bool
247
     */
248
    public static function isLessThan($value, $compareWithValue)
249
    {
250
        return version_compare($value, $compareWithValue, '<');
251
    }
252
253
    /**
254
     * @param int|string $value
255
     * @param int|string $compareWithValue
256
     * @return bool
257
     */
258
    public static function isLessThanOrEqual($value, $compareWithValue)
259
    {
260
        return version_compare($value, $compareWithValue, '<=');
261
    }
262
}
263