Skip to content

Commit cb5d9e7

Browse files
committed
Added confirm method for getting user input
1 parent 27c44f6 commit cb5d9e7

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/CLI/Console.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,23 @@ static public function info(string $message) {
6666
static public function warning(string $message) {
6767
return fwrite(STDERR, "\033[1;33m" . $message . "\033[0m\n");
6868
}
69+
70+
/**
71+
* Warning
72+
*
73+
* Log warning messages to console
74+
*
75+
* @param string $message
76+
* @return bool|int
77+
*/
78+
static public function confirm(string $question) {
79+
self::log($question);
80+
81+
$handle = fopen('php://stdin', 'r');
82+
$line = trim(fgets($handle));
83+
84+
fclose($handle);
85+
86+
return $line;
87+
}
6988
}

tests/CLI/ConsoleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ public function testLogs()
3434
$this->assertEquals(15, Console::error('error'));
3535
$this->assertEquals(14, Console::info('info'));
3636
$this->assertEquals(19, Console::warning('warning'));
37+
$this->assertEquals('this is an answer', Console::confirm('this is a question'));
3738
}
3839
}

tests/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is an answer

0 commit comments

Comments
 (0)