Completed
Push — master ( 328402...1cd18c )
by Julien
587:13 queued 584:54
created

Input::build()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 0
dl 0
loc 16
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\Prompt;
4
5
use Symfony\Component\Console\Question\Question;
6
7
class Input extends AbstractInput
8
{
9
    /** @var null|string */
10
    protected $default;
11
12
    /** @var string[] */
13
    private $autocompleterValues;
14
15
    /**
16
     * @param null|string $default
17
     * @return self
18
     */
19
    public function setDefault(?string $default): self
20
    {
21
        $this->default = $default;
22
        return $this;
23
    }
24
25
    /**
26
     * @param string[] $autocompleterValues
27
     * @return self
28
     */
29
    public function setAutocompleterValues(array $autocompleterValues): self
30
    {
31
        $this->autocompleterValues = $autocompleterValues;
32
        return $this;
33
    }
34
35
    /**
36
     * @return Question
37
     */
38
    protected function build(): Question
39
    {
40
        $question = parent::build();
41
        $message = $question->getQuestion();
42
        $validator = $question->getValidator();
43
        if (!empty($this->default)) {
44
            $message .= ' [' . $this->default . ']: ';
45
        } else {
46
            $message .= ': ';
47
        }
48
        $question = new Question($message, $this->default);
49
        $question->setValidator($validator);
50
        if (!empty($this->autocompleterValues)) {
51
            $question->setAutocompleterValues($this->autocompleterValues);
52
        }
53
        return $question;
54
    }
55
56
    /**
57
     * @return null|mixed
58
     */
59
    public function run()
60
    {
61
        $question = $this->build();
62
        do {
63
            $response = $this->questionHelper->ask($this->input, $this->output, $question);
64
        } while (!empty($this->helpText) && $response === '?');
65
        return empty($response) ? null : $response;
66
    }
67
}
68