Prompt::printBlock()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\Prompt;
4
5
use Symfony\Component\Console\Helper\FormatterHelper;
6
use Symfony\Component\Console\Helper\QuestionHelper;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use TheAentMachine\Prompt\Helper\PromptHelper;
10
11
final class Prompt
12
{
13
    /** @var InputInterface */
14
    private $input;
15
16
    /** @var OutputInterface */
17
    private $output;
18
19
    /** @var QuestionHelper */
20
    private $questionHelper;
21
22
    /** @var FormatterHelper */
23
    private $formatterHelper;
24
25
    /** @var PromptHelper */
26
    private $promptHelper;
27
28
    /**
29
     * Prompt constructor.
30
     * @param InputInterface $input
31
     * @param OutputInterface $output
32
     * @param QuestionHelper $questionHelper
33
     * @param FormatterHelper $formatterHelper
34
     */
35
    public function __construct(InputInterface $input, OutputInterface $output, QuestionHelper $questionHelper, FormatterHelper $formatterHelper)
36
    {
37
        $this->input = $input;
38
        $this->output = $output;
39
        $this->questionHelper = $questionHelper;
40
        $this->formatterHelper = $formatterHelper;
41
        $this->promptHelper = new PromptHelper($input, $output, $questionHelper);
42
    }
43
44
    /**
45
     * @param string $text
46
     * @return void
47
     */
48
    public function printBlock(string $text): void
49
    {
50
        $this->output->write("\n");
51
        $this->output->writeln('');
52
        $this->output->writeln($this->formatterHelper->formatBlock($text, 'block', true));
53
        $this->output->writeln('');
54
    }
55
56
    /**
57
     * @param string $text
58
     * @return void
59
     */
60
    public function printAltBlock(string $text): void
61
    {
62
        $this->output->write("\n");
63
        $this->output->writeln('');
64
        $this->output->writeln($this->formatterHelper->formatBlock($text, 'altblock', false));
65
        $this->output->writeln('');
66
    }
67
68
    /**
69
     * @param string $text
70
     * @param null|string $helpText
71
     * @param null|string $default
72
     * @param bool $compulsory
73
     * @param callable|null $validator
74
     * @return null|string
75
     */
76
    public function input(string $text, ?string $helpText = null, ?string $default = null, bool $compulsory = false, ?callable $validator = null): ?string
77
    {
78
        $input = new Input($this->input, $this->output, $this->questionHelper);
79
        $input
80
            ->setText($text)
81
            ->setHelpText($helpText)
82
            ->setCompulsory($compulsory)
83
            ->setValidator($validator);
84
        $input
85
            ->setDefault($default);
86
        return $input->run();
87
    }
88
89
    /**
90
     * @param string $text
91
     * @param string[] $items
92
     * @param null|string $helpText
93
     * @param null|string $default
94
     * @param bool $compulsory
95
     * @param callable|null $validator
96
     * @return null|string
97
     */
98
    public function autocompleter(string $text, array $items, ?string $helpText = null, ?string $default = null, bool $compulsory = false, ?callable $validator = null): ?string
99
    {
100
        $input = new Input($this->input, $this->output, $this->questionHelper);
101
        $input
102
            ->setText($text)
103
            ->setHelpText($helpText)
104
            ->setCompulsory($compulsory)
105
            ->setValidator($validator);
106
        $input
107
            ->setDefault($default)
108
            ->setAutocompleterValues($items);
109
        return $input->run();
110
    }
111
112
    /**
113
     * @param string $text
114
     * @param null|string $helpText
115
     * @param null|bool $default
116
     * @param bool $compulsory
117
     * @return bool
118
     */
119
    public function confirm(string $text, ?string $helpText = null, ?bool $default = null, bool $compulsory = false): bool
120
    {
121
        $confirm = new Confirm($this->input, $this->output, $this->questionHelper);
122
        $confirm
123
            ->setText($text)
124
            ->setHelpText($helpText)
125
            ->setCompulsory($compulsory);
126
        $confirm
127
            ->setDefault($default);
128
        return $confirm->run();
129
    }
130
131
    /**
132
     * @param string $text
133
     * @param mixed[] $items
134
     * @param null|string $helpText
135
     * @param null|string $default
136
     * @param bool $compulsory
137
     * @param callable|null $validator
138
     * @return null|string
139
     */
140
    public function select(string $text, array $items, ?string $helpText = null, ?string $default = null, bool $compulsory = false, ?callable $validator = null): ?string
141
    {
142
        $select = new Select($this->input, $this->output, $this->questionHelper);
143
        $select
144
            ->setText($text)
145
            ->setHelpText($helpText)
146
            ->setCompulsory($compulsory)
147
            ->setValidator($validator);
148
        $select
149
            ->setDefault($default);
150
        $select
151
            ->setItems($items);
152
        return $select->run();
153
    }
154
155
    /**
156
     * @param string $text
157
     * @param mixed[] $items
158
     * @param null|string $helpText
159
     * @param null|string $default
160
     * @param bool $compulsory
161
     * @param callable|null $validator
162
     * @return null|string[]
163
     */
164
    public function multiselect(string $text, array $items, ?string $helpText = null, ?string $default = null, bool $compulsory = false, ?callable $validator = null): ?array
165
    {
166
        $select = new Multiselect($this->input, $this->output, $this->questionHelper);
167
        $select
168
            ->setText($text)
169
            ->setHelpText($helpText)
170
            ->setCompulsory($compulsory)
171
            ->setValidator($validator);
172
        $select
173
            ->setDefault($default);
174
        $select
175
            ->setItems($items);
176
        return $select->run();
177
    }
178
179
    /**
180
     * @return PromptHelper
181
     */
182
    public function getPromptHelper(): PromptHelper
183
    {
184
        return $this->promptHelper;
185
    }
186
}
187