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