Passed
Push — master ( ece31d...41b8a6 )
by Paul
10:20 queued 04:17
created

Helper::consolidateArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 6
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\HelperTraits\Arr;
7
use GeminiLabs\SiteReviews\HelperTraits\Str;
8
use GeminiLabs\Vectorface\Whip\Whip;
9
10
class Helper
11
{
12
    use Arr;
13
    use Str;
14
15
    /**
16
     * @param string $name
17
     * @param string $path
18
     * @return string
19
     */
20 4
    public function buildClassName($name, $path = '')
21
    {
22 4
        $className = $this->camelCase($name);
23 4
        $path = ltrim(str_replace(__NAMESPACE__, '', $path), '\\');
24 4
        return !empty($path)
25 2
            ? __NAMESPACE__.'\\'.$path.'\\'.$className
26 4
            : $className;
27
    }
28
29
    /**
30
     * @param string $name
31
     * @param string $prefix
32
     * @return string
33
     */
34 2
    public function buildMethodName($name, $prefix = '')
35
    {
36 2
        return lcfirst($prefix.$this->buildClassName($name));
37
    }
38
39
    /**
40
     * @param string $name
41
     * @return string
42
     */
43 1
    public function buildPropertyName($name)
44
    {
45 1
        return lcfirst($this->buildClassName($name));
46
    }
47
48
    /**
49
     * @param string $cast
50
     * @param mixed $value
51
     * @return mixed
52
     */
53 7
    public function castTo($cast = '', $value)
54
    {
55 7
        switch ($cast) {
56
            case 'array':
57
                return (array) $value;
58
            case 'bool':
59
            case 'boolean':
60 1
                return filter_var($value, FILTER_VALIDATE_BOOLEAN);
61
            case 'float':
62
                return (float) filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);
63
            case 'int':
64
            case 'integer':
65
                return (int) filter_var($value, FILTER_VALIDATE_INT);
66
            case 'object':
67
                return (object) (array) $value;
68
            case 'str':
69
            case 'string':
70
                if (is_object($value) && in_array('__toString', get_class_methods($value))) {
71
                    return (string) $value->__toString();
72
                }
73
                if (is_array($value) || is_object($value)) {
74
                    return serialize($value);
75
                }
76
                return (string) $value;
77
            default:
78 7
                return $value;
79
        }
80
    }
81
82
    /**
83
     * @param string $key
84
     * @return mixed
85
     */
86 2
    public function filterInput($key, array $request = [])
87
    {
88 2
        if (isset($request[$key])) {
89 1
            return $request[$key];
90
        }
91 2
        $variable = filter_input(INPUT_POST, $key);
92 2
        if (is_null($variable) && isset($_POST[$key])) {
93 2
            $variable = $_POST[$key];
94
        }
95 2
        return $variable;
96
    }
97
98
    /**
99
     * @param string $key
100
     * @return array
101
     */
102 2
    public function filterInputArray($key)
103
    {
104 2
        $variable = filter_input(INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
105 2
        if (empty($variable) && !empty($_POST[$key]) && is_array($_POST[$key])) {
106 2
            $variable = $_POST[$key];
107
        }
108 2
        return (array) $variable;
109
    }
110
111
    /**
112
     * @return string
113
     */
114 1
    public function getIpAddress()
115
    {
116 1
        $cloudflareIps = glsr(Cache::class)->getCloudflareIps();
117 1
        $ipv6 = defined('AF_INET6')
118 1
            ? $cloudflareIps['v6']
119 1
            : [];
120 1
        $whitelist = apply_filters('site-reviews/whip/whitelist', [
121 1
            Whip::CLOUDFLARE_HEADERS => [
122 1
                Whip::IPV4 => $cloudflareIps['v4'],
123 1
                Whip::IPV6 => $ipv6,
124
            ],
125 1
            Whip::CUSTOM_HEADERS => [
126 1
                Whip::IPV4 => ['127.0.0.1'],
127 1
                Whip::IPV6 => ['::1'],
128
            ],
129
        ]);
130 1
        $methods = Whip::CUSTOM_HEADERS | Whip::CLOUDFLARE_HEADERS | Whip::REMOTE_ADDR;
131 1
        $methods = apply_filters('site-reviews/whip/methods', $methods);
132 1
        $whip = new Whip($methods, $whitelist);
133 1
        do_action_ref_array('site-reviews/whip', [$whip]);
134 1
        return (string) $whip->getValidIpAddress();
135
    }
136
}
137