Skip to content

Commit

Permalink
Merge pull request #56 from sunrise-php/release/v1.1.0
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
fenric authored Feb 1, 2021
2 parents ebee1fe + f44e825 commit 045b1b8
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 50 deletions.
55 changes: 55 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
version: 2
jobs:
php71:
docker:
- image: circleci/php:7.1-cli-node-browsers
steps:
- checkout
- run: php -v
- run: composer install --no-interaction --prefer-source --no-suggest
- run: php vendor/bin/phpunit --colors=always
php72:
docker:
- image: circleci/php:7.2-cli-node-browsers
steps:
- checkout
- run: php -v
- run: composer install --no-interaction --prefer-source --no-suggest
- run: php vendor/bin/phpunit --colors=always
php73:
docker:
- image: circleci/php:7.3-cli-node-browsers
steps:
- checkout
- run: php -v
- run: composer install --no-interaction --prefer-source --no-suggest
- run: php vendor/bin/phpunit --colors=always
php74:
docker:
- image: circleci/php:7.4-cli-node-browsers
steps:
- checkout
- run: php -v
- run: composer install --no-interaction --prefer-source --no-suggest
- run: php vendor/bin/phpunit --colors=always
php80:
docker:
- image: circleci/php:8.0-cli-node-browsers
steps:
- checkout
- run: php -v
- run: composer install --no-interaction --prefer-source --no-suggest
- run: php vendor/bin/phpunit --colors=always
workflows:
version: 2
build:
jobs:
- php71
- php72
- php73
- php74
- php80
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.php_cs.cache
.phpunit.result.cache
composer.lock
coverage.xml
phpbench.json
phpcs.xml
phpunit.xml
vendor/
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ matrix:
- php: 7.1
- php: 7.2
- php: 7.3
- php: 7.4
- php: nightly
fast_finish: true

before_install:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Anatoly Nekhay
Copyright (c) 2018 Sunrise // PHP

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
## Sunrise // Simple VIN decoder for PHP 7.1+ based on ISO-3779
## Simple VIN decoder for PHP 7.1+ (incl. PHP 8) based on ISO-3779

[![Gitter](https://badges.gitter.im/sunrise-php/support.png)](https://gitter.im/sunrise-php/support)
[![Build Status](https://scrutinizer-ci.com/g/sunrise-php/vin/badges/build.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/vin/build-status/master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/sunrise-php/vin/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/vin/?branch=master)
[![Build Status](https://circleci.com/gh/sunrise-php/vin.svg?style=shield)](https://circleci.com/gh/sunrise-php/vin)
[![Code Coverage](https://scrutinizer-ci.com/g/sunrise-php/vin/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/vin/?branch=master)
[![Latest Stable Version](https://img.shields.io/packagist/v/sunrise/vin.svg?label=version)](https://packagist.org/packages/sunrise/vin)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/sunrise-php/vin/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/vin/?branch=master)
[![Total Downloads](https://img.shields.io/packagist/dt/sunrise/vin.svg?label=downloads)](https://packagist.org/packages/sunrise/vin)
[![Latest Stable Version](https://img.shields.io/packagist/v/sunrise/vin.svg?label=version)](https://packagist.org/packages/sunrise/vin)
[![License](https://img.shields.io/packagist/l/sunrise/vin.svg?label=license)](https://packagist.org/packages/sunrise/vin)

## Installation
Expand All @@ -23,7 +23,7 @@ use Sunrise\Vin\Vin;
try {
$vin = new Vin('WVWZZZ1KZ6W612305');
} catch (InvalidArgumentException $e) {
// It's not a valid VIN
// It isn't a valid VIN...
}

$vin->getVin(); // returns "WVWZZZ1KZ6W612305"
Expand Down
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "sunrise/vin",
"homepage": "https://github.com/sunrise-php/vin",
"description": "Sunrise // Simple VIN decoder for PHP 7.1+ based on ISO-3779",
"description": "Simple VIN decoder for PHP 7.1+ based on ISO-3779",
"license": "MIT",
"keywords": [
"fenric",
"sunrise",
"vin",
"decoder",
"iso-3779"
"iso-3779",
"php7",
"php8"
],
"authors": [
{
Expand All @@ -28,11 +30,10 @@
}
],
"require": {
"php": "^7.1"
"php": "^7.1|^8.0"
},
"require-dev": {
"phpunit/phpunit": "7.5.13",
"phpunit/php-code-coverage": "6.1.4",
"phpunit/phpunit": "7.5.20|9.5.0",
"sunrise/coding-standard": "1.0.0"
},
"autoload": {
Expand All @@ -47,8 +48,8 @@
},
"scripts": {
"test": [
"phpcs",
"phpunit --colors=always --coverage-text"
"phpunit --colors=always --coverage-text",
"phpcs"
]
}
}
12 changes: 6 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0"?>
<phpunit colors="true">
<phpunit colors="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Sunrise VIN Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
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 045b1b8

Please sign in to comment.