Skip to content

Commit 324acd8

Browse files
authored
Merge pull request #10 from LSFLK/nizar-dev
NSID validation fixes
2 parents d76c960 + 94c75fe commit 324acd8

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/UniqueUid.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static function format(string $token, int $split)
108108
for ($i = 0; $i < $length; $i++) {
109109
$newToken .= '-' . $partitions[$i];
110110
}
111-
return substr($newToken, 1, strlen($newToken));
111+
return ltrim(rtrim(substr($newToken, 1, strlen($newToken)), '-'));
112112
}
113113

114114
/**
@@ -117,8 +117,15 @@ public static function format(string $token, int $split)
117117
* @param string $token
118118
* @return boolean
119119
*/
120-
public static function isValidUniqueId(string $token,$validLength = 9)
120+
public static function isValidUniqueId(string $token, $validLength = 9, $split = 3)
121121
{
122+
$actualLength = strlen($token);
123+
124+
$equalSplit = ($validLength % $split) == 0 ? true : false;
125+
126+
//calculate valid string length
127+
$validActualLength = $equalSplit ? (($validLength + ($split - 1))) : (($validLength + $split));
128+
122129
//remove - form the token
123130
$token = str_replace("-", "", $token);
124131

@@ -127,12 +134,20 @@ public static function isValidUniqueId(string $token,$validLength = 9)
127134

128135
// validate the character set
129136
$valid = preg_match("/^[" . self::$charSet . "]+$/", $token);
130-
if($validLength != $length){
137+
138+
//validate formatted length
139+
if ($actualLength != $validActualLength) {
140+
return false;
141+
//validate un formatted length
142+
} elseif ($validLength != $length) {
131143
return false;
132-
}elseif (!$valid) {
144+
//validate charset
145+
} elseif (!$valid) {
133146
return false;
134-
}elseif(is_numeric($token)){
147+
//validate 100 % numeric NSID
148+
} elseif (is_numeric($token)) {
135149
return false;
150+
//validate check digit
136151
} else {
137152
//get the check character from the token
138153
$checkChar = str_split($token)[$length - 1];

tests/ExampleTest.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ public function testOne()
1919
$id = $this->userId::getUniqueAlphanumeric();
2020
$valid =$this->userId::isValidUniqueId($id);
2121
$this->assertEquals(true,$valid);
22+
$valid =$this->userId::isValidUniqueId($id.'-');
23+
$this->assertEquals(false,$valid);
24+
25+
}
26+
27+
28+
public function testFalseRight()
29+
{
30+
$id = $this->userId::getUniqueAlphanumeric();
31+
$valid =$this->userId::isValidUniqueId($id.'-');
32+
$this->assertEquals(false,$valid);
33+
}
34+
35+
public function testFalseLeft()
36+
{
37+
$id = $this->userId::getUniqueAlphanumeric();
38+
$valid =$this->userId::isValidUniqueId('-'.$id);
39+
$this->assertEquals(false,$valid);
2240
}
2341

2442
public function testTest()
@@ -27,6 +45,8 @@ public function testTest()
2745
while ($number >= 0) {
2846
$number--;
2947
$this->testOne();
48+
$this->testFalseRight();
49+
$this->testFalseLeft();
3050
}
3151
}
3252

@@ -47,8 +67,10 @@ public function testInvalid(){
4767
$this->assertEquals(false,$valid6);
4868
$valid7 = $this->userId::isValidUniqueId('45464565');
4969
$this->assertEquals(false,$valid7);
50-
$valid7 = $this->userId::isValidUniqueId('FCV-Y7P-YD2-M',9);
51-
$this->assertEquals(false,$valid7);
70+
$valid7 = $this->userId::isValidUniqueId('P3F-VHD-FHB',9);
71+
$this->assertEquals(true,$valid7);
72+
$valid8 = $this->userId::isValidUniqueId('P3F-VHD-FHB-',9);
73+
$this->assertEquals(false,$valid8);
5274
}
5375

5476
public function testArrayInput() {

0 commit comments

Comments
 (0)