1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Input; |
4
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\MenuStyle; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Aydin Hassan <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class Password implements Input |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var InputIO |
14
|
|
|
*/ |
15
|
|
|
private $inputIO; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $promptText = 'Enter password:'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $validationFailedText = 'Invalid password, try again'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $placeholderText = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var null|callable |
34
|
|
|
*/ |
35
|
|
|
private $validator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var MenuStyle |
39
|
|
|
*/ |
40
|
|
|
private $style; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
private $passwordLength = 16; |
46
|
|
|
|
47
|
|
|
public function __construct(InputIO $inputIO, MenuStyle $style) |
48
|
|
|
{ |
49
|
|
|
$this->inputIO = $inputIO; |
50
|
|
|
$this->style = $style; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setPromptText(string $promptText) : Input |
54
|
|
|
{ |
55
|
|
|
$this->promptText = $promptText; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getPromptText() : string |
61
|
|
|
{ |
62
|
|
|
return $this->promptText; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function setValidationFailedText(string $validationFailedText) : Input |
66
|
|
|
{ |
67
|
|
|
$this->validationFailedText = $validationFailedText; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getValidationFailedText() : string |
73
|
|
|
{ |
74
|
|
|
return $this->validationFailedText; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setPlaceholderText(string $placeholderText) : Input |
78
|
|
|
{ |
79
|
|
|
$this->placeholderText = $placeholderText; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getPlaceholderText() : string |
85
|
|
|
{ |
86
|
|
|
return $this->placeholderText; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function setValidator(callable $validator) : Input |
90
|
|
|
{ |
91
|
|
|
$this->validator = $validator; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function ask() : InputResult |
97
|
|
|
{ |
98
|
|
|
return $this->inputIO->collect($this); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function validate(string $input) : bool |
102
|
|
|
{ |
103
|
|
|
if ($this->validator) { |
104
|
|
|
$validator = $this->validator; |
105
|
|
|
|
106
|
|
|
if ($validator instanceof \Closure) { |
107
|
|
|
$validator = $validator->bindTo($this); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $validator($input); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return mb_strlen($input) >= $this->passwordLength; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function filter(string $value) : string |
117
|
|
|
{ |
118
|
|
|
return str_repeat('*', mb_strlen($value)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getStyle() : MenuStyle |
122
|
|
|
{ |
123
|
|
|
return $this->style; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function setPasswordLength(int $length) : int |
127
|
|
|
{ |
128
|
|
|
return $this->passwordLength = $length; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|