Completed
Push — master ( 04e95d...37b4c4 )
by Maxim
04:52
created

PromptHelper::choose()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 38
rs 8.439
cc 6
eloc 21
nc 16
nop 2
1
<?php
2
3
namespace Weew\Console\Helpers;
4
5
use Weew\Console\IInput;
6
use Weew\Console\IOutput;
7
8
class PromptHelper {
9
    /**
10
     * @var IInput
11
     */
12
    private $input;
13
14
    /**
15
     * @var IOutput
16
     */
17
    private $output;
18
19
    /**
20
     * PromptHelper constructor.
21
     *
22
     * @param IInput $input
23
     * @param IOutput $output
24
     */
25
    public function __construct(IInput $input, IOutput $output) {
26
        $this->input = $input;
27
        $this->output = $output;
28
    }
29
30
    /**
31
     * @param $string
32
     * @param string $default
33
     *
34
     * @return null|string
35
     */
36
    public function prompt($string, $default = null) {
37
        if ($default !== null) {
38
            $suffix = (string) $default;
39
            $question = "<question>$string</question> [<yellow>$suffix</yellow>]: ";
40
        } else {
41
            $question = "<question>$string</question>: ";
42
        }
43
44
        $this->output->write($question);
45
        $input = $this->input->readLine();
46
47
        if (strlen($input) == 0) {
48
            $input = $default;
49
        }
50
51
        if ($input === null) {
52
            return $this->prompt($string, $default);
53
        }
54
55
        return $input;
56
    }
57
58
    /**
59
     * @param $string
60
     * @param bool $default
61
     *
62
     * @return bool
63
     */
64
    public function ask($string, $default = null) {
65
        if ($default === true) {
66
            $suffix = 'Y/n';
67
        } else if ($default === false) {
68
            $suffix = 'y/N';
69
        } else {
70
            $suffix = 'y/n';
71
        }
72
73
        $this->output->write(
74
            "<question>$string</question> [<yellow>$suffix</yellow>]: "
75
        );
76
        $input = $this->input->readLine();
77
78
        if (array_contains(['yes', 'y'], $input)) {
79
            return true;
80
        }
81
82
        if (array_contains(['no', 'n'], $input)) {
83
            return false;
84
        }
85
86
        if (empty($input)) {
87
            if ($default === true) {
88
                return true;
89
            }
90
91
            if ($default === false) {
92
                return false;
93
            }
94
        }
95
96
        return $this->ask($string, $default);
97
    }
98
99
    /**
100
     * @param string $string
101
     * @param array $choices
102
     *
103
     * @return mixed
104
     */
105
    public function choose($string, array $choices) {
106
        $this->output->writeLine("<question>$string</question>");
107
108
        $choicesMap = [];
109
110
        foreach ($choices as $subject => $message) {
111
            $index = count($choicesMap) + 1;
112
113
            $choicesMap[$index] = [
114
                'subject' => $subject,
115
                'message' => $message,
116
            ];
117
        }
118
119
        foreach ($choicesMap as $index => $choice) {
120
            $message = array_get($choice, 'message');
121
122
            $this->output->writeLineIndented("[<yellow>$index</yellow>] $message");
123
        }
124
125
        $choice = null;
126
127
        while (true) {
128
            $this->output->writeIndented('Choice: ');
129
            $input = $this->input->readLine();
130
131
            if (array_has($choicesMap, $input)) {
132
                $choice = array_get(array_get($choicesMap, $input), 'subject');
133
                break;
134
            }
135
136
            if ( ! empty($input)) {
137
                $this->output->writeLineIndented('<yellow>Invalid choice</yellow>');
138
            }
139
        }
140
141
        return $choice;
142
    }
143
}
144