Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from ronanguilloux/vin
+1 Vin validation
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace IsoCodes; | ||
|
||
/** | ||
* Class VisNA, validating VIN numbers in North America. | ||
* | ||
* A vehicle identification number (VIN) (also called a chassis number or frame number) is a unique code, | ||
* including a serial number, used by the automotive industry to identify | ||
* individual motor vehicles, towed vehicles, motorcycles, scooters and mopeds, | ||
* as defined in ISO 3779 (content and structure) and ISO 4030 (location and attachment). | ||
* | ||
* @source https://en.wikipedia.org/wiki/Vehicle_identification_number#North_American_check_digits | ||
*/ | ||
class VinNA implements IsoCodeInterface | ||
{ | ||
/** | ||
* VIN validation. | ||
* | ||
* @param mixed $vin | ||
* | ||
* @return bool | ||
*/ | ||
public static function validate($vin) | ||
{ | ||
$vin = strtolower($vin); | ||
if (!preg_match('/^[^\Wioq]{17}$/', $vin)) { | ||
return false; | ||
} | ||
|
||
$weights = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2]; | ||
|
||
$letterVal = [ | ||
'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, | ||
'e' => 5, 'f' => 6, 'g' => 7, 'h' => 8, | ||
'j' => 1, 'k' => 2, 'l' => 3, 'm' => 4, | ||
'n' => 5, 'p' => 7, 'r' => 9, 's' => 2, | ||
't' => 3, 'u' => 4, 'v' => 5, 'w' => 6, | ||
'x' => 7, 'y' => 8, 'z' => 9, | ||
]; | ||
|
||
$sum = 0; | ||
|
||
for ($i = 0; $i < strlen($vin); ++$i) { | ||
if (!is_numeric($vin[$i])) { | ||
$sum += $letterVal[$vin[$i]] * $weights[$i]; | ||
} else { | ||
$sum += $vin[$i] * $weights[$i]; | ||
} | ||
} | ||
|
||
$check = $sum % 11; | ||
if (10 == $check) { | ||
$check = 'x'; | ||
} | ||
|
||
return $check == $vin[8]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace IsoCodes\Tests; | ||
|
||
/** | ||
* Class CifTest. | ||
* | ||
* @covers \IsoCodes\VinNA | ||
*/ | ||
class VinNATest extends AbstractIsoCodeInterfaceTest | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getValidValues() | ||
{ | ||
return [ | ||
['1M8GDM9AXKP042788'], // https://en.wikipedia.org/wiki/Vehicle_identification_number#Worked_example | ||
['WAUZZZF49HA036784'], // https://www.rac.co.uk/drive/advice/buying-and-selling-guides/vin-number/ | ||
['1HGBH41JXMN109186'], // https://www.yourmechanic.com/article/how-to-decode-a-vin-vehicle-identification-number-by-jason-unrau | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getInvalidValues() | ||
{ | ||
return [ | ||
['1M8GDM9AYKP042788'], // wrong check | ||
]; | ||
} | ||
} |