@@ 14-55 (lines=42) @@ | ||
11 | * @package Samurai\Project\Question |
|
12 | * @author Raphaël Lefebvre <[email protected]> |
|
13 | */ |
|
14 | class HomepageQuestion extends Question |
|
15 | { |
|
16 | /** |
|
17 | * @param InputInterface $input |
|
18 | * @param OutputInterface $output |
|
19 | * @return int |
|
20 | */ |
|
21 | public function execute(InputInterface $input, OutputInterface $output) |
|
22 | { |
|
23 | try { |
|
24 | $this->getProject()->setHomepage($this->ask($input, $output, $this->buildQuestion())); |
|
25 | }catch(\Exception $e){ |
|
26 | $this->getProject()->setHomepage(''); |
|
27 | } |
|
28 | return ITask::NO_ERROR_CODE; |
|
29 | } |
|
30 | ||
31 | /** |
|
32 | * @return SimpleQuestion |
|
33 | */ |
|
34 | private function buildQuestion() |
|
35 | { |
|
36 | $question = new SimpleQuestion('<question>Enter your project homepage:</question>'); |
|
37 | $question->setValidator($this->buildValidator()); |
|
38 | $question->setMaxAttempts(3); |
|
39 | return $question; |
|
40 | } |
|
41 | ||
42 | /** |
|
43 | * @return callable |
|
44 | */ |
|
45 | private function buildValidator() |
|
46 | { |
|
47 | return function ($answer) { |
|
48 | if ($answer && !filter_var($answer, FILTER_VALIDATE_URL)) { |
|
49 | throw new \RuntimeException('Error: format not valid'); |
|
50 | } |
|
51 | return $answer; |
|
52 | }; |
|
53 | } |
|
54 | ||
55 | } |
|
56 |
@@ 14-50 (lines=37) @@ | ||
11 | * @package Samurai\Project\Question |
|
12 | * @author Raphaël Lefebvre <[email protected]> |
|
13 | */ |
|
14 | class NameQuestion extends Question |
|
15 | { |
|
16 | /** |
|
17 | * @param InputInterface $input |
|
18 | * @param OutputInterface $output |
|
19 | * @return int |
|
20 | */ |
|
21 | public function execute(InputInterface $input, OutputInterface $output) |
|
22 | { |
|
23 | $this->getProject()->setName($this->ask($input, $output, $this->buildQuestion())); |
|
24 | return $this->getProject()->getName() ? ITask::NO_ERROR_CODE : ITask::BLOCKING_ERROR_CODE; |
|
25 | } |
|
26 | ||
27 | /** |
|
28 | * @return SimpleQuestion |
|
29 | */ |
|
30 | private function buildQuestion() |
|
31 | { |
|
32 | $question = new SimpleQuestion('<question>Enter your project name (<vendor>/<package>):</question>'); |
|
33 | $question->setValidator($this->buildValidator()); |
|
34 | $question->setMaxAttempts(3); |
|
35 | return $question; |
|
36 | } |
|
37 | ||
38 | /** |
|
39 | * @return callable |
|
40 | */ |
|
41 | private function buildValidator() |
|
42 | { |
|
43 | return function ($answer) { |
|
44 | if (!preg_match('{^[a-z0-9_.-]+/[a-z0-9_.-]+$}', $answer)) { |
|
45 | throw new \RuntimeException('Error: format not valid'); |
|
46 | } |
|
47 | return $answer; |
|
48 | }; |
|
49 | } |
|
50 | } |
|
51 |