Skip to content

Commit

Permalink
code improved
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Feb 1, 2021
1 parent a715f6f commit f44e825
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
69 changes: 45 additions & 24 deletions src/Vin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,48 +33,64 @@ class Vin implements VinInterface
{

/**
* Regular expression for a VIN parsing (ISO 3779)
* Regular expression for a VIN parsing/validation (ISO 3779)
*
* @var string
*/
public const REGEX = '/^(?<wmi>[0-9A-HJ-NPR-Z]{3})(?<vds>[0-9A-HJ-NPR-Z]{6})(?<vis>[0-9A-HJ-NPR-Z]{8})$/';

/**
* The VIN code
*
* @var string
*/
private $vin;

/**
* World manufacturer identifier
*
* @var string
*/
private $wmi;

/**
* Vehicle descriptor section
*
* @var string
*/
private $vds;

/**
* Vehicle identifier section
*
* @var string
*/
private $vis;

/**
* Vehicle region
*
* @var null|string
*/
private $region;

/**
* Vehicle country
*
* @var null|string
*/
private $country;

/**
* Vehicle manufacturer
*
* @var null|string
*/
private $manufacturer;

/**
* Vehicle model year
*
* @var int[]
*/
private $modelYear;
Expand All @@ -91,7 +107,7 @@ public function __construct(string $value)
// The given VIN must be in upper case...
$value = strtoupper($value);

if (! preg_match(self::REGEX, $value, $match)) {
if (!preg_match(self::REGEX, $value, $match)) {
throw new InvalidArgumentException(
sprintf('The value "%s" is not a valid VIN', $value)
);
Expand All @@ -104,10 +120,10 @@ public function __construct(string $value)
$this->vis = $match['vis'];

// Parsed values
$this->region = $this->identifyRegion();
$this->country = $this->identifyCountry();
$this->manufacturer = $this->identifyManufacturer();
$this->modelYear = $this->identifyModelYear();
$this->region = $this->determineRegion();
$this->country = $this->determineCountry();
$this->manufacturer = $this->determineManufacturer();
$this->modelYear = $this->determineModelYear();
}

/**
Expand Down Expand Up @@ -175,7 +191,9 @@ public function getModelYear() : array
}

/**
* {@inheritDoc}
* Converts the object to array
*
* @return array
*/
public function toArray() : array
{
Expand All @@ -192,63 +210,66 @@ public function toArray() : array
}

/**
* Tries to determine vehicle region
*
* @return null|string
*/
private function identifyRegion() : ?string
private function determineRegion() : ?string
{
// undefined region...
if (! isset(REGIONS[$this->wmi[0]])) {
return null;
}

return REGIONS[$this->wmi[0]]['region'] ?? null;
}

/**
* Tries to determine vehicle country
*
* @return null|string
*/
private function identifyCountry() : ?string
private function determineCountry() : ?string
{
// undefined region...
if (! isset(REGIONS[$this->wmi[0]])) {
$countries = REGIONS[$this->wmi[0]]['countries'] ?? null;
if (null === $countries) {
return null;
}

foreach (REGIONS[$this->wmi[0]]['countries'] as $chars => $title) {
if (! (false === strpbrk($this->wmi[1], (string) $chars))) {
return $title;
foreach ($countries as $chars => $name) {
if (!(false === strpbrk($this->wmi[1], (string) $chars))) {
return $name;
}
}

return null;
}

/**
* Tries to determine vehicle manufacturer
*
* @return null|string
*/
private function identifyManufacturer() : ?string
private function determineManufacturer() : ?string
{
return MANUFACTURERS[$this->wmi] ?? MANUFACTURERS[$this->wmi[0] . $this->wmi[1]] ?? null;
}

/**
* Tries to determine vehicle model year(s)
*
* @return int[]
*/
private function identifyModelYear() : array
private function determineModelYear() : array
{
$comingYear = (int) date('Y') + 1;
$certainYears = [];
$estimatedYears = [];

foreach (YEARS as $year => $char) {
if ($this->vis[0] === $char) {
$certainYears[] = $year;
$estimatedYears[] = $year;
}

if ($comingYear === $year) {
break;
}
}

return $certainYears;
return $estimatedYears;
}
}
7 changes: 0 additions & 7 deletions src/VinInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,4 @@ public function getManufacturer() : ?string;
* @since 1.0.13
*/
public function getModelYear() : array;

/**
* Converts the VIN to array
*
* @return array
*/
public function toArray() : array;
}

0 comments on commit f44e825

Please sign in to comment.