Skip to content

Commit

Permalink
Merge branch 'validate-invalid-characters'
Browse files Browse the repository at this point in the history
  • Loading branch information
ievgenii.kaurov committed May 12, 2020
2 parents e41b225 + aca5d20 commit 973ef5c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/CUSIP.php
Expand Up @@ -82,12 +82,10 @@ public static function isCUSIP( $cusip ) {

$checksumDigit = CUSIP::getChecksumDigit( $cusip );
// If the last character of the cusip is equal to the checksum digit, then it validates.
if ( substr( $cusip,
-1 ) == $checksumDigit
):
$isValid = $checksumDigit !== false && (\substr($cusip, -1) == $checksumDigit);
if ($isValid) {
return true;
endif;

}
return false;
}

Expand Down Expand Up @@ -135,6 +133,10 @@ public static function getChecksumDigit( $cusip ) {
$position = $ord - 96;
$v = $position + 9; // S&P encodes A == 10, and so on.
endif;
if ($v === null) {
// invalid character is provided
return false;
}
// Of the 8 characters we are checking, if the character being checked right now is
// in an odd position, then we are supposed to double it's value. Example: 6 becomes 12
if ( ( $i % 2 ) != 0 ):
Expand Down
10 changes: 10 additions & 0 deletions tests/CUSIPTest.php
Expand Up @@ -139,6 +139,16 @@ public function testIsCusipWithInvalidCheckDigit() {
$this->assertFalse( $isCusip );
}


public function testIsCusipWithInvalidCheckInvalidCharacters() {
$isCusip = CUSIP::isCUSIP('1234' . \chr(0) . '6787');
static::assertFalse($isCusip);

$isCusip = CUSIP::isCUSIP('1234' . \chr(127) . '6787');
static::assertFalse($isCusip);
}


/**
* @test
*/
Expand Down

0 comments on commit 973ef5c

Please sign in to comment.