Skip to content

PHP package to Validate and Extract information from National Identification Numbers.

License

Notifications You must be signed in to change notification settings

reducktion/socrates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Socrates logo

Usage example

Badge Total Downloads Latest Version License StyleCI Contributors


Introduction

I am a Citizen of the World, and my Nationality is Goodwill.

Socrates is a PHP Package that allows you to validate and retrieve personal data from National Identification Numbers. Most countries in Europe are supported as well as some North and South American ones, with the goal to support as many countries in the world as possible.

Some countries also encode personal information of the citizen, such as gender or the place of birth. This package allows you to extract that information in a consistent way.

This package can be useful for many things such as validating a user's ID for finance related applications or verifying a user's age without asking for it explicitly. We recommend you review your country's data processing and protection laws before storing any information.

Ports of this package to other languages are currently in progress. Check further below for which ones are currently available.

PHP Version

PHP 8.1 is the minimum required version. If you are using an older version you should pull in the 1.3.0 version of this package. We highly recommend you upgrade though!

Installation

composer require reducktion/socrates

Usage

Socrates provides two methods: validateId and getCitizenDataFromId. Both receive an ID and the country code as a backed enum with the ISO 3166-2 format as the first and second parameters respectively. Simply instantiate the class and call the method you wish:

use Reducktion\Socrates\Socrates;
use Reducktion\Socrates\Constants\Country;

$socrates = new Socrates();
$socrates->validateId('14349483 0 ZV3', Country::Portugal);

validateId

This method will return true or false. In case the ID has a wrong character length an InvalidLengthException will be thrown.

if ($socrates->validateId('719102091', Country::Netherlands)) {
    echo 'Valid ID.';
} else {
    echo 'Invalid ID.';
}

getCitizenDataFromId

This method will return an instance of Citizen.
If the ID is invalid, an InvalidIdException will be thrown.
If the country does not support data extraction, an UnsupportedOperationException will be thrown.

$citizen = $socrates->getCitizenDataFromId('3860123012', Country::Estonia);

The Citizen class stores the extracted citizen data in a consistent format across all countries.
It exposes the getGender(), getDateOfBirth(), getAge() and getPlaceOfBirth() methods.

getGender will return an instance of the Gender enum.
getPlaceOfBirth will return a city or region name as a string.
getAge() returns the age of the citizen as an int.
getDateOfBirth() returns a DateTime instance.

Using the example above, Estonia only encodes the date of birth and gender of the citizen in their ID. So the above methods will return:

echo $citizen->getGender(); // 'Gender::Male'
echo $citizen->getDateOfBirth(); // DateTime instance with the date '1986-01-23'
echo $citizen->getAge(); // (The current age as a number)
echo $citizen->getPlaceOfBirth(); // null - Estonia does not encode place of birth on its ID numbers

Supported and Unsupported Countries

Here you can see the full list of supported countries and whether they support data extraction.

Four european countries are currently unsupported: Austria πŸ‡¦πŸ‡Ή, Belarus πŸ‡§πŸ‡Ύ, Cyprus πŸ‡¨πŸ‡Ύ and Luxembourg πŸ‡±πŸ‡Ί. A number of countries in the Americas are also unsupported. This is because we could not find a reliable source for the algorithm, if at all. Help would be appreciated to get these countries supported.

Testing

composer test

Ports

This package is also available for the following languages:

Rust
Java

Contributing

Did you find a problem in any of the algorithms? Do you know how to implement a country which we have missed? Are there any improvements that you think should be made to the codebase? Any help is appreciated! Take a look at our contributing guidelines.

Code of Conduct

Our CoC is based on Ruby's. Check out our code of conduct.

License

The MIT License (MIT). Please see License File for more information.

Credits

Socrates was made with πŸ’– by Alexandre Olival and JoΓ£o Cruz. We are Reducktion. We hope to make someone's life easier after all the hard work compiling, researching, reverse-engineering and agonizing over ID validation algorithms - many of which were very obscure and hard to find.

Special Thanks

A big thanks goes to these people who helped us either test with real life IDs or guide us in finding the algorithm for their countries:

  • Alexandra from πŸ‡·πŸ‡΄
  • Berilay from πŸ‡ΉπŸ‡·
  • Christian from πŸ‡¨πŸ‡­
  • Domynikas from πŸ‡±πŸ‡Ή
  • Jeppe from πŸ‡©πŸ‡°
  • Jeremy from πŸ‡«πŸ‡·
  • Lisa from πŸ‡¬πŸ‡·
  • Miguel from πŸ‡ͺπŸ‡Έ

and Nair from πŸ‡΅πŸ‡Ή for the package name.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


SLourenco

πŸ› πŸ“– πŸ’»

FlΓ‘vio Heleno

πŸ’» πŸ“–

Yves Bos

πŸ“– πŸ’» πŸ›

bofalke

πŸ’» πŸ“–

Rodolpho Lima

πŸ’» πŸ“–

tiagomichaelsousa

πŸ’» πŸ“–

This project follows the all-contributors specification. Contributions of any kind welcome!