Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/reducktion/socrates

PHP package to Validate and Extract information from National Identification Numbers.
https://github.com/reducktion/socrates

composer-package countries hacktoberfest identification-number identity laravel national-id-validator php php-package symfony validation

Last synced: 29 days ago
JSON representation

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

Awesome Lists containing this project

README

        


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](https://en.wikipedia.org/wiki/National_identification_number).
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](https://github.com/reducktion/socrates#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](https://en.wikipedia.org/wiki/ISO_3166-2) as the first and second parameters respectively. Simply instantiate the class and call the method you wish:

```php
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.

```php
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.

```php
$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:



```php
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](COUNTRIES.md) 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](https://github.com/reducktion/socrates-rs)

[Java](https://github.com/reducktion/socrates-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](CONTRIBUTING.md).

## Code of Conduct
Our CoC is based on Ruby's. Check out [our code of conduct](CODE_OF_CONDUCT.md).

## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

## Credits
Socrates was made with πŸ’– by [Alexandre Olival](https://github.com/AlexOlival) and [JoΓ£o Cruz](https://github.com/JoaoFSCruz).
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](https://allcontributors.org/docs/en/emoji-key)):



SLourenco

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

FlΓ‘vio Heleno

πŸ’» πŸ“–

Yves Bos

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

bofalke

πŸ’» πŸ“–

Rodolpho Lima

πŸ’» πŸ“–

tiagomichaelsousa

πŸ’» πŸ“–

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!