diff --git a/README.md b/README.md index 811b930..886df1a 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,9 @@ Set the maximum allowed length of the password ##### setCheckContainsLetters(boolean $value) Set the flag to check that the password contains at least one letter +##### setCheckContainsCapitals(boolean $value) +Set the flag to check that the password contains at least one *capital* letter + ##### setCheckContainsNumbers(boolean $value) Set the flag to check that the password contains at least one number diff --git a/src/Verifier.php b/src/Verifier.php index 0791a35..49b8aa0 100644 --- a/src/Verifier.php +++ b/src/Verifier.php @@ -42,6 +42,10 @@ public function checkPassword($password) { $this->errors['letters'] = "Password must contain at least one letter"; } + if ($this->checkCapitals && !preg_match('/[A-Z]/', $password)) { + $this->errors['capitals'] = "Password must contain at least one capital letter"; + } + if ($this->checkNumbers && !preg_match('/[0-9]/', $password)) { $this->errors['numbers'] = "Password must contain at least one number"; } @@ -88,7 +92,7 @@ public function setMaxLength($length) { return $this; } - public function setCheckContainsLetters($value) { + public function setCheckContainsLetters($value = true) { if ($value !== false && $value !== true) { throw new SetConstraintException("setCheckContainsLetters value must be of type boolean", 4); } @@ -98,7 +102,17 @@ public function setCheckContainsLetters($value) { return $this; } - public function setCheckContainsNumbers($value) { + public function setCheckContainsCapitals($value = true) { + if ($value !== false && $value !== true) { + throw new SetConstraintException("setCheckContainsCapitals value must be of type boolean", 8); + } + else { + $this->checkCapitals = $value; + } + return $this; + } + + public function setCheckContainsNumbers($value = true) { if ($value !== false && $value !== true) { throw new SetConstraintException("setCheckContainsNumbers value must be of type boolean", 5); } @@ -108,7 +122,7 @@ public function setCheckContainsNumbers($value) { return $this; } - public function setCheckContainsSpecialChrs($value) { + public function setCheckContainsSpecialChrs($value = true) { if ($value !== false && $value !== true) { throw new SetConstraintException("setCheckContainsSpecialChrs value must be of type boolean", 6); } @@ -118,7 +132,7 @@ public function setCheckContainsSpecialChrs($value) { return $this; } - public function setCheckBlacklist($value) { + public function setCheckBlacklist($value = true) { if ($value !== false && $value !== true) { throw new SetConstraintException("setCheckBlacklist value must be of type boolean", 7); } diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php index 3c073e3..725474d 100644 --- a/tests/ExceptionTest.php +++ b/tests/ExceptionTest.php @@ -51,6 +51,16 @@ public function testCheckContainsLettersInputIsBoolean() { $passwordVerifier->setCheckContainsLetters('hello'); } + /** + * @expectedException acurrieclark\PhpPasswordVerifier\Exception\SetConstraintException + * @expectedExceptionCode 8 + */ + + public function testCheckContainsCapitalsInputIsBoolean() { + $passwordVerifier = new Verifier(); + $passwordVerifier->setCheckContainsCapitals('hello'); + } + /** * @expectedException acurrieclark\PhpPasswordVerifier\Exception\SetConstraintException * @expectedExceptionCode 5 diff --git a/tests/VerifyTest.php b/tests/VerifyTest.php index c2f8893..a0e177f 100644 --- a/tests/VerifyTest.php +++ b/tests/VerifyTest.php @@ -66,6 +66,20 @@ public function testContainsLetterPass() { $this->assertEquals(empty($passwordVerifier->getErrors()), true); } + public function testContainsCapitalsFail() { + $passwordVerifier = new Verifier(); + $passwordVerifier->setCheckContainsCapitals(true); + $this->assertEquals($passwordVerifier->checkPassword('asdf'), false); + $this->assertEquals(isset($passwordVerifier->getErrors()['capitals']), true); + } + + public function testContainsCapitalsPass() { + $passwordVerifier = new Verifier(); + $passwordVerifier->setCheckContainsCapitals(true); + $this->assertEquals($passwordVerifier->checkPassword('Asdf'), true); + $this->assertEquals(empty($passwordVerifier->getErrors()), true); + } + public function testContainsNumberFail() { $passwordVerifier = new Verifier(); $passwordVerifier->setCheckContainsNumbers(true);