Completed
Push — version-4-wip ( 86a2b9...5c3ca2 )
by Craig
03:49
created

Confirm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace League\CLImate\TerminalObject\Dynamic;
4
5
use League\CLImate\Util\Reader\ReaderInterface;
6
use function in_array;
7
use function strtolower;
8
use function substr;
9
10
class Confirm extends Input
11
{
12
13
14
    /**
15
     * @inheritdoc
16
     */
17 16
    public function __construct($prompt, ReaderInterface $reader = null)
18
    {
19 16
        parent::__construct($prompt, $reader);
20
21 16
        $this->default = "n";
22 16
    }
23
24
25
    /**
26
     * Let us know if the user confirmed.
27
     *
28
     * @return bool
29
     */
30 16
    public function confirmed()
31
    {
32 16
        if (in_array($this->default, ["y", "yes"], true)) {
33 4
            $this->prompt .= " [Y/n]";
34
        } else {
35 12
            $this->prompt .= " [y/N]";
36
        }
37
38 16
        $this->accept(['y', 'yes', 'n', 'no'], false);
39
40 16
        $response = strtolower($this->prompt());
41
42 16
        return (substr($response, 0, 1) === 'y');
43
    }
44
}
45