Passed
Push — master ( 8bec44...8e2a07 )
by Adam
02:03
created

Input::getBytes()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 3
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
1
<?php
2
/**
3
 * Includes/Input.php.
4
 *
5
 * @author  Adam "Saibamen" Stachowicz <[email protected]>
6
 * @license MIT
7
 *
8
 * @link    https://github.com/Saibamen/Generate-Sort-Numbers
9
 */
10
11
namespace Includes;
12
13
/**
14
 * Functions for receiving input from User.
15
 */
16
class Input
17
{
18
    /**
19
     * Get number from User.
20
     *
21
     * @param string    $message Message for User what he must type
22
     * @param int|float $default Default number for empty input. Default is 0
23
     *
24
     * @return float Inserted number
25
     */
26
    public static function getNumberInput($message, $default = 0)
27
    {
28
        echo $message.' [Default: '.$default.']: ';
29
30
        do {
31
            $input = trim(fgets(STDIN));
0 ignored issues
show
Bug introduced by
Includes\STDIN of type string is incompatible with the type resource expected by parameter $handle of fgets(). ( Ignorable by Annotation )

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

31
            $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN));
Loading history...
32
33
            if (is_null($input) || empty($input)) {
34
                Text::debug('Using default input: '.$default);
35
                $input = $default;
36
            } elseif (!is_numeric($input)) {
37
                echo 'Please input number: ';
38
            }
39
        } while (!is_numeric($input));
40
41
        return (float) $input;
42
    }
43
44
    /**
45
     * Get file size from User.
46
     *
47
     * @param string $message Message for User what he must type
48
     * @param string $default Default file size for empty input. Default is '5MB'
49
     *
50
     * @return int Inserted file size in bytes
51
     */
52
    public static function getFileSizeInput($message, $default = '5MB')
53
    {
54
        echo $message.' [Default: '.$default.']: ';
55
56
        do {
57
            $input = trim(fgets(STDIN));
0 ignored issues
show
Bug introduced by
Includes\STDIN of type string is incompatible with the type resource expected by parameter $handle of fgets(). ( Ignorable by Annotation )

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

57
            $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN));
Loading history...
58
59
            $isInputWrong = true;
60
            $isDefault = false;
61
62
            if (is_null($input) || empty($input)) {
63
                Text::debug('Using default input: '.$default);
64
                $input = $default;
65
                $isInputWrong = false;
66
                $isDefault = true;
67
            } else {
68
                // https://regex101.com/r/v936BS/2
69
                if (preg_match('/^(?<number>\d+(?!\.+\,+\d*))\s*(?<unit>B|KB|MB|GB|TB)$/', $input, $matches)) {
70
                    $isInputWrong = false;
71
                } else {
72
                    echo 'Please input valid file size: ';
73
                }
74
            }
75
        } while ($isInputWrong);
76
77
        if ($isDefault) {
78
            preg_match('/^(?<number>\d+(?!\.+\,+\d*))\s*(?<unit>B|KB|MB|GB|TB)$/', $input, $matches);
79
        }
80
81
        $input = self::getBytes($matches['number'], $matches['unit']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $matches does not seem to be defined for all execution paths leading up to this point.
Loading history...
82
83
        return $input;
84
    }
85
86
    /**
87
     * Get valid filename from User.
88
     *
89
     * @param string $message Message for User what he must type
90
     * @param string $default Default filename for empty input. Default is 'output'
91
     *
92
     * @return string Inserted filename
93
     */
94
    public static function getFilenameInput($message, $default = 'output')
95
    {
96
        echo $message.' [Default: '.$default.']: ';
97
98
        do {
99
            $input = trim(fgets(STDIN));
0 ignored issues
show
Bug introduced by
Includes\STDIN of type string is incompatible with the type resource expected by parameter $handle of fgets(). ( Ignorable by Annotation )

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

99
            $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN));
Loading history...
100
101
            /*
102
             * Invalid characters in files:
103
             * (Windows)  \/:*?"<>|
104
             * (Linux)    /
105
             */
106
107
            $lastCharacter = substr($input, -1);
108
109
            // Only last character because / and \ are for folders
110
            $isInputWrong = $lastCharacter === '/' || $lastCharacter === '\\';
111
112
            // Running under Windows?
113
            if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
114
                // https://regex101.com/r/wR5d0J/2/
115
                $isInputWrong = $isInputWrong || preg_match('/[:*?"<>|]/', $input);
116
            }
117
118
            if (is_null($input) || empty($input)) {
119
                Text::debug('Using default input: '.$default);
120
                $input = $default;
121
            } elseif ($isInputWrong) {
122
                echo 'Please input valid filename: ';
123
            }
124
        } while ($isInputWrong);
125
126
        return $input;
127
    }
128
129
    /**
130
     * Terminate script if User denied on confirmation.
131
     *
132
     * @see Input::getUserConfirm()
133
     */
134
    public static function dieOnDenyUserConfirm()
135
    {
136
        if (!self::getUserConfirm()) {
137
            die('Script terminated by user.');
138
        }
139
    }
140
141
    /**
142
     * Get User confirmation. Default is YES.
143
     *
144
     * @return bool Confirmation result
145
     */
146
    public static function getUserConfirm()
147
    {
148
        while (1) {
149
            echo 'Do you really want to continue? [Y/n]: ';
150
151
            $input = trim(fgets(STDIN));
0 ignored issues
show
Bug introduced by
Includes\STDIN of type string is incompatible with the type resource expected by parameter $handle of fgets(). ( Ignorable by Annotation )

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

151
            $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN));
Loading history...
152
153
            // Default is YES
154
            if (is_null($input) || empty($input) || strtolower($input) == 'y' || strtolower($input) == 'yes') {
155
                return true;
156
            } elseif (strtolower($input) == 'n' || strtolower($input) == 'no') {
157
                return false;
158
            }
159
        }
160
161
        // Fix missing return statement warning. Return true...
162
        return true;
163
    }
164
165
    /**
166
     * Get bytes number from unit conversation
167
     *
168
     * @param int    $number
169
     * @param string $unit
170
     * @param int    $notation Notation type for kilo
171
     *
172
     * @return float|int
173
     */
174
    protected static function getBytes($number, $unit, $notation = 1024)
175
    {
176
        switch ($unit) {
177
            case 'KB':
178
                $bytes = $number * $notation;
179
                break;
180
            case 'MB':
181
                $bytes = $number * pow($notation, 2);
182
                break;
183
            case 'GB':
184
                $bytes = $number * pow($notation, 3);
185
                break;
186
            case 'TB':
187
                $bytes = $number * pow($notation, 4);
188
                break;
189
            // As Bytes
190
            default:
191
                $bytes = $number;
192
                break;
193
        }
194
195
        return $bytes;
196
    }
197
}
198