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
|
|
|
Text::debug('User input: '.$input); |
33
|
|
|
|
34
|
|
|
if (is_null($input) || empty($input)) { |
35
|
|
|
Text::debug('Using default input: '.$default); |
36
|
|
|
$input = $default; |
37
|
|
|
} elseif (!is_numeric($input)) { |
38
|
|
|
echo 'Please input number: '; |
39
|
|
|
} |
40
|
|
|
} while (!is_numeric($input)); |
41
|
|
|
|
42
|
|
|
return (float) $input; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get filename from User. |
47
|
|
|
* |
48
|
|
|
* @param string $message Message for User what he must type |
49
|
|
|
* |
50
|
|
|
* @return string Inserted filename |
51
|
|
|
*/ |
52
|
|
|
public static function getFilenameInput($message) |
53
|
|
|
{ |
54
|
|
|
echo $message.': '; |
55
|
|
|
|
56
|
|
|
do { |
57
|
|
|
$input = trim(fgets(STDIN)); |
|
|
|
|
58
|
|
|
Text::debug('User input: '.$input); |
59
|
|
|
|
60
|
|
|
/* |
61
|
|
|
* TODO: Invalid characters: |
62
|
|
|
* (Windows) \/:*?"<>| (check \ and / only at the end of string - need to test) |
63
|
|
|
* (Linux) / |
64
|
|
|
*/ |
65
|
|
|
$isInputWrong = is_null($input) || empty($input); |
66
|
|
|
|
67
|
|
|
if ($isInputWrong) { |
68
|
|
|
echo 'Please input filename: '; |
69
|
|
|
} |
70
|
|
|
} while ($isInputWrong); |
71
|
|
|
|
72
|
|
|
return $input; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Terminate script if User denied on confirmation. |
77
|
|
|
* |
78
|
|
|
* @see Input::getUserConfirm() |
79
|
|
|
*/ |
80
|
|
|
public static function dieOnDenyUserConfirm() |
81
|
|
|
{ |
82
|
|
|
if (!self::getUserConfirm()) { |
83
|
|
|
die('Script terminated by user.'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get User confirmation. Default is YES. |
89
|
|
|
* |
90
|
|
|
* @return bool Confirmation result |
91
|
|
|
*/ |
92
|
|
|
public static function getUserConfirm() |
93
|
|
|
{ |
94
|
|
|
while (1) { |
95
|
|
|
echo 'Do you really want to continue? [Y/n]: '; |
96
|
|
|
|
97
|
|
|
$input = trim(fgets(STDIN)); |
|
|
|
|
98
|
|
|
|
99
|
|
|
// Default is YES |
100
|
|
|
if (is_null($input) || empty($input) || strtolower($input) == 'y' || strtolower($input) == 'yes') { |
101
|
|
|
return true; |
102
|
|
|
} elseif (strtolower($input) == 'n' || strtolower($input) == 'no') { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Fix missing return statement warning. Return true... |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|