Completed
Pull Request — master (#78)
by Aydin
02:26
created

Password::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace PhpSchool\CliMenu\Input;
4
5
/**
6
 * @author Aydin Hassan <[email protected]>
7
 */
8
class Password implements Input
9
{
10
    /**
11
     * @var InputIO
12
     */
13
    private $inputIO;
14
15
    /**
16
     * @var string
17
     */
18
    private $promptText = 'Enter password:';
19
20
    /**
21
     * @var string
22
     */
23
    private $validationFailedText = 'Invalid password, try again';
24
25
    /**
26
     * @var string
27
     */
28
    private $placeholderText = '';
29
30
    /**
31
     * @var null|callable
32
     */
33
    private $validator;
34
35
    public function __construct(InputIO $inputIO)
36
    {
37
        $this->inputIO = $inputIO;
38
    }
39
40
    public function setPromptText(string $promptText) : Input
41
    {
42
        $this->promptText = $promptText;
43
44
        return $this;
45
    }
46
47
    public function getPromptText() : string
48
    {
49
        return $this->promptText;
50
    }
51
52
    public function setValidationFailedText(string $validationFailedText) : Input
53
    {
54
        $this->validationFailedText = $validationFailedText;
55
56
        return $this;
57
    }
58
59
    public function getValidationFailedText() : string
60
    {
61
        return $this->validationFailedText;
62
    }
63
64
    public function setPlaceholderText(string $placeholderText) : Input
65
    {
66
        $this->placeholderText = $placeholderText;
67
68
        return $this;
69
    }
70
71
    public function getPlaceholderText() : string
72
    {
73
        return $this->placeholderText;
74
    }
75
76
    public function setValidator(callable $validator)
77
    {
78
        $this->validator = $validator;
79
    }
80
81
    public function ask() : InputResult
82
    {
83
        return $this->inputIO->collect($this);
84
    }
85
86
    public function validate(string $input) : bool
87
    {
88
        if ($this->validator) {
89
            $validator = $this->validator;
90
            return $validator($input);
91
        }
92
93
        return mb_strlen($input) > 16;
94
    }
95
96
    public function format(string $value) : string
97
    {
98
        return str_repeat('*', mb_strlen($value));
99
    }
100
}
101