Completed
Push — master ( 092c93...6df846 )
by Adam
04:10
created

Input::dieOnDenyUserConfirm()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
            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));
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
            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));
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

97
            $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN));
Loading history...
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