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 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!
composer require reducktion/socrates
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);
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.';
}
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
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.
composer test
This package is also available for the following languages:
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.
Our CoC is based on Ruby's. Check out our code of conduct.
The MIT License (MIT). Please see License File for more information.
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.
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.
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!