Total Complexity | 7 |
Total Lines | 45 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
18 | class Prompter implements PrompterInterface |
||
19 | { |
||
20 | private $answers = []; |
||
21 | private $hasBeenAsked = false; |
||
22 | private $question; |
||
23 | private $unansweredQuestions = false; |
||
24 | |||
25 | public function setAnswer($answer) |
||
26 | { |
||
27 | $this->answers[] = $answer; |
||
28 | } |
||
29 | |||
30 | public function askConfirmation(string $question, bool $default = true): bool |
||
31 | { |
||
32 | $this->hasBeenAsked = true; |
||
33 | $this->question = $question; |
||
34 | |||
35 | $this->unansweredQuestions = \count($this->answers) > 1; |
||
36 | |||
37 | return (bool) array_shift($this->answers); |
||
38 | } |
||
39 | |||
40 | public function hasBeenAsked($question = null) |
||
41 | { |
||
42 | if (!$question) { |
||
43 | return $this->hasBeenAsked; |
||
44 | } |
||
45 | |||
46 | return $this->hasBeenAsked |
||
47 | && $this->normalise($this->question) === $this->normalise($question); |
||
48 | } |
||
49 | |||
50 | public function hasUnansweredQuestions() |
||
51 | { |
||
52 | return $this->unansweredQuestions; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param mixed $question |
||
57 | * |
||
58 | * @return mixed |
||
59 | */ |
||
60 | private function normalise($question) |
||
61 | { |
||
62 | return preg_replace('/\s+/', '', trim(strip_tags($question))); |
||
63 | } |
||
65 |