Skip to content

Commit

Permalink
Added a check for capital letters
Browse files Browse the repository at this point in the history
methods for setting check flags now do not always require an input boolean; the input will default to true when called
  • Loading branch information
acurrieclark committed Aug 13, 2016
1 parent b0f44d0 commit bb76bb1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 18 additions & 4 deletions src/Verifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions tests/VerifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit bb76bb1

Please sign in to comment.