Completed
Push — master ( 37b4c4...8cb761 )
by Maxim
02:41
created

PromptHelper::choose()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 42
rs 6.7272
cc 7
eloc 24
nc 24
nop 3
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
     * @param bool $useChoiceKeysAsSelector
103
     *
104
     * @return mixed
105
     */
106
    public function choose($string, array $choices, $useChoiceKeysAsSelector = false) {
107
        $this->output->writeLine("<question>$string</question>");
108
109
        $choicesMap = [];
110
111
        foreach ($choices as $subject => $message) {
112
            if ($useChoiceKeysAsSelector) {
113
                $index = $subject;
114
            } else {
115
                $index = count($choicesMap) + 1;
116
            }
117
118
            $choicesMap[$index] = [
119
                'subject' => $subject,
120
                'message' => $message,
121
            ];
122
        }
123
124
        foreach ($choicesMap as $index => $choice) {
125
            $message = array_get($choice, 'message');
126
127
            $this->output->writeLineIndented("[<yellow>$index</yellow>] $message");
128
        }
129
130
        $choice = null;
131
132
        while (true) {
133
            $this->output->writeIndented('Choice: ');
134
            $input = $this->input->readLine();
135
136
            if (array_has($choicesMap, $input)) {
137
                $choice = array_get(array_get($choicesMap, $input), 'subject');
138
                break;
139
            }
140
141
            if ( ! empty($input)) {
142
                $this->output->writeLineIndented('<yellow>Invalid choice</yellow>');
143
            }
144
        }
145
146
        return $choice;
147
    }
148
}
149