Passed
Push — master ( d497c1...a69d41 )
by Bobby
10:25
created

Plexity::requireNumericCharacters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ballen\Plexity;
4
5
use Ballen\Collection\Collection;
6
use Ballen\Plexity\Interfaces\PasswordHistoryInterface;
7
use Ballen\Plexity\Support\Validator;
8
9
/**
10
 * Plexity
11
 *
12
 * Plexity (Password Complexity) is a password complexity library that
13
 * enables you to set "rules" for a password (or any other kind of string) that
14
 * you can then check against in your application.
15
 *
16
 * @author Bobby Allen <[email protected]>
17
 * @license http://opensource.org/licenses/MIT
18
 * @link https://github.com/allebb/plexity
19
 * @link https://bobbyallen.me
20
 *
21
 */
22
class Plexity
23
{
24
25
    const RULE_UPPER = 'upper';
26
    const RULE_LOWER = 'lower';
27
    const RULE_SPECIAL = 'special';
28
    const RULE_NUMERIC = 'numeric';
29
    const RULE_LENGTH_MIN = 'length_min';
30
    const RULE_LENGTH_MAX = 'length_max';
31
    const RULE_NOT_IN = 'not_in';
32
33
    /**
34
     * The configured list of rules for the object.
35
     * @var \Ballen\Collection\Collection
36
     */
37
    private $rules;
38
39
    /**
40
     * The string to validate against
41
     * @var string
42
     */
43
    private $checkString;
44
45
    /**
46
     * Default collection settings
47
     * @var array<string,mixed>
48
     */
49
    private $defaultConfiguration = [
50
        self::RULE_UPPER => 0,
51
        self::RULE_LOWER => 0,
52
        self::RULE_SPECIAL => 0,
53
        self::RULE_NUMERIC => 0,
54
        self::RULE_LENGTH_MIN => 0,
55
        self::RULE_LENGTH_MAX => 0,
56
        self::RULE_NOT_IN => null,
57
    ];
58
59
    /**
60
     * The validator instance.
61
     * @var Support\Validator
62
     */
63
    public $validator;
64
65
    /**
66
     * Instaniate a new instance of the Plexity class.
67
     */
68 33
    public function __construct()
69
    {
70 33
        $this->rules = new Collection($this->defaultConfiguration);
71
72 33
        $this->validator = new Validator;
73
    }
74
75
    /**
76
     * Requires the password to contain upper case characters.
77
     * @param int $amount Optional minimum number of required upper case characters (will default to 1)
78
     * @return Plexity
79
     */
80 4
    public function requireUpperCase($amount = 1)
81
    {
82 4
        $this->rules->put(self::RULE_UPPER, $amount);
83 4
        return $this;
84
    }
85
86
    /**
87
     * Requires the password to contain lower case characters.
88
     * @param int $amount Optional minimum number of required lower case characters (will default to 1)
89
     * @return Plexity
90
     */
91 4
    public function requireLowerCase($amount = 1)
92
    {
93 4
        $this->rules->put(self::RULE_LOWER, $amount);
94 4
        return $this;
95
    }
96
97
    /**
98
     * Requires the password to contain special characters.
99
     * @param int $amount Optional amount of special characters the string must atleast contain.
100
     * @return Plexity
101
     */
102 5
    public function requireSpecialCharacters($amount = 1)
103
    {
104 5
        $this->rules->put(self::RULE_SPECIAL, $amount);
105 5
        return $this;
106
    }
107
108
    /**
109
     * Requires the password to contain numeric characters.
110
     * @deprecated See bug report: https://github.com/allebb/plexity/issues/4
111
     * @note Use the requireNumericCharacters() method instead, proxied this method for now to prevent broken code but will be removed in a future release.
112
     */
113 4
    public function requireNumericChataters($amount = 1)
114
    {
115 4
        return $this->requireNumericCharacters($amount);
116
    }
117
118
    /**
119
     * Requires the password to contain numeric characters.
120
     * @param int $amount Optional amount of numeric characters the string must at least contain.
121
     * @return Plexity
122
     */
123 6
    public function requireNumericCharacters($amount = 1)
124
    {
125 6
        $this->rules->put(self::RULE_NUMERIC, $amount);
126 6
        return $this;
127
    }
128
129
    /**
130
     * Requires the password/string to be atleast X characters long.
131
     * @param int $minLength Minimum length that the password/string must be.
132
     * @return Plexity
133
     */
134 7
    public function minimumLength($minLength)
135
    {
136 7
        if (!is_int($minLength)) {
0 ignored issues
show
introduced by
The condition is_int($minLength) is always true.
Loading history...
137 1
            throw new \InvalidArgumentException('The minimum length value must be of type integer.');
138
        }
139 6
        $this->rules->put(self::RULE_LENGTH_MIN, $minLength);
140 6
        return $this;
141
    }
142
143
    /**
144
     * Requires the password/string to be a maximum of X characters long.
145
     * @param int $maxLength Maximum length that the password/string can be.
146
     * @return Plexity
147
     */
148 7
    public function maximumLength($maxLength)
149
    {
150 7
        if (!is_int($maxLength)) {
0 ignored issues
show
introduced by
The condition is_int($maxLength) is always true.
Loading history...
151 1
            throw new \InvalidArgumentException('The maximum length value must be of type integer.');
152
        }
153 6
        $this->rules->put(self::RULE_LENGTH_MAX, $maxLength);
154 6
        return $this;
155
    }
156
157
    /**
158
     * An alias for adding both the minimumLenght() and maximumLenght() methods/rules.
159
     * @param int $minimmum Length must be atleast X characters long.
160
     * @param int $maximum Length must not exceed X characters long.
161
     * @return Plexity
162
     */
163 4
    public function lengthBetween($minimmum, $maximum)
164
    {
165 4
        $this->minimumLength($minimmum);
166 4
        $this->maximumLength($maximum);
167 4
        return $this;
168
    }
169
170
    /**
171
     * Requires that the password/string is not found in a password history collection.
172
     * @param array<string>|PasswordHistoryInterface $history An array of passwords/strings to check against or an implementation of PasswordHistoryInterface.
173
     * @return Plexity
174
     */
175 4
    public function notIn($history)
176
    {
177 4
        $this->rules->put(self::RULE_NOT_IN, $history);
178 4
        return $this;
179
    }
180
181
    /**
182
     * Check the string against the list of configured rules to ensure that it is valid.
183
     * @param string $string The password/string to validate.
184
     * @return bool
185
     * @throws Exceptions\ValidationException
186
     */
187 31
    public function check($string)
188
    {
189 31
        $this->checkString = $string;
190 31
        return $this->validator->validate($this);
191
    }
192
193
    /**
194
     * Returns the configured rule set.
195
     * @return Collection
196
     */
197 31
    public function rules()
198
    {
199 31
        return $this->rules;
200
    }
201
202
    /**
203
     * Returns the password/string of which is to be checked.
204
     * @return string
205
     */
206 31
    public function checkString()
207
    {
208 31
        return $this->checkString;
209
    }
210
}
211