1 | <?php |
||
2 | |||
3 | namespace TheAentMachine\Prompt; |
||
4 | |||
5 | use Symfony\Component\Console\Helper\QuestionHelper; |
||
6 | use Symfony\Component\Console\Input\InputInterface; |
||
7 | use Symfony\Component\Console\Output\OutputInterface; |
||
8 | use Symfony\Component\Console\Question\Question; |
||
9 | use TheAentMachine\Prompt\Helper\ValidatorHelper; |
||
10 | |||
11 | abstract class AbstractInput |
||
12 | { |
||
13 | /** @var InputInterface */ |
||
14 | protected $input; |
||
15 | |||
16 | /** @var OutputInterface */ |
||
17 | protected $output; |
||
18 | |||
19 | /** @var QuestionHelper */ |
||
20 | protected $questionHelper; |
||
21 | |||
22 | /** @var string */ |
||
23 | protected $text; |
||
24 | |||
25 | /** @var null|string */ |
||
26 | protected $helpText; |
||
27 | |||
28 | /** @var bool */ |
||
29 | private $compulsory; |
||
30 | |||
31 | /** @var callable|null */ |
||
32 | protected $validator; |
||
33 | |||
34 | /** |
||
35 | * AbstractInput constructor. |
||
36 | * @param InputInterface $input |
||
37 | * @param OutputInterface $output |
||
38 | * @param QuestionHelper $questionHelper |
||
39 | */ |
||
40 | public function __construct(InputInterface $input, OutputInterface $output, QuestionHelper $questionHelper) |
||
41 | { |
||
42 | $this->input = $input; |
||
43 | $this->output = $output; |
||
44 | $this->questionHelper = $questionHelper; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @return Question |
||
49 | */ |
||
50 | protected function build(): Question |
||
51 | { |
||
52 | $message = $this->text; |
||
53 | if (!empty($this->helpText)) { |
||
54 | $message .= ' (? for help)'; |
||
55 | } |
||
56 | $question = new Question($message); |
||
57 | $question->setValidator($this->getHelpTextValidator($this->getCompulsoryValidator($this->validator))); |
||
58 | return $question; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param callable|null |
||
63 | * @return callable|null |
||
64 | */ |
||
65 | private function getHelpTextValidator(?callable $else): ?callable |
||
66 | { |
||
67 | if (!empty($this->helpText)) { |
||
68 | return function (?string $response) use ($else) { |
||
69 | $response = $response ?? ''; |
||
70 | if (\trim($response) === '?') { |
||
71 | $this->output->writeln($this->helpText ?: ''); |
||
72 | return '?'; |
||
73 | } |
||
74 | return !empty($else) ? $else($response) : $response; |
||
75 | }; |
||
76 | } |
||
77 | return $else; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param callable|null |
||
82 | * @return callable|null |
||
83 | */ |
||
84 | private function getCompulsoryValidator(?callable $else): ?callable |
||
85 | { |
||
86 | if ($this->compulsory && empty($this->default)) { |
||
87 | return function (?string $response) { |
||
88 | $response = $response ?? ''; |
||
89 | if (\trim($response) === '') { |
||
90 | throw new \InvalidArgumentException('Hey, this field is compulsory!'); |
||
91 | } |
||
92 | return !empty($else) ? $else($response) : $response; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
93 | }; |
||
94 | } |
||
95 | return function (?string $response) use ($else) { |
||
96 | $response = $response ?? ''; |
||
97 | if (\trim($response) === '') { |
||
98 | return $response; |
||
99 | } |
||
100 | return !empty($else) ? $else($response) : $response; |
||
101 | }; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @return null|mixed |
||
106 | */ |
||
107 | abstract public function run(); |
||
108 | |||
109 | /** |
||
110 | * @param string $text |
||
111 | * @return self |
||
112 | */ |
||
113 | public function setText(string $text): self |
||
114 | { |
||
115 | $this->text = $text; |
||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param null|string $helpText |
||
121 | * @return self |
||
122 | */ |
||
123 | public function setHelpText(?string $helpText): self |
||
124 | { |
||
125 | $this->helpText = $helpText; |
||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param bool $compulsory |
||
131 | * @return self |
||
132 | */ |
||
133 | public function setCompulsory(bool $compulsory): self |
||
134 | { |
||
135 | $this->compulsory = $compulsory; |
||
136 | return $this; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param callable|null $validator |
||
141 | * @return self |
||
142 | */ |
||
143 | public function setValidator(?callable $validator): self |
||
144 | { |
||
145 | $this->validator = $validator; |
||
146 | return $this; |
||
147 | } |
||
148 | } |
||
149 |