Passed
Push — master ( f2d884...787ada )
by Adam
01:43
created

Input::getFileSizeInput()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 9
nop 2
dl 0
loc 38
rs 6.7272
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 '1MB'
49
     *
50
     * @return int Inserted file size in bytes
51
     */
52
    public static function getFileSizeInput($message, $default = '1MB')
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
            $isDefault = false;
60
61
            if (is_null($input) || empty($input)) {
62
                Text::debug('Using default input: '.$default);
63
                $isDefault = true;
64
                break;
65
            } else {
66
                $checkInput = self::getBytes($input);
67
68
                if (!$checkInput['error']) {
69
                    $input = $checkInput['bytes'];
70
                    break;
71
                }
72
73
                echo 'Please input valid file size: ';
74
            }
75
        } while (1);
76
77
        if ($isDefault) {
78
            // Check $default just for sure
79
            $checkDefault = self::getBytes($default);
80
81
            // 1 MB in bytes
82
            $input = 1048576;
83
84
            if (!$checkDefault['error']) {
85
                $input = $checkDefault['bytes'];
86
            }
87
        }
88
89
        return $input;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $input also could return the type string which is incompatible with the documented return type integer.
Loading history...
90
    }
91
92
    /**
93
     * Get valid filename from User.
94
     *
95
     * @param string $message Message for User what he must type
96
     * @param string $default Default filename for empty input. Default is 'output'
97
     *
98
     * @return string Inserted filename
99
     */
100
    public static function getFilenameInput($message, $default = 'output')
101
    {
102
        echo $message.' [Default: '.$default.']: ';
103
104
        do {
105
            $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

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

157
            $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN));
Loading history...
158
159
            // Default is YES
160
            if (is_null($input) || empty($input) || strtolower($input) == 'y' || strtolower($input) == 'yes') {
161
                return true;
162
            } elseif (strtolower($input) == 'n' || strtolower($input) == 'no') {
163
                return false;
164
            }
165
        }
166
167
        // Fix missing return statement warning. Return true...
168
        return true;
169
    }
170
171
    /**
172
     * Get bytes number from unit conversation.
173
     *
174
     * @param string $input    User input e.g. '5B', '5MB'
175
     * @param int    $notation Notation type for kilo
176
     *
177
     * @return array
178
     */
179
    protected static function getBytes($input, $notation = 1024)
180
    {
181
        $error = true;
182
        $bytes = 0;
183
184
        // https://regex101.com/r/v936BS/2
185
        if (preg_match('/^(?<number>\d+(?!\.+\,+\d*))\s*(?<unit>B|KB|MB|GB|TB)$/', $input, $matches)) {
186
            $error = false;
187
        }
188
189
        if ($error) {
190
            return array('error' => $error, 'bytes' => $bytes);
191
        }
192
193
        switch ($matches['unit']) {
194
            case 'KB':
195
                $bytes = $matches['number'] * $notation;
196
                break;
197
            case 'MB':
198
                $bytes = $matches['number'] * pow($notation, 2);
199
                break;
200
            case 'GB':
201
                $bytes = $matches['number'] * pow($notation, 3);
202
                break;
203
            case 'TB':
204
                $bytes = $matches['number'] * pow($notation, 4);
205
                break;
206
            // As Bytes
207
            default:
208
                $bytes = $matches['number'];
209
                break;
210
        }
211
212
        return array('error' => $error, 'bytes' => $bytes);
213
    }
214
}
215