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)); |
|
|
|
|
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 valid filename from User. |
46
|
|
|
* |
47
|
|
|
* @param string $message Message for User what he must type |
48
|
|
|
* @param string $default Default filename for empty input. Default is 'output' |
49
|
|
|
* |
50
|
|
|
* @return string Inserted filename |
51
|
|
|
*/ |
52
|
|
|
public static function getFilenameInput($message, $default = 'output') |
53
|
|
|
{ |
54
|
|
|
echo $message.' [Default: '.$default.']: '; |
55
|
|
|
|
56
|
|
|
do { |
57
|
|
|
$input = trim(fgets(STDIN)); |
|
|
|
|
58
|
|
|
|
59
|
|
|
/* |
60
|
|
|
* Invalid characters in files: |
61
|
|
|
* (Windows) \/:*?"<>| |
62
|
|
|
* (Linux) / |
63
|
|
|
*/ |
64
|
|
|
|
65
|
|
|
$lastCharacter = substr($input, -1); |
66
|
|
|
|
67
|
|
|
// Only last character because / and \ are for folders |
68
|
|
|
$isInputWrong = $lastCharacter === '/' || $lastCharacter === '\\'; |
69
|
|
|
|
70
|
|
|
// Running under Windows? |
71
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
72
|
|
|
// https://regex101.com/r/wR5d0J/2/ |
73
|
|
|
$isInputWrong = $isInputWrong || preg_match('/[:*?"<>|]/', $input); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (is_null($input) || empty($input)) { |
77
|
|
|
Text::debug('Using default input: '.$default); |
78
|
|
|
$input = $default; |
79
|
|
|
} elseif ($isInputWrong) { |
80
|
|
|
echo 'Please input valid filename: '; |
81
|
|
|
} |
82
|
|
|
} while ($isInputWrong); |
83
|
|
|
|
84
|
|
|
return $input; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Terminate script if User denied on confirmation. |
89
|
|
|
* |
90
|
|
|
* @see Input::getUserConfirm() |
91
|
|
|
*/ |
92
|
|
|
public static function dieOnDenyUserConfirm() |
93
|
|
|
{ |
94
|
|
|
if (!self::getUserConfirm()) { |
95
|
|
|
die('Script terminated by user.'); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get User confirmation. Default is YES. |
101
|
|
|
* |
102
|
|
|
* @return bool Confirmation result |
103
|
|
|
*/ |
104
|
|
|
public static function getUserConfirm() |
105
|
|
|
{ |
106
|
|
|
while (1) { |
107
|
|
|
echo 'Do you really want to continue? [Y/n]: '; |
108
|
|
|
|
109
|
|
|
$input = trim(fgets(STDIN)); |
|
|
|
|
110
|
|
|
|
111
|
|
|
// Default is YES |
112
|
|
|
if (is_null($input) || empty($input) || strtolower($input) == 'y' || strtolower($input) == 'yes') { |
113
|
|
|
return true; |
114
|
|
|
} elseif (strtolower($input) == 'n' || strtolower($input) == 'no') { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Fix missing return statement warning. Return true... |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|