Completed
Pull Request — master (#81)
by Aydin
01:47
created

Password   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 1
dl 0
loc 109
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setPromptText() 0 6 1
A getPromptText() 0 4 1
A setValidationFailedText() 0 6 1
A getValidationFailedText() 0 4 1
A setPlaceholderText() 0 6 1
A getPlaceholderText() 0 4 1
A setValidator() 0 4 1
A ask() 0 4 1
A validate() 0 14 3
A filter() 0 4 1
A getStyle() 0 4 1
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
    public function __construct(InputIO $inputIO, MenuStyle $style)
43
    {
44
        $this->inputIO = $inputIO;
45
        $this->style = $style;
46
    }
47
48
    public function setPromptText(string $promptText) : Input
49
    {
50
        $this->promptText = $promptText;
51
52
        return $this;
53
    }
54
55
    public function getPromptText() : string
56
    {
57
        return $this->promptText;
58
    }
59
60
    public function setValidationFailedText(string $validationFailedText) : Input
61
    {
62
        $this->validationFailedText = $validationFailedText;
63
64
        return $this;
65
    }
66
67
    public function getValidationFailedText() : string
68
    {
69
        return $this->validationFailedText;
70
    }
71
72
    public function setPlaceholderText(string $placeholderText) : Input
73
    {
74
        $this->placeholderText = $placeholderText;
75
76
        return $this;
77
    }
78
79
    public function getPlaceholderText() : string
80
    {
81
        return $this->placeholderText;
82
    }
83
84
    public function setValidator(callable $validator) : void
85
    {
86
        $this->validator = $validator;
87
    }
88
89
    public function ask() : InputResult
90
    {
91
        return $this->inputIO->collect($this);
92
    }
93
94
    public function validate(string $input) : bool
95
    {
96
        if ($this->validator) {
97
            $validator = $this->validator;
98
            
99
            if ($validator instanceof \Closure) {
100
                $validator = $validator->bindTo($this);
101
            }
102
            
103
            return $validator($input);
104
        }
105
106
        return mb_strlen($input) >= 16;
107
    }
108
109
    public function filter(string $value) : string
110
    {
111
        return str_repeat('*', mb_strlen($value));
112
    }
113
114
    public function getStyle() : MenuStyle
115
    {
116
        return $this->style;
117
    }
118
}
119