Passed
Push — master ( dfb12b...5aafb2 )
by Janko
26:27
created

request::postStringFatal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Stu\Exception\InvalidParamException;
4
5
class request
6
{
7
    /** @var array<string, mixed> */
8
    private static ?array $mockVars = null;
9
10
    /**
11
     * @return array<string, mixed>
12
     */
13 14
    public static function getvars(): array
14
    {
15 14
        if (self::$mockVars !== null) {
16 12
            return self::$mockVars;
17
        }
18
19 2
        global $_GET;
20 2
        return $_GET;
21
    }
22
23
    /**
24
     * @return array<string, mixed>
25
     */
26 7
    public static function postvars(): array
27
    {
28 7
        global $_POST;
29 7
        return $_POST;
30
    }
31
32
    public static function isRequest(): bool
33
    {
34
        return array_key_exists('REQUEST_METHOD', $_SERVER);
35
    }
36
37
    public static function isPost(): bool
38
    {
39
        if (!static::isRequest()) {
40
            return false;
41
        }
42
43
        return $_SERVER['REQUEST_METHOD'] === 'POST';
44
    }
45
46 8
    public static function has(string $key): bool
47
    {
48 8
        return self::getvars()[$key] ?? self::postvars()[$key] ?? false;
49
    }
50
51
    /**
52
     * @param array<int|string, mixed> $method
53
     *
54
     * @return mixed
55
     */
56 12
    public static function getVarByMethod(array $method, string $var, bool $fatal = false)
57
    {
58 12
        if (!@array_key_exists($var, $method)) {
59 6
            if ($fatal === true) {
60 1
                throw new InvalidParamException(sprintf('request parameter "%s" does not exist', $var));
61
            }
62 5
            return false;
63
        }
64 11
        return $method[$var];
65
    }
66
67
    public static function getInt(string $var, int $std = 0): int
68
    {
69
        $int = self::getVarByMethod(self::getvars(), $var);
70
        if (strlen($int) == 0) {
0 ignored issues
show
Bug introduced by
It seems like $int can also be of type false; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        if (strlen(/** @scrutinizer ignore-type */ $int) == 0) {
Loading history...
71
            return $std;
72
        }
73
        return self::returnInt($int);
74
    }
75
76 4
    public static function getIntFatal(string $var): int
77
    {
78 4
        $int = self::getVarByMethod(self::getvars(), $var, true);
79 4
        return self::returnInt($int);
80
    }
81
82
    public static function postInt(string $var): int
83
    {
84
        $int = self::getVarByMethod(self::postvars(), $var);
85
        return self::returnInt($int);
86
    }
87
88
    public static function postIntFatal(string $var): int
89
    {
90
        $int = self::getVarByMethod(self::postvars(), $var, true);
91
        return self::returnInt($int);
92
    }
93
94
    public static function getString(string $var): string|false
95
    {
96
        return self::getVarByMethod(self::getvars(), $var);
97
    }
98
99
    public static function postString(string $var): string|false
100
    {
101
        return self::getVarByMethod(self::postvars(), $var);
102
    }
103
104
    public static function indString(string $var): string|false
105
    {
106
        $value = self::getVarByMethod(self::postvars(), $var);
107
        if ($value) {
108
            return $value;
109
        }
110
        return self::getVarByMethod(self::getvars(), $var);
111
    }
112
113 5
    public static function indInt(string $var): int
114
    {
115 5
        $value = self::getVarByMethod(self::postvars(), $var);
116 5
        if ($value) {
117
            return self::returnInt($value);
118
        }
119 5
        return self::returnInt(self::getVarByMethod(self::getvars(), $var));
120
    }
121
122
    public static function postStringFatal(string $var): string
123
    {
124
        return self::getVarByMethod(self::postvars(), $var, true);
125
    }
126
127 3
    public static function getStringFatal(string $var): string
128
    {
129 3
        return self::getVarByMethod(self::getvars(), $var, true);
130
    }
131
132
    /**
133
     * @return array<int|string, mixed>
134
     */
135
    public static function postArrayFatal(string $var): array
136
    {
137
        return self::returnArray(self::getVarByMethod(self::postvars(), $var, true));
138
    }
139
140
    /**
141
     * @return array<int|string, mixed>
142
     */
143
    public static function getArrayFatal(string $var): array
144
    {
145
        return self::returnArray(self::getVarByMethod(self::getvars(), $var, true));
146
    }
147
148
    /**
149
     * @return array<int|string, mixed>
150
     */
151
    public static function postArray(string $var): array
152
    {
153
        return self::returnArray(self::getVarByMethod(self::postvars(), $var));
154
    }
155
156 9
    public static function returnInt($result): int
157
    {
158
        if (
159 9
            !$result
160 9
            || $result < 0
161
        ) {
162
            return 0;
163
        }
164 9
        return (int) $result;
165
    }
166
167
    /**
168
     * @return array<int|string, mixed>
169
     */
170
    public static function returnArray(mixed $result): array
171
    {
172
        if (!is_array($result)) {
173
            return [];
174
        }
175
        return $result;
176
    }
177
178
    public static function setVar(string $var, mixed $value): void
179
    {
180
        global $_GET, $_POST;
181
        $_GET[$var] = $value;
182
        $_POST[$var] = $value;
183
    }
184
185
    public static function isAjaxRequest(): bool
186
    {
187
        return !empty($_SERVER['HTTP_X_REQUESTED_WITH'])
188
            && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
189
    }
190
191
    /** @param array<string, mixed> $mockVars */
192 1261
    public static function setMockVars(?array $mockVars): void
193
    {
194 1261
        self::$mockVars = $mockVars;
195
    }
196
}
197