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