diff --git a/.all-contributorsrc b/.all-contributorsrc
index 4f1333f..1528393 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -74,4 +74,4 @@
"repoType": "github",
"repoHost": "https://github.com",
"skipCi": true
-}
+}
\ No newline at end of file
diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml
index 781e3b3..dfba794 100644
--- a/.github/workflows/php.yml
+++ b/.github/workflows/php.yml
@@ -1,4 +1,4 @@
-name: Build
+name: Run Tests
on:
push:
@@ -12,27 +12,43 @@ on:
jobs:
build:
-
runs-on: ubuntu-latest
+ strategy:
+ fail-fast: false
+ matrix:
+ php: ['8.0', '7.4', '7.3', '7.2']
+ dependency-version: [prefer-lowest, prefer-stable]
+
+ name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }}
steps:
- - uses: actions/checkout@v2
+ - name: Checkout
+ uses: actions/checkout@v2
+
+ - name: Setup PHP
+ uses: shivammathur/setup-php@v2
+ with:
+ php-version: ${{ matrix.php }}
+ extensions: mbstring, intl
+ ini-values: post_max_size=256M, log_errors=1
+ coverage: pcov
+ tools: pecl
- - name: Validate composer.json and composer.lock
- run: composer validate
+ - name: Validate composer.json and composer.lock
+ run: composer validate
- - name: Cache Composer packages
- id: composer-cache
- uses: actions/cache@v2
- with:
- path: vendor
- key: ${{ runner.os }}-node-${{ hashFiles('**/composer.lock') }}
- restore-keys: |
- ${{ runner.os }}-node-
-
- - name: Install dependencies
- if: steps.composer-cache.outputs.cache-hit != 'true'
- run: composer install --prefer-dist --no-progress --no-suggest
-
- - name: Run test suite
- run: vendor/bin/phpunit
+ - name: Cache Composer packages
+ id: composer-cache
+ uses: actions/cache@v2
+ with:
+ path: vendor
+ key: ${{ runner.os }}-node-${{ hashFiles('**/composer.lock') }}
+ restore-keys: |
+ ${{ runner.os }}-node-
+
+ - name: Install dependencies
+ if: steps.composer-cache.outputs.cache-hit != 'true'
+ run: composer install --prefer-dist --no-progress --no-suggest
+
+ - name: Run test suite
+ run: vendor/bin/phpunit
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index d3610c5..2e64aaa 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -1,9 +1,13 @@
# CONTRIBUTING
-Contributions are welcome, and are accepted via pull requests.
-Please review these guidelines before submitting any pull requests.
+Thank you for your interest in wanting to contribute to Socrates!
+Up next is a small guide to create your first pull request. Check further below for a
+[walk-through](#how-to-implement-a-country-from-scratch) on how to
+contribute with a new country from scratch.
-## Process
+## The Basics
+
+### Process
1. Fork the project
1. Create a new branch
@@ -11,18 +15,469 @@ Please review these guidelines before submitting any pull requests.
1. Open a pull request detailing your changes.
1. Describe what your pull request is (change, bugfix, new country implementation, etc.)
-## Guidelines
+### Guidelines
-* We use PSR-12 as our coding style, make sure to follow it.
-* If you have multiple commits, squashing some of them might help us have a better sense of what you did.
+* We use [PSR-12](https://www.php-fig.org/psr/psr-12/) as our coding style, make sure to follow it.
+* If you have multiple commits, squashing some of them might help us better understand what you did.
* You may need to [rebase](https://git-scm.com/book/en/v2/Git-Branching-Rebasing) to avoid merge conflicts.
-## Setup
+### Setup
Clone your fork, then install the dev dependencies:
```bash
composer install
```
-It is avised to run a Laravel/Vanilla PHP application locally, and pull in your local fork of the package to test.
+It is advised to run a Laravel/Vanilla PHP application locally, and pull in your local fork of the package to test.
Check [this post](https://johnbraun.blog/posts/creating-a-laravel-package-1) for a guide on how to do it.
+
+## How to implement a country from scratch
+
+### Step 1 - Research
+
+Let's imagine we want to implement the fictional european country of "Socratia".
The first thing we should do
+is find out what its two-digit ISO code is. After that, we need to *thoroughly* research and find out what the
+Socratia's National Identification Number is (henceforth referred to as NIN) and how it is validated, as well as
+collect some example specimens to then write our tests. Additionally, we should find out if the NIN encodes any personal
+information on the Citizen - gender, date of birth or place of birth.
+
+After some imaginary googling we find out that:
+- The two-digit ISO code is `SC`.
+- The NIN is eleven characters long excluding hyphens.
+- The NIN follows the format `GR-DDDDDDDD-C`, G a letter for gender, R a letter for region, the Ds
+ are numbers representing a date and C is a control number.
+- `G` can either be "M" or "F".
+- `R` can be "P", "J" or "R" referring to its three regions of "Phpilia", "Javardia" and "Rustara".
+- `C` must be an even number, but it can not be "2" if the citizen was registered after 2001 as those particular
+ numbers have been phased out.
+
+Clearly there is some citizen information encoded in that ID. This means we will implement both a Validator and
+an Extractor.
+
+
+### Step 2 - Implement the Validator
+
+Let's start working on our implementation. First we need to create the validator class.
+Create a directory in `src/Core/Europe/` named "Socratia" and create a new PHP class inside it.
+We'll name it `SocratiaIdvalidator` and implement `IdValidator`:
+
+```php
+
+Let's go ahead and do that now:
+
+```php
+sanitize($id);
+ }
+
+ // We usually create a sanitize method to strip the
+ // ID of any separator characters and check its length.
+ private function sanitize($string $id): string
+ {
+ $id = str_replace('-', '', $id);
+
+ $idLength = strlen($id);
+
+ // We want an eleven character string after stripping it from hyphens, else
+ // we throw one of our internal exceptions.
+
+ if ($idLength !== 11) {
+ throw new InvalidLengthException('Socratian NIN', '11', $idLength);
+ }
+
+ $id = strtoupper($id);
+
+ return $id;
+ }
+}
+```
+
+Great! We can now check the other conditions and implement the rest of the `validate()` method:
+
+```php
+public function validate(string $id): bool
+{
+ $id = $this->sanitize($id);
+
+ // Characters #3 to #10 must be digits
+ $dateDigits = substr($id, 2, 8);
+ if (!is_numeric($dateDigits)) {
+ return false;
+ }
+
+ // `G` can either be "M" or "F"
+ $genderCharacter = $id[0];
+ if ($genderCharacter !== 'M' && $genderCharacter !== 'F') {
+ return false;
+ }
+
+ // `R` can be "P", "J" or "R"
+ $regionCharacter = $id[1];
+ if ($regionCharacter !== 'P' && $regionCharacter !== 'J' && $regionCharacter !== 'R') {
+ return false;
+ }
+
+ // `C` must be an even number, but it can not be "2" if the citizen was registered after 2001
+ $controlDigit = (int) $id[10];
+ $year = (int) substr($dateDigits, 0, 4);
+ if ($controlDigit === 2 && $year >= 2001) {
+ return false;
+ }
+
+ if ($controlDigit % 2 !== 0) {
+ return false;
+ }
+
+ // Don't accept silly dates like 2010-99-99
+ $month = (int) substr($dateDigits, 4, 2);
+ $day = (int) substr($dateDigits, 6, 2);
+ try {
+ new DateTime("$year-$month-$day");
+ } catch (Exception $e) {
+ return false;
+ }
+
+ // The ID is valid!
+ return true;
+}
+```
+That's it for our validator!
+Now we head off to `Countries.php` and add the class to the validators array. You'll notice it is alphabetically ordered
+and organised by the continent, so let's be sure to add it in the right place:
+
+```php
+ \Reducktion\Socrates\Core\Europe\Albania\AlbaniaIdValidator::class,
+ //...
+ 'SC' => \Reducktion\Socrates\Core\Europe\Socratia\SocratiaIdValidator::class,
+ 'SE' => \Reducktion\Socrates\Core\Europe\Sweden\SwedenIdValidator::class,
+ //...
+```
+
+Let's also add a constant to `Country.php` with our country code for convenience:
+
+```php
+sanitize($id);
+
+ if (! (new SocratiaIdValidator())->validate($id)) {
+ // Throw one of our internal exceptions
+ throw new InvalidIdException();
+ }
+
+ // We are good to go
+}
+```
+
+A good practice is to segment each piece logic for each type of information in its own method.
+If you recall from above, we can extract the gender, date of birth and place of birth from the Socratian ID.
+Our `extract` method then becomes:
+
+```php
+public function extract(string $id): Citizen
+{
+ $id = $this->sanitize($id);
+
+ if (! (new SocratiaIdValidator())->validate($id)) {
+ // Throw one of our internal exceptions
+ throw new InvalidIdException();
+ }
+
+ $gender = $this->getGender($id);
+ $dateOfBirth = $this->getDateOfBirth($id);
+ $placeOfBirth = $this->getPlaceOfBirth($id);
+
+ $citizen = new Citizen();
+ $citizen->setGender($gender);
+ $citizen->setDateOfBirth($dateOfBirth);
+ $citizen->setPlaceOfBirth($placeOfBirth);
+
+ return $citizen;
+}
+```
+
+Let us do each in order. Getting the gender is simple. Because we have already validated the ID we
+can be confident that the gender character is safe to check:
+
+```php
+public function getGender(string $id): string
+{
+ // Use our internal "enum"
+ return $id[0] === 'M' ? Gender::MALE : Gender::FEMALE;
+}
+```
+
+Now for the date:
+
+```php
+public function getDateOfBirth(string $id): DateTime
+{
+ $dateDigits = substr($id, 2, 8);
+
+ $year = (int) substr($dateDigits, 0, 4);
+ $month = (int) substr($dateDigits, 4, 2);
+ $day = (int) substr($dateDigits, 6, 2);
+
+ return new DateTime("$year-$month-$day");
+}
+```
+
+For the place of birth let us create a separate class in the same directory to hold all possible region values:
+
+```php
+ 'Phpilia',
+ 'J' => 'Javardia',
+ 'R' => 'Rustaria',
+ ];
+}
+```
+
+Getting the place of birth is now as simple as:
+
+```php
+private function getPlaceOfBirth(string $id): string
+{
+ $regionCharacter = $id[1];
+
+ if (! isset(SocratiaRegionsList::$regions[$regionCharacter])) {
+ // Throw another of our internal exceptions
+ throw new UnrecognisedPlaceOfBirthException(
+ "The provided character '$regionCharacter' does not match any regions."
+ );
+ }
+
+ return SocratiaRegionsList::$regions[$regionCharacter];
+ }
+```
+
+Finally, as before, let us register our extractor class in the extractors array in `Countries.php`:
+
+```php
+ \Reducktion\Socrates\Core\Europe\Albania\AlbaniaCitizenInformationExtractor::class,
+ //...
+ 'SC' => \Reducktion\Socrates\Core\Europe\Socratia\SocratiaCitizenInformationExtractor::class,
+ 'SE' => \Reducktion\Socrates\Core\Europe\Sweden\SwedenCitizenInformationExtractor::class,
+ //...
+```
+
+That's it! We have implemented both the validator and the extractor for our fictional country.
+However, we are not done yet as we need to...
+
+### Step 4 - Write Tests
+
+We want to ensure the quality of the algorithm and the extracted data. To do so, we will now write a test class.
+First head over to `tests/Feature/Europe` and create a new `SocratiaTest` test class. We will extend our `FeatureTest`
+abstract class:
+
+```php
+people = [
+ 'alexandre' => [
+ 'id' => 'MR-19940916-4',
+ 'gender' => Gender::MALE,
+ 'dob' => new DateTime('1994-09-16'),
+ 'age' => 26, // as of 2021
+ 'pob' => 'Rustaria',
+ ],
+ // ...
+ ];
+
+ // Try to cover as many edge cases as possible
+ $this->invalidIds = [
+ 'OR-19940916-4', // Gender is wrong
+ 'MZ-19940916-4', // Place of birth does not exist
+ 'MR-19941916-4', // Date of birth is invalid
+ 'MR-19940916-3', // Control digit is not even
+ 'MR-20020916-3', // Control digit is two but the citizen was born post 2001
+ 'MR-020916-3', // ID is too short
+ // ...
+ ];
+}
+```
+
+Now for each of those two data sets we run our validator and extractor classes and check if all is well:
+
+```php
+public function test_extract_behaviour(): void
+{
+ foreach ($this->people as $person) {
+ $citizen = Socrates::getCitizenDataFromId($person['id'], Country::SOCRATIA);
+
+ self::assertEquals($person['gender'], $citizen->getGender());
+ self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth());
+ self::assertEquals($person['dob'], $citizen->getDateOfBirthNative());
+ self::assertEquals($person['age'], $citizen->getAge());
+ self::assertEquals($person['pob'], $citizen->getPlaceOfBirth());
+ }
+
+ $this->expectException(InvalidIdException::class);
+
+ // An invalid ID should not be able to be extracted
+ Socrates::getCitizenDataFromId('OR-19940916-4', 'SC');
+}
+
+public function test_validation_behaviour(): void
+{
+ foreach ($this->people as $person) {
+ self::assertTrue(
+ Socrates::validateId($person['id'], 'SC')
+ );
+ }
+
+ foreach ($this->invalidIds as $fc) {
+ self::assertFalse(
+ Socrates::validateId($fc, 'SC')
+ );
+ }
+
+ $this->expectException(InvalidLengthException::class);
+
+ Socrates::validateId('OR-940916-4', 'SC');
+}
+```
+
+### Final step - Update the documentation
+
+Finally we just need to update our `COUNTRIES.md` file for reference:
+
+```text
+| Country | Country Code | Validation | Extraction |
+|---------------------------|--------------|--------------------|--------------------|
+| Albania ๐ฆ๐ฑ | AL | :heavy_check_mark: | :heavy_check_mark: |
+(...)
+| Socratia ๐ด | SC | :heavy_check_mark: | :heavy_check_mark: |
+| Sweden ๐ธ๐ช | SE | :heavy_check_mark: | :heavy_check_mark: |
+```
+
+Congratulations! Now go ahead and implement a real country! ๐
\ No newline at end of file
diff --git a/COUNTRIES.md b/COUNTRIES.md
index b5dca90..70cab75 100644
--- a/COUNTRIES.md
+++ b/COUNTRIES.md
@@ -3,16 +3,20 @@
| Country | Country Code | Validation | Extraction |
|---------------------------|--------------|--------------------|--------------------|
| Albania ๐ฆ๐ฑ | AL | :heavy_check_mark: | :heavy_check_mark: |
+| Argentina ๐ฆ๐ท | AR | :heavy_check_mark: | :x: |
| Belgium ๐ง๐ช | BE | :heavy_check_mark: | :heavy_check_mark: |
| Bosnia and Herzegovina ๐ง๐ฆ | BA | :heavy_check_mark: | :heavy_check_mark: |
| Bulgaria ๐ง๐ฌ | BG | :heavy_check_mark: | :heavy_check_mark: |
| Brazil ๐ง๐ท | BR | :heavy_check_mark: | :x: |
+| Chile ๐จ๐ฑ | CL | :heavy_check_mark: | :x: |
| Croatia ๐ญ๐ท | HR | :heavy_check_mark: | :heavy_check_mark: |
| Czech Republic ๐จ๐ฟ | CZ | :heavy_check_mark: | :heavy_check_mark: |
| Denmark ๐ฉ๐ฐ | DK | :heavy_check_mark: | :heavy_check_mark: |
+| Ecuador ๐ช๐จ | EC | :heavy_check_mark: | :x: |
| Estonia ๐ช๐ช | EE | :heavy_check_mark: | :heavy_check_mark: |
| Finland ๐ซ๐ฎ | FI | :heavy_check_mark: | :heavy_check_mark: |
| France ๐ซ๐ท | FR | :heavy_check_mark: | :heavy_check_mark: |
+| Germany ๐ฉ๐ช | DE | :heavy_check_mark: | :x: |
| Greece ๐ฌ๐ท | GR | :heavy_check_mark: | :x: |
| Hungary ๐ญ๐บ | HU | :heavy_check_mark: | :heavy_check_mark: |
| Iceland ๐ฎ๐ธ | IS | :heavy_check_mark: | :heavy_check_mark: |
@@ -21,12 +25,13 @@
| Kosovo ๐ฝ๐ฐ | XK | :heavy_check_mark: | :heavy_check_mark: |
| Latvia ๐ฑ๐ป | LV | :heavy_check_mark: | :heavy_check_mark: |
| Lithuania ๐ฑ๐น | LT | :heavy_check_mark: | :heavy_check_mark: |
-| Luxembourg ๐ฑ๐บ | LU | :heavy_check_mark: | :heavy_check_mark: |
| Moldova ๐ฒ๐ฉ | MD | :heavy_check_mark: | :x: |
| Montenegro ๐ฒ๐ช | ME | :heavy_check_mark: | :heavy_check_mark: |
+| Mรฉxico ๐ฒ๐ฝ | ME | :heavy_check_mark: | :heavy_check_mark: |
| Netherlands ๐ณ๐ฑ | NL | :heavy_check_mark: | :x: |
| North Macedonia ๐ฒ๐ฐ | MK | :heavy_check_mark: | :heavy_check_mark: |
| Norway ๐ณ๐ด | NO | :heavy_check_mark: | :heavy_check_mark: |
+| Peru ๐ต๐ช | PE | :heavy_check_mark: | :x: |
| Poland ๐ต๐ฑ | PL | :heavy_check_mark: | :heavy_check_mark: |
| Portugal ๐ต๐น | PT | :heavy_check_mark: | :x: |
| Romania ๐ท๐ด | RO | :heavy_check_mark: | :heavy_check_mark: |
@@ -39,3 +44,4 @@
| Turkey ๐น๐ท | TR | :heavy_check_mark: | :x: |
| Ukraine ๐บ๐ฆ | UA | :heavy_check_mark: | :heavy_check_mark: |
| United Kingdom ๐ฌ๐ง | GB | :heavy_check_mark: | :x: |
+| Uruguay ๐บ๐พ | UY | :heavy_check_mark: | :x: |
diff --git a/README.md b/README.md
index 51a59e5..bf25cfc 100644
--- a/README.md
+++ b/README.md
@@ -19,19 +19,14 @@
You can now read [a blog post](https://medium.com/@alex.olival/road-to-mastery-building-an-open-source-package-3936f57aed81) about how this package was created and its goals.
-**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). At the moment, most countries in Europe are supported, but the goal is to eventually support as many countries in the world as possible.
+**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 American ones, but the goal is to eventually 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.
-For Laravel, a Facade and request Validator is also available (see below).
+A Facade and Validation Rule is also available for Laravel (see below).
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. However, we recommend you review your country's data 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. -Our goals: -* Standardize and centralise what is usually very difficult and sparse information to find. -* Have a consistent API for retrieving citizen information from an ID, if available. -* Test each individual country validation and data extraction algorithm with a number of valid and invalid IDs. -* Support as many countries as viably possible. - ## Installation `composer require reducktion/socrates` @@ -40,63 +35,66 @@ Socrates provides two methods: `validateId` and `getCitizenDataFromId`. Both rec ```php use Reducktion\Socrates\Socrates; +use Reducktion\Socrates\Constants\Country; $socrates = new Socrates(); +$socrates->validateId('14349483 0 ZV3', Country::PORTUGAL); +// or $socrates->validateId('14349483 0 ZV3', 'PT'); ``` -For Laravel, a facade is also available for your convenience: +If you're using Laravel a facade is also available for your convenience: ```php use Reducktion\Socrates\Laravel\Facades\Socrates; +use Reducktion\Socrates\Constants\Country; -Socrates::getCitizenDataFromId('550309-6447', 'SE'); +Socrates::getCitizenDataFromId('550309-6447', Country::SWEDEN); ``` -You can also use the `national_id:[COUNTRY CODE]` request validation rule: +Still in Laravel you can use the `national_id:[COUNTRY CODE]` request validation rule. +Here is an example checking if a request parameter is a valid latvian ID: ```php $request->validate([ - 'id' => 'national_id:lv' + 'national_id' => 'required|string|national_id:lv', ]); ``` -Still in Laravel, the package will try to guess the country to be validated by the default locale: - -```php -App::setLocale('PT'); - -Socrates::validateId('11084129 8 ZX8'); -``` - -However this is **not** recommended. The safest way is to always explicitly pass the country code as the second parameter. ### validateId -This method will return true or false depending on the validity of the ID. +This method will return true or false. In case the ID has the wrong character length, an `InvalidLengthException` will be thrown. ```php -if ($socrates->validateId('719102091', 'NL')) { - echo 'Valid ID.' +if ($socrates->validateId('719102091', Country::NETHERLANDS)) { + echo 'Valid ID.'; } else { - echo 'Invalid ID.' + 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. +This method will return an instance of `Citizen`.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(); // 'Male' -echo $citizen->getDateOfBirth(); // A Carbon instance with the date '1986-01-23' -echo $citizen->getAge(); // 34 (as of June 2020) +echo $citizen->getDateOfBirthNative(); // DateTime instance with the date '1986-01-23' +echo $citizen->getDateOfBirth(); // DEPRECATED - Carbon instance with the date '1986-01-23' +echo $citizen->getAge(); // 34 echo $citizen->getPlaceOfBirth(); // null ``` @@ -104,7 +102,8 @@ echo $citizen->getPlaceOfBirth(); // null [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 Germany ๐ฉ๐ช. 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. +Four european countries are currently unsupported: Austria ๐ฆ๐น, Belarus ๐ง๐พ, Cyprus ๐จ๐พ and Luxembourg ๐ฑ๐บ. +A number of countries in the Americas are also unsupproted. 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` @@ -168,4 +167,4 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d -This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! +This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! \ No newline at end of file diff --git a/composer.json b/composer.json index cf87335..a423b0d 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ } ], "require": { - "php": "^7.2", + "php": "^7.2|^8.0", "nesbot/carbon": "^2.35.0" }, "require-dev": { diff --git a/docs/example.png b/docs/example.png index a1a928e..1bd0f90 100644 Binary files a/docs/example.png and b/docs/example.png differ diff --git a/src/Config/Countries.php b/src/Config/Countries.php index 291f433..5bfb4c1 100644 --- a/src/Config/Countries.php +++ b/src/Config/Countries.php @@ -2,7 +2,7 @@ namespace Reducktion\Socrates\Config; -class Countries +abstract class Countries { public static $all = [ 'AF', @@ -258,72 +258,98 @@ class Countries ]; public static $validators = [ - 'AL' => \Reducktion\Socrates\Core\Albania\AlbaniaIdValidator::class, - 'BA' => \Reducktion\Socrates\Core\BosniaAndHerzegovina\BosniaAndHerzegovinaIdValidator::class, - 'BE' => \Reducktion\Socrates\Core\Belgium\BelgiumIdValidator::class, - 'BG' => \Reducktion\Socrates\Core\Bulgaria\BulgariaIdValidator::class, - 'BR' => \Reducktion\Socrates\Core\Brazil\BrazilIdValidator::class, - 'CH' => \Reducktion\Socrates\Core\Switzerland\SwitzerlandIdValidator::class, - 'CZ' => \Reducktion\Socrates\Core\CzechRepublic\CzechRepublicIdValidator::class, - 'DK' => \Reducktion\Socrates\Core\Denmark\DenmarkIdValidator::class, - 'EE' => \Reducktion\Socrates\Core\Estonia\EstoniaIdValidator::class, - 'ES' => \Reducktion\Socrates\Core\Spain\SpainIdValidator::class, - 'FI' => \Reducktion\Socrates\Core\Finland\FinlandIdValidator::class, - 'FR' => \Reducktion\Socrates\Core\France\FranceIdValidator::class, - 'GB' => \Reducktion\Socrates\Core\UnitedKingdom\UnitedKingdomIdValidator::class, - 'GR' => \Reducktion\Socrates\Core\Greece\GreeceIdValidator::class, - 'HR' => \Reducktion\Socrates\Core\Croatia\CroatiaIdValidator::class, - 'HU' => \Reducktion\Socrates\Core\Hungary\HungaryIdValidator::class, - 'IE' => \Reducktion\Socrates\Core\Ireland\IrelandIdValidator::class, - 'IS' => \Reducktion\Socrates\Core\Iceland\IcelandIdValidator::class, - 'IT' => \Reducktion\Socrates\Core\Italy\ItalyIdValidator::class, - 'LT' => \Reducktion\Socrates\Core\Lithuania\LithuaniaIdValidator::class, - 'LU' => \Reducktion\Socrates\Core\Luxembourg\LuxembourgIdValidator::class, - 'LV' => \Reducktion\Socrates\Core\Latvia\LatviaIdValidator::class, - 'MD' => \Reducktion\Socrates\Core\Moldova\MoldovaIdValidator::class, - 'ME' => \Reducktion\Socrates\Core\Montenegro\MontenegroIdValidator::class, - 'MK' => \Reducktion\Socrates\Core\NorthMacedonia\NorthMacedoniaIdValidator::class, - 'NL' => \Reducktion\Socrates\Core\Netherlands\NetherlandsIdValidator::class, - 'NO' => \Reducktion\Socrates\Core\Norway\NorwayIdValidator::class, - 'PL' => \Reducktion\Socrates\Core\Poland\PolandIdValidator::class, - 'PT' => \Reducktion\Socrates\Core\Portugal\PortugalIdValidator::class, - 'RO' => \Reducktion\Socrates\Core\Romania\RomaniaIdValidator::class, - 'RS' => \Reducktion\Socrates\Core\Serbia\SerbiaIdValidator::class, - 'SE' => \Reducktion\Socrates\Core\Sweden\SwedenIdValidator::class, - 'SI' => \Reducktion\Socrates\Core\Slovenia\SloveniaIdValidator::class, - 'SK' => \Reducktion\Socrates\Core\Slovakia\SlovakiaIdValidator::class, - 'TR' => \Reducktion\Socrates\Core\Turkey\TurkeyIdValidator::class, - 'UA' => \Reducktion\Socrates\Core\Ukraine\UkraineIdValidator::class, - 'XK' => \Reducktion\Socrates\Core\Kosovo\KosovoIdValidator::class, + /** + * Validators for european countries. + */ + 'AL' => \Reducktion\Socrates\Core\Europe\Albania\AlbaniaIdValidator::class, + 'BA' => \Reducktion\Socrates\Core\Europe\BosniaAndHerzegovina\BosniaAndHerzegovinaIdValidator::class, + 'BE' => \Reducktion\Socrates\Core\Europe\Belgium\BelgiumIdValidator::class, + 'BG' => \Reducktion\Socrates\Core\Europe\Bulgaria\BulgariaIdValidator::class, + 'CH' => \Reducktion\Socrates\Core\Europe\Switzerland\SwitzerlandIdValidator::class, + 'CZ' => \Reducktion\Socrates\Core\Europe\CzechRepublic\CzechRepublicIdValidator::class, + 'DE' => \Reducktion\Socrates\Core\Europe\Germany\GermanIdValidator::class, + 'DK' => \Reducktion\Socrates\Core\Europe\Denmark\DenmarkIdValidator::class, + 'EE' => \Reducktion\Socrates\Core\Europe\Estonia\EstoniaIdValidator::class, + 'ES' => \Reducktion\Socrates\Core\Europe\Spain\SpainIdValidator::class, + 'FI' => \Reducktion\Socrates\Core\Europe\Finland\FinlandIdValidator::class, + 'FR' => \Reducktion\Socrates\Core\Europe\France\FranceIdValidator::class, + 'GB' => \Reducktion\Socrates\Core\Europe\UnitedKingdom\UnitedKingdomIdValidator::class, + 'GR' => \Reducktion\Socrates\Core\Europe\Greece\GreeceIdValidator::class, + 'HR' => \Reducktion\Socrates\Core\Europe\Croatia\CroatiaIdValidator::class, + 'HU' => \Reducktion\Socrates\Core\Europe\Hungary\HungaryIdValidator::class, + 'IE' => \Reducktion\Socrates\Core\Europe\Ireland\IrelandIdValidator::class, + 'IS' => \Reducktion\Socrates\Core\Europe\Iceland\IcelandIdValidator::class, + 'IT' => \Reducktion\Socrates\Core\Europe\Italy\ItalyIdValidator::class, + 'LT' => \Reducktion\Socrates\Core\Europe\Lithuania\LithuaniaIdValidator::class, + 'LV' => \Reducktion\Socrates\Core\Europe\Latvia\LatviaIdValidator::class, + 'MD' => \Reducktion\Socrates\Core\Europe\Moldova\MoldovaIdValidator::class, + 'ME' => \Reducktion\Socrates\Core\Europe\Montenegro\MontenegroIdValidator::class, + 'MK' => \Reducktion\Socrates\Core\Europe\NorthMacedonia\NorthMacedoniaIdValidator::class, + 'NL' => \Reducktion\Socrates\Core\Europe\Netherlands\NetherlandsIdValidator::class, + 'NO' => \Reducktion\Socrates\Core\Europe\Norway\NorwayIdValidator::class, + 'PL' => \Reducktion\Socrates\Core\Europe\Poland\PolandIdValidator::class, + 'PT' => \Reducktion\Socrates\Core\Europe\Portugal\PortugalIdValidator::class, + 'RO' => \Reducktion\Socrates\Core\Europe\Romania\RomaniaIdValidator::class, + 'RS' => \Reducktion\Socrates\Core\Europe\Serbia\SerbiaIdValidator::class, + 'SE' => \Reducktion\Socrates\Core\Europe\Sweden\SwedenIdValidator::class, + 'SI' => \Reducktion\Socrates\Core\Europe\Slovenia\SloveniaIdValidator::class, + 'SK' => \Reducktion\Socrates\Core\Europe\Slovakia\SlovakiaIdValidator::class, + 'TR' => \Reducktion\Socrates\Core\Europe\Turkey\TurkeyIdValidator::class, + 'UA' => \Reducktion\Socrates\Core\Europe\Ukraine\UkraineIdValidator::class, + 'XK' => \Reducktion\Socrates\Core\Europe\Kosovo\KosovoIdValidator::class, + + /** + * Validators for north american countries. + */ + 'CA' => \Reducktion\Socrates\Core\NorthAmerica\Canada\CanadaIdValidator::class, + 'US' => \Reducktion\Socrates\Core\NorthAmerica\UnitedStates\UnitedStatesIdValidator::class, + 'MX' => \Reducktion\Socrates\Core\NorthAmerica\Mexico\MexicoIdValidator::class, + + /** + * Validators for south american countries. + */ + 'AR' => \Reducktion\Socrates\Core\SouthAmerica\Argentina\ArgentinaIdValidator::class, + 'BR' => \Reducktion\Socrates\Core\SouthAmerica\Brazil\BrazilIdValidator::class, + 'CL' => \Reducktion\Socrates\Core\SouthAmerica\Chile\ChileIdValidator::class, + 'EC' => \Reducktion\Socrates\Core\SouthAmerica\Ecuador\EcuadorIdValidator::class, + 'PE' => \Reducktion\Socrates\Core\SouthAmerica\Peru\PeruIdValidator::class, + 'UY' => \Reducktion\Socrates\Core\SouthAmerica\Uruguay\UruguayIdValidator::class, ]; public static $extractors = [ - 'AL' => \Reducktion\Socrates\Core\Albania\AlbaniaCitizenInformationExtractor::class, - 'BA' => \Reducktion\Socrates\Core\BosniaAndHerzegovina\BosniaAndHerzegovinaCitizenInformationExtractor::class, - 'BE' => \Reducktion\Socrates\Core\Belgium\BelgiumCitizenInformationExtractor::class, - 'BG' => \Reducktion\Socrates\Core\Bulgaria\BulgariaCitizenInformationExtractor::class, - 'DK' => \Reducktion\Socrates\Core\Denmark\DenmarkCitizenInformationExtractor::class, - 'CZ' => \Reducktion\Socrates\Core\CzechRepublic\CzechRepublicCitizenInformationExtractor::class, - 'EE' => \Reducktion\Socrates\Core\Estonia\EstoniaCitizenInformationExtractor::class, - 'FI' => \Reducktion\Socrates\Core\Finland\FinlandCitizenInformationExtractor::class, - 'FR' => \Reducktion\Socrates\Core\France\FranceCitizenInformationExtractor::class, - 'HR' => \Reducktion\Socrates\Core\Croatia\CroatiaCitizenInformationExtractor::class, - 'HU' => \Reducktion\Socrates\Core\Hungary\HungaryCitizenInformationExtractor::class, - 'IS' => \Reducktion\Socrates\Core\Iceland\IcelandCitizenInformationExtractor::class, - 'IT' => \Reducktion\Socrates\Core\Italy\ItalyCitizenInformationExtractor::class, - 'LT' => \Reducktion\Socrates\Core\Lithuania\LithuaniaCitizenInformationExtractor::class, - 'LU' => \Reducktion\Socrates\Core\Luxembourg\LuxembourgCitizenInformationExtractor::class, - 'LV' => \Reducktion\Socrates\Core\Latvia\LatviaCitizenInformationExtractor::class, - 'ME' => \Reducktion\Socrates\Core\Montenegro\MontenegroCitizenInformationExtractor::class, - 'MK' => \Reducktion\Socrates\Core\NorthMacedonia\NorthMacedoniaCitizenInformationExtractor::class, - 'NO' => \Reducktion\Socrates\Core\Norway\NorwayCitizenInformationExtractor::class, - 'PL' => \Reducktion\Socrates\Core\Poland\PolandCitizenInformationExtractor::class, - 'RO' => \Reducktion\Socrates\Core\Romania\RomaniaCitizenInformationExtractor::class, - 'RS' => \Reducktion\Socrates\Core\Serbia\SerbiaCitizenInformationExtractor::class, - 'SE' => \Reducktion\Socrates\Core\Sweden\SwedenCitizenInformationExtractor::class, - 'SI' => \Reducktion\Socrates\Core\Slovenia\SloveniaCitizenInformationExtractor::class, - 'SK' => \Reducktion\Socrates\Core\Slovakia\SlovakiaCitizenInformationExtractor::class, - 'UA' => \Reducktion\Socrates\Core\Ukraine\UkraineCitizenInformationExtractor::class, - 'XK' => \Reducktion\Socrates\Core\Kosovo\KosovoCitizenInformationExtractor::class, + /** + * Extractors for european countries. + */ + 'AL' => \Reducktion\Socrates\Core\Europe\Albania\AlbaniaCitizenInformationExtractor::class, + 'BA' => \Reducktion\Socrates\Core\Europe\BosniaAndHerzegovina\BosniaAndHerzegovinaCitizenInformationExtractor::class, + 'BE' => \Reducktion\Socrates\Core\Europe\Belgium\BelgiumCitizenInformationExtractor::class, + 'BG' => \Reducktion\Socrates\Core\Europe\Bulgaria\BulgariaCitizenInformationExtractor::class, + 'DK' => \Reducktion\Socrates\Core\Europe\Denmark\DenmarkCitizenInformationExtractor::class, + 'CZ' => \Reducktion\Socrates\Core\Europe\CzechRepublic\CzechRepublicCitizenInformationExtractor::class, + 'EE' => \Reducktion\Socrates\Core\Europe\Estonia\EstoniaCitizenInformationExtractor::class, + 'FI' => \Reducktion\Socrates\Core\Europe\Finland\FinlandCitizenInformationExtractor::class, + 'FR' => \Reducktion\Socrates\Core\Europe\France\FranceCitizenInformationExtractor::class, + 'HR' => \Reducktion\Socrates\Core\Europe\Croatia\CroatiaCitizenInformationExtractor::class, + 'HU' => \Reducktion\Socrates\Core\Europe\Hungary\HungaryCitizenInformationExtractor::class, + 'IS' => \Reducktion\Socrates\Core\Europe\Iceland\IcelandCitizenInformationExtractor::class, + 'IT' => \Reducktion\Socrates\Core\Europe\Italy\ItalyCitizenInformationExtractor::class, + 'LT' => \Reducktion\Socrates\Core\Europe\Lithuania\LithuaniaCitizenInformationExtractor::class, + 'LV' => \Reducktion\Socrates\Core\Europe\Latvia\LatviaCitizenInformationExtractor::class, + 'ME' => \Reducktion\Socrates\Core\Europe\Montenegro\MontenegroCitizenInformationExtractor::class, + 'MK' => \Reducktion\Socrates\Core\Europe\NorthMacedonia\NorthMacedoniaCitizenInformationExtractor::class, + 'NO' => \Reducktion\Socrates\Core\Europe\Norway\NorwayCitizenInformationExtractor::class, + 'PL' => \Reducktion\Socrates\Core\Europe\Poland\PolandCitizenInformationExtractor::class, + 'RO' => \Reducktion\Socrates\Core\Europe\Romania\RomaniaCitizenInformationExtractor::class, + 'RS' => \Reducktion\Socrates\Core\Europe\Serbia\SerbiaCitizenInformationExtractor::class, + 'SE' => \Reducktion\Socrates\Core\Europe\Sweden\SwedenCitizenInformationExtractor::class, + 'SI' => \Reducktion\Socrates\Core\Europe\Slovenia\SloveniaCitizenInformationExtractor::class, + 'SK' => \Reducktion\Socrates\Core\Europe\Slovakia\SlovakiaCitizenInformationExtractor::class, + 'UA' => \Reducktion\Socrates\Core\Europe\Ukraine\UkraineCitizenInformationExtractor::class, + 'XK' => \Reducktion\Socrates\Core\Europe\Kosovo\KosovoCitizenInformationExtractor::class, + + /** + * Extractors for north american countries. + */ + 'MX' => \Reducktion\Socrates\Core\NorthAmerica\Mexico\MexicoCitizenInformationExtractor::class, ]; } diff --git a/src/Constants/Country.php b/src/Constants/Country.php new file mode 100644 index 0000000..27b6d91 --- /dev/null +++ b/src/Constants/Country.php @@ -0,0 +1,70 @@ +getMonthFromDigits($monthDigits, $this->getGender($id)); - return Carbon::createFromFormat('Y-m-d', "$year-$month-$day"); + return new DateTime("$year-$month-$day"); } private function getYearFromCode(string $yearCode): int diff --git a/src/Core/Albania/AlbaniaIdValidator.php b/src/Core/Europe/Albania/AlbaniaIdValidator.php similarity index 86% rename from src/Core/Albania/AlbaniaIdValidator.php rename to src/Core/Europe/Albania/AlbaniaIdValidator.php index 9c643ec..ff00d24 100644 --- a/src/Core/Albania/AlbaniaIdValidator.php +++ b/src/Core/Europe/Albania/AlbaniaIdValidator.php @@ -1,6 +1,6 @@ 'A', @@ -48,11 +48,11 @@ public function validate(string $id): bool 0 => 'W', ]; - $firstLetter = $id[0]; - $checkLetter = $id[9]; - $firstLetterNumber = array_search($firstLetter, $verificationTable); + $firstLetter = $idArray[0]; + $checkLetter = $idArray[9]; + $firstLetterNumber = array_search($firstLetter, $verificationTable, true); - $eightMiddleDigits = array_slice($id, 1, 8); + $eightMiddleDigits = array_slice($idArray, 1, 8); $sum = array_sum( array_map( diff --git a/src/Core/Belgium/BelgiumCitizenInformationExtractor.php b/src/Core/Europe/Belgium/BelgiumCitizenInformationExtractor.php similarity index 71% rename from src/Core/Belgium/BelgiumCitizenInformationExtractor.php rename to src/Core/Europe/Belgium/BelgiumCitizenInformationExtractor.php index 802daab..927b55d 100644 --- a/src/Core/Belgium/BelgiumCitizenInformationExtractor.php +++ b/src/Core/Europe/Belgium/BelgiumCitizenInformationExtractor.php @@ -1,13 +1,20 @@ isAfter2000($id) ? $year + 2000 : $year + 1900; - return Carbon::createFromFormat('Y-m-d', "$year-$month-$day"); + return new DateTime("$year-$month-$day"); } private function isAfter2000($id): bool diff --git a/src/Core/Belgium/BelgiumIdValidator.php b/src/Core/Europe/Belgium/BelgiumIdValidator.php similarity index 65% rename from src/Core/Belgium/BelgiumIdValidator.php rename to src/Core/Europe/Belgium/BelgiumIdValidator.php index 9dea5a1..87461a0 100644 --- a/src/Core/Belgium/BelgiumIdValidator.php +++ b/src/Core/Europe/Belgium/BelgiumIdValidator.php @@ -1,17 +1,28 @@ sanitize($id); + if (! $this->validateSequenceNumber($id)) { + return false; + } + $checksumFromId = (int) substr($id, -2); $after2000 = false; $checksum = $this->calculateChecksum($id, $after2000); @@ -33,7 +44,7 @@ public function validate(string $id): bool private function sanitize(string $id): string { - $id = str_replace(['-', '.'], '', $id); + $id = str_replace(['-', ' ', '.'], '', $id); $idLength = strlen($id); @@ -44,6 +55,13 @@ private function sanitize(string $id): string return $id; } + private function validateSequenceNumber(string $id): bool + { + $sequenceNumber = (int) substr($id, 6, 3); + + return $sequenceNumber !== 0 && $sequenceNumber !== 999; + } + private function calculateChecksum(string $id, bool $after2000): int { if ($after2000) { @@ -60,18 +78,21 @@ private function validDateOfBirth(string $id, bool $after2000): bool $dateDigits = substr($id, 0, 6); [$year, $month, $day] = str_split($dateDigits, 2); + $year = (int) $year; + $month = (int) $month; + $day = (int) $day; + + $month = $month === 0 ? 1 : $month; + $day = $day === 0 ? 1 : $day; + if ($month < 1 || $month > 12 || $day < 1 || $day > 31) { return false; } $year = $after2000 ? $year + 2000 : $year + 1900; - $dob = Carbon::createFromFormat('Y-m-d', "$year-$month-$day"); - - if ($dob->greaterThan($dob->subYears(12))) { - return false; - } + $dateOfBirth = new DateTime("$year-$month-$day"); - return true; + return (new DateTime())->diff($dateOfBirth)->y >= 12; } } diff --git a/src/Core/BosniaAndHerzegovina/BosniaAndHerzegovinaCitizenInformationExtractor.php b/src/Core/Europe/BosniaAndHerzegovina/BosniaAndHerzegovinaCitizenInformationExtractor.php similarity index 84% rename from src/Core/BosniaAndHerzegovina/BosniaAndHerzegovinaCitizenInformationExtractor.php rename to src/Core/Europe/BosniaAndHerzegovina/BosniaAndHerzegovinaCitizenInformationExtractor.php index fe03dca..3a5805c 100644 --- a/src/Core/BosniaAndHerzegovina/BosniaAndHerzegovinaCitizenInformationExtractor.php +++ b/src/Core/Europe/BosniaAndHerzegovina/BosniaAndHerzegovinaCitizenInformationExtractor.php @@ -1,12 +1,12 @@ 12 ? $month - 50 : $month; - return Carbon::createFromFormat('Y-m-d', "$year-$month-$day"); + return new DateTime("$year-$month-$day"); } } diff --git a/src/Core/Czechoslovakia/CzechoslovakiaIdValidator.php b/src/Core/Europe/Czechoslovakia/CzechoslovakiaIdValidator.php similarity index 90% rename from src/Core/Czechoslovakia/CzechoslovakiaIdValidator.php rename to src/Core/Europe/Czechoslovakia/CzechoslovakiaIdValidator.php index e5af59b..3970551 100644 --- a/src/Core/Czechoslovakia/CzechoslovakiaIdValidator.php +++ b/src/Core/Europe/Czechoslovakia/CzechoslovakiaIdValidator.php @@ -1,6 +1,6 @@ format('Y') - : Carbon::createFromFormat('y', (string) $twoDigitYear)->format('Y'); + ? DateTime::createFromFormat('Y', "19$twoDigitYear")->format('Y') + : DateTime::createFromFormat('y', (string) $twoDigitYear)->format('Y'); if (($seventhDigit === 4 || $seventhDigit === 9) && $year <= 1936) { $year += 100; @@ -56,6 +56,6 @@ private function getDateOfBirth(string $cpr): Carbon $year > 1957 ? $year -= 100 : $year += 100; } - return Carbon::createFromFormat('Y-m-d', "$year-$month-$day"); + return new DateTime("$year-$month-$day"); } } diff --git a/src/Core/Denmark/DenmarkIdValidator.php b/src/Core/Europe/Denmark/DenmarkIdValidator.php similarity index 95% rename from src/Core/Denmark/DenmarkIdValidator.php rename to src/Core/Europe/Denmark/DenmarkIdValidator.php index 68d125e..48aa328 100644 --- a/src/Core/Denmark/DenmarkIdValidator.php +++ b/src/Core/Europe/Denmark/DenmarkIdValidator.php @@ -1,6 +1,6 @@ format('y'); + $currentYear = (int) (new DateTime())->format('y'); + $year = $year > $currentYear ? $year + 1900 : $year + 2000; if ($month > 0 && $month < 13) { - return Carbon::createFromFormat('Y-m', "$year-$month"); + return DateTime::createFromFormat('Y-m', "$year-$month"); } if ($month > 30 && $month < 43) { $month -= 30; - return Carbon::createFromFormat('Y-m', "$year-$month"); + return DateTime::createFromFormat('Y-m', "$year-$month"); } - return Carbon::createFromFormat('Y', $year); + return DateTime::createFromFormat('Y', $year); } private function getPlaceOfBirth(string $id): string diff --git a/src/Core/France/FranceIdValidator.php b/src/Core/Europe/France/FranceIdValidator.php similarity index 94% rename from src/Core/France/FranceIdValidator.php rename to src/Core/Europe/France/FranceIdValidator.php index 27b5f92..5298474 100644 --- a/src/Core/France/FranceIdValidator.php +++ b/src/Core/Europe/France/FranceIdValidator.php @@ -1,6 +1,6 @@ 31 ? Gender::FEMALE : Gender::MALE; } - private function getDateOfBirth(string $id): Carbon + private function getDateOfBirth(string $id): DateTime { $dayDigits = substr($id, 9, 2); $monthChar = $id[8]; $yearDigits = substr($id, 6, 2); $months = 'ABCDEHLMPRST'; - $currentYear = (int) now()->format('y'); + $currentYear = (int) (new DateTime())->format('y'); $day = (int) $dayDigits > 31 ? (int) $dayDigits - 40 : (int) $dayDigits; $month = strpos($months, $monthChar) + 1; $year = (int) $yearDigits > $currentYear ? (int) $yearDigits + 1900 : (int) $yearDigits + 2000; - return Carbon::createFromFormat('Y-m-d', "$year-$month-$day"); + return new DateTime("$year-$month-$day"); } private function getPlaceOfBirth(string $id): string diff --git a/src/Core/Italy/ItalyIdValidator.php b/src/Core/Europe/Italy/ItalyIdValidator.php similarity index 96% rename from src/Core/Italy/ItalyIdValidator.php rename to src/Core/Europe/Italy/ItalyIdValidator.php index 6e28a6a..4861503 100644 --- a/src/Core/Italy/ItalyIdValidator.php +++ b/src/Core/Europe/Italy/ItalyIdValidator.php @@ -1,6 +1,6 @@ 1) { $tenDigitId .= $id[$i]; } @@ -61,24 +62,24 @@ private function convertToTenDigitId(string $id): string private function getGender(string $id): string { - return $id[8] % 2 == 0 ? Gender::FEMALE : Gender::MALE; + return $id[8] % 2 === 0 ? Gender::FEMALE : Gender::MALE; } - private function getDateOfBirth(string $id, bool $isOverOneHundredYearsOld): Carbon + private function getDateOfBirth(string $id, bool $isOverOneHundredYearsOld): DateTime { $dateDigits = substr($id, 0, 6); [$twoDigitYear, $month, $day] = str_split($dateDigits, 2); if ($isOverOneHundredYearsOld) { - $year = Carbon::createFromFormat('Y', "19$twoDigitYear")->format('Y'); + $year = DateTime::createFromFormat('Y', "19$twoDigitYear")->format('Y'); } else { - $presentTwoDigitYear = (int) now()->format('y'); + $presentTwoDigitYear = (int) (new DateTime())->format('y'); $year = $twoDigitYear < $presentTwoDigitYear - ? Carbon::createFromFormat('y', (string) $twoDigitYear)->format('Y') - : Carbon::createFromFormat('Y', "19$twoDigitYear")->format('Y'); + ? DateTime::createFromFormat('y', (string) $twoDigitYear)->format('Y') + : DateTime::createFromFormat('Y', "19$twoDigitYear")->format('Y'); } - return Carbon::createFromFormat('Y-m-d', "$year-$month-$day"); + return new DateTime("$year-$month-$day"); } } diff --git a/src/Core/Sweden/SwedenIdValidator.php b/src/Core/Europe/Sweden/SwedenIdValidator.php similarity index 93% rename from src/Core/Sweden/SwedenIdValidator.php rename to src/Core/Europe/Sweden/SwedenIdValidator.php index 11c1b5a..df2df0a 100644 --- a/src/Core/Sweden/SwedenIdValidator.php +++ b/src/Core/Europe/Sweden/SwedenIdValidator.php @@ -1,6 +1,6 @@ 1) { $tenDigitId .= $id[$i]; } diff --git a/src/Core/Switzerland/SwitzerlandIdValidator.php b/src/Core/Europe/Switzerland/SwitzerlandIdValidator.php similarity index 95% rename from src/Core/Switzerland/SwitzerlandIdValidator.php rename to src/Core/Europe/Switzerland/SwitzerlandIdValidator.php index 25a208b..5a03c57 100644 --- a/src/Core/Switzerland/SwitzerlandIdValidator.php +++ b/src/Core/Europe/Switzerland/SwitzerlandIdValidator.php @@ -1,6 +1,6 @@ addDays($birthDateDigits); + $date = new DateTime('1900-01-01'); + $date->add(new DateInterval("P{$birthDateDigits}D")); + + return $date; } } diff --git a/src/Core/Ukraine/UkraineIdValidator.php b/src/Core/Europe/Ukraine/UkraineIdValidator.php similarity index 95% rename from src/Core/Ukraine/UkraineIdValidator.php rename to src/Core/Europe/Ukraine/UkraineIdValidator.php index 6229606..e2b3359 100644 --- a/src/Core/Ukraine/UkraineIdValidator.php +++ b/src/Core/Europe/Ukraine/UkraineIdValidator.php @@ -1,6 +1,6 @@ year; + $currentYear = (int) (new DateTime())->format('Y'); if ($year + 1000 < $currentYear && $year + 1000 > 1900) { $year += 1000; @@ -50,7 +47,7 @@ private static function getDateOfBirth(string $id): Carbon $year += 2000; } - return Carbon::createFromFormat('Y-m-d', "$year-$month-$day"); + return new DateTime("$year-$month-$day"); } private static function getPlaceOfBirth(string $id): string diff --git a/src/Core/Yugoslavia/YugoslaviaIdValidator.php b/src/Core/Europe/Yugoslavia/YugoslaviaIdValidator.php similarity index 93% rename from src/Core/Yugoslavia/YugoslaviaIdValidator.php rename to src/Core/Europe/Yugoslavia/YugoslaviaIdValidator.php index e6f5eaf..ec20cde 100644 --- a/src/Core/Yugoslavia/YugoslaviaIdValidator.php +++ b/src/Core/Europe/Yugoslavia/YugoslaviaIdValidator.php @@ -1,6 +1,6 @@ 'foreigner in Bosnia and Herzegovina', '02' => 'foreigners in Montenegro', '03' => 'foreigners in Croatia', - '04' => ' foreigners in North Macedonia', + '04' => 'foreigners in North Macedonia', '05' => 'foreigners in Slovenia', '06' => 'foreigners in Central Serbia', '07' => 'foreigners in Serbian province of Vojvodina', diff --git a/src/Core/NorthAmerica/Canada/CanadaIdValidator.php b/src/Core/NorthAmerica/Canada/CanadaIdValidator.php new file mode 100644 index 0000000..b040430 --- /dev/null +++ b/src/Core/NorthAmerica/Canada/CanadaIdValidator.php @@ -0,0 +1,47 @@ +sanitize($id); + + $idArray = str_split($id); + $parity = count($idArray) % 2; + $sum = 0; + foreach ($idArray as $key => $value) { + if ($key % 2 === $parity) { + $value *= 2; + } + + $sum += ($value >= 10 ? $value - 9 : $value); + } + + return $sum % 10 === 0; + } + + public function sanitize(string $id): string + { + $id = str_replace(' ', '', $id); + + $idLength = strlen($id); + + if ($idLength !== 9) { + throw new InvalidLengthException('Canadian SIN', '9', $idLength); + } + + return $id; + } +} diff --git a/src/Core/NorthAmerica/Mexico/MexicoCitizenInformationExtractor.php b/src/Core/NorthAmerica/Mexico/MexicoCitizenInformationExtractor.php new file mode 100644 index 0000000..b0daca0 --- /dev/null +++ b/src/Core/NorthAmerica/Mexico/MexicoCitizenInformationExtractor.php @@ -0,0 +1,50 @@ +validate($id)) { + throw new InvalidIdException(); + } + + $gender = $this->getGender($id); + $dob = $this->getDateOfBirth($id); + + $citizen = new Citizen(); + $citizen->setGender($gender); + $citizen->setDateOfBirth($dob); + + return $citizen; + } + + private function getGender(string $id): string + { + $identifier = $id[10]; + + if ($identifier === 'H') { + return Gender::MALE; + } + + return Gender::FEMALE; + } + + private function getDateOfBirth(string $id): DateTime + { + $year = (int) substr($id, 4, 2); + $month = (int) substr($id, 6, 2); + $day = (int) substr($id, 8, 2); + + $year += is_numeric($id[16]) ? 1900 : 2000; + + return new DateTime("$year-$month-$day"); + } +} diff --git a/src/Core/NorthAmerica/Mexico/MexicoIdValidator.php b/src/Core/NorthAmerica/Mexico/MexicoIdValidator.php new file mode 100644 index 0000000..c09c032 --- /dev/null +++ b/src/Core/NorthAmerica/Mexico/MexicoIdValidator.php @@ -0,0 +1,158 @@ +sanitize($id); + + if (!$this->validateRegex($id)) { + return false; + } + + if (!$this->validateNames(substr($id, 0, 3))) { + return false; + } + + if (!$this->validateBirthdate(substr($id, 4, 6))) { + return false; + } + + if (!$this->validateGender($id[10])) { + return false; + } + + if (!$this->validateState(substr($id, 11, 2))) { + return false; + } + + if (!$this->validateConsonants(substr($id, 13, 3))) { + return false; + } + + return $this->validateChecksum($id); + } + + private function sanitize(string $id): string + { + $idLength = strlen($id); + + if ($idLength !== 18) { + throw new InvalidLengthException('Mexico CURP', 18, $idLength); + } + + return strtoupper($id); + } + + private function validateRegex(string $id): bool + { + return preg_match('/^[A-Z]{4}\d{6}[A-Z]{6}[0-9A-Z]\d$/', $id, $matches); + } + + private function validateNames(string $names): bool + { + $length = strlen($names); + for ($i = 0; $i < $length; $i++) { + if (!ctype_alpha($names[$i])) { + return false; + } + + if (!in_array($names[1], self::VOWELS)) { + return false; + } + } + + if (in_array($names, self::BLACKLISTED_NAMES)) { + return false; + } + + return true; + } + + private function validateBirthdate(string $birthdate): bool + { + $length = strlen($birthdate); + for ($i = 0; $i < $length; $i++) { + if (!is_numeric($birthdate[$i])) { + return false; + } + } + + if (!in_array((int)substr($birthdate, 2, 2), range(1, 12), true)) { + return false; + } + + if (!in_array((int)substr($birthdate, 4, 2), range(1, 31), true)) { + return false; + } + + return true; + } + + private function validateGender(string $gender): bool + { + return !(!ctype_alpha($gender) || !in_array($gender, self::GENDERS)); + } + + private function validateState(string $state): bool + { + return !(!array_key_exists($state, MexicoStatesList::$states) || !ctype_alpha($state)); + } + + private function validateConsonants(string $names): bool + { + if (!ctype_alpha($names)) { + return false; + } + + $length = strlen($names); + for ($i = 0; $i < $length; $i++) { + if (in_array($names[$i], self::VOWELS)) { + return false; + } + } + + return true; + } + + private function validateChecksum(string $id): bool + { + $alphabet = "0123456789ABCDEFGHIJKLMN&OPQRSTUVWXYZ"; + + if (!is_numeric($id[17])) { + return false; + } + + $code = substr($id, 0, 17); + $check = 0; + + $length = strlen($code); + for ($i = 0; $i < $length; $i++) { + $check += array_search($code[$i], str_split($alphabet), true) * (18 - $i); + } + return (10 - $check % 10) % 10 === (int) $id[17]; + } +} diff --git a/src/Core/NorthAmerica/Mexico/MexicoStatesList.php b/src/Core/NorthAmerica/Mexico/MexicoStatesList.php new file mode 100644 index 0000000..fa75982 --- /dev/null +++ b/src/Core/NorthAmerica/Mexico/MexicoStatesList.php @@ -0,0 +1,49 @@ + "AGUASCALIENTES", + 'BS' => "BAJA CALIFORNIA SUR", + 'CL' => "COAHUILA", + 'CS' => "CHIAPAS", + 'DF' => "DISTRITO FEDERAL", + 'GT' => "GUANAJUATO", + 'HG' => "HIDALGO", + 'MC' => "MรXICO", + 'MS' => "MORELOS", + 'NL' => "NUEVO LEรN", + 'PL' => "PUEBLA", + 'QR' => "QUINTANA ROO", + 'SL' => "SINALOA", + 'TC' => "TABASCO", + 'TL' => "TLAXCALA", + 'YN' => "YUCATรN", + 'NE' => "NACIDO EN EL EXTRANJERO", + 'BC' => "BAJA CALIFORNIA", + 'CC' => "CAMPECHE", + 'CM' => "COLIMA", + 'CH' => "CHIHUAHUA", + 'DG' => "DURANGO", + 'GR' => "GUERRERO", + 'JC' => "JALISCO", + 'MN' => "MICHOACรN", + 'NT' => "NAYARIT", + 'OC' => "OAXACA", + 'QT' => "QUERรTARO", + 'SP' => "SAN LUIS POTOSร", + 'SR' => "SONORA", + 'TS' => "TAMAULIPAS", + 'VZ' => "VERACRUZ", + 'ZS' => "ZACATECAS" + ]; +} diff --git a/src/Core/NorthAmerica/UnitedStates/UnitedStatesIdValidator.php b/src/Core/NorthAmerica/UnitedStates/UnitedStatesIdValidator.php new file mode 100644 index 0000000..78309a4 --- /dev/null +++ b/src/Core/NorthAmerica/UnitedStates/UnitedStatesIdValidator.php @@ -0,0 +1,58 @@ +sanitize($id); + + if (in_array($id, $this->blacklist, true)) { + return false; + } + + $areaNumber = (int) substr($id, 0, 3); + if ($areaNumber === 0 || $areaNumber === 666 || $areaNumber > 899) { + return false; + } + + $groupNumber = (int) substr($id, 3, 2); + if ($groupNumber === 0) { + return false; + } + + $serialNumber = (int) substr($id, 5, 4); + + return !($serialNumber === 0); + } + + private function sanitize(string $id): string + { + $id = str_replace('-', '', $id); + + $idLength = strlen($id); + + if ($idLength !== 9) { + throw new InvalidLengthException('American SSN', '9', $idLength); + } + + return $id; + } +} diff --git a/src/Core/SouthAmerica/Argentina/ArgentinaIdValidator.php b/src/Core/SouthAmerica/Argentina/ArgentinaIdValidator.php new file mode 100644 index 0000000..bad835b --- /dev/null +++ b/src/Core/SouthAmerica/Argentina/ArgentinaIdValidator.php @@ -0,0 +1,44 @@ +sanitize($id); + + $sum = 0; + for ($i = 0; $i <= 9; ++$i) { + $sum += $id[9 - $i] * (2 + ($i % 6)); + } + + $checksum = 11 - ($sum % 11); + $checksum = $checksum === 11 ? 0 : $checksum; + + return (string)$checksum === $id[-1]; + } + + private function sanitize(string $id): string + { + $id = preg_replace('/[\D]/', '', $id); + + $idLength = strlen($id); + + if ($idLength !== 11) { + throw new InvalidLengthException('Argentina CUIL/CUIT', '11', $idLength); + } + + return $id; + } +} diff --git a/src/Core/Brazil/BrazilIdValidator.php b/src/Core/SouthAmerica/Brazil/BrazilIdValidator.php similarity index 77% rename from src/Core/Brazil/BrazilIdValidator.php rename to src/Core/SouthAmerica/Brazil/BrazilIdValidator.php index b68c81a..f746944 100644 --- a/src/Core/Brazil/BrazilIdValidator.php +++ b/src/Core/SouthAmerica/Brazil/BrazilIdValidator.php @@ -1,10 +1,17 @@ sanitize($id); + + $lastDigit = $id[-1]; + + $id = substr($id, 0, -1); + + $sum = 1; + for ($m = 0; $id != 0; $id /= 10) { + $sum = ($sum + $id % 10 * (9 - $m++ % 6)) % 11; + } + + $checksum = chr($sum ? $sum + 47 : 75); + + return $checksum === $lastDigit; + } + + private function sanitize(string $id): string + { + $lastDigitCharIsK = substr($id, -1) === 'K'; + $id = preg_replace('/[\D]/', '', $id); + + if (is_string($id)) { + $id .= $lastDigitCharIsK ? 'K' : ''; + } + + $idLength = strlen($id); + + //The RUT number's format is: 7 or 8 digits, one dash, 1 check digit (0-9, K) + if ($idLength < 8 || $idLength > 9) { + throw new InvalidLengthException('Chile RUT', '8', $idLength); + } + + return $id; + } +} diff --git a/src/Core/SouthAmerica/Ecuador/EcuadorIdValidator.php b/src/Core/SouthAmerica/Ecuador/EcuadorIdValidator.php new file mode 100644 index 0000000..d9715fb --- /dev/null +++ b/src/Core/SouthAmerica/Ecuador/EcuadorIdValidator.php @@ -0,0 +1,80 @@ +sanitize($id); + + $provinceCode = (int)substr($id, 0, 2); + $thirdDigit = (int)$id[2]; + $sequenceNumber = substr($id, 0, 9); + $lastDigit = (int)$id[-1]; + + if ($provinceCode < 0 || $provinceCode > 24) { + return false; + } + + if ($thirdDigit < 0 || $thirdDigit > 5) { + return false; + } + + return $this->validateCheckDigit($sequenceNumber, $lastDigit); + } + + private function sanitize(string $id): string + { + $id = preg_replace('/[\D]/', '', $id); + + $idLength = strlen($id); + + if ($idLength !== 10) { + throw new InvalidLengthException('Ecuador CI', '10', $idLength); + } + + return $id; + } + + protected function validateCheckDigit(string $sequence, int $lastDigit): bool + { + $coefficients = [2, 1, 2, 1, 2, 1, 2, 1, 2]; + + $sequence = array_map( + static function ($digit) { + return (int)$digit; + }, + str_split($sequence) + ); + + $sum = 0; + foreach ($sequence as $key => $value) { + $posValue = $value * $coefficients[$key]; + + if ($posValue >= 10) { + $posValue = (int)array_sum(str_split($posValue)); + } + + $sum += $posValue; + } + + $remainder = $sum % 10; + $result = $remainder !== 0 ? 10 - $remainder : 0; + + return $result === $lastDigit; + } +} diff --git a/src/Core/SouthAmerica/Peru/PeruIdValidator.php b/src/Core/SouthAmerica/Peru/PeruIdValidator.php new file mode 100644 index 0000000..c80bdd7 --- /dev/null +++ b/src/Core/SouthAmerica/Peru/PeruIdValidator.php @@ -0,0 +1,70 @@ +sanitize($id); + + $sum = 0; + $factors = [3, 2, 7, 6, 5, 4, 3, 2]; + $numberKeys = [6, 7, 8, 9, 0, 1, 1, 2, 3, 4, 5]; + $charKeys = ['K', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']; + + $dniDigits = substr($id, 0, -1); + + if (preg_match('/[\D]/', $dniDigits)) { + return false; + } + + $dniDigits = array_map( + static function ($digit) { + return (int)$digit; + }, + str_split($dniDigits) + ); + + $lastDigit = substr($id, -1); + + $countDniDigits = count($dniDigits); + + for ($i = 0; $i < $countDniDigits; $i++) { + $sum += $dniDigits[$i] * $factors[$i]; + } + + $key = 11 - ($sum % 11); + $key = $key === 11 ? 0 : $key; + + return is_numeric($lastDigit) + ? $numberKeys[$key] === (int)$lastDigit + : $charKeys[$key] === strtoupper($lastDigit); + } + + private function sanitize(string $id): string + { + $id = preg_replace('/[\W]/', '', $id); + + $idLength = strlen($id); + + if ($idLength !== 9) { + throw new InvalidLengthException('Peru CUI', '9', $idLength); + } + + return $id; + } +} diff --git a/src/Core/SouthAmerica/Uruguay/UruguayIdValidator.php b/src/Core/SouthAmerica/Uruguay/UruguayIdValidator.php new file mode 100644 index 0000000..47e673c --- /dev/null +++ b/src/Core/SouthAmerica/Uruguay/UruguayIdValidator.php @@ -0,0 +1,50 @@ +sanitize($id); + $lastDigit = (int)$id[-1]; + $id = str_pad($id, 7, '0', STR_PAD_LEFT); + + $a = 0; + + $baseNumber = "2987634"; + for ($i = 0; $i < 7; $i++) { + $baseDigit = (int)$baseNumber[$i]; + $ciDigit = (int)$id[$i]; + + $a += ($baseDigit * $ciDigit) % 10; + } + + $validationDigit = $a % 10 === 0 ? 0 : 10 - $a % 10; + + return $lastDigit === $validationDigit; + } + + private function sanitize(string $id): string + { + $id = preg_replace('/[\D]/', '', $id); + + $idLength = strlen($id); + + if ($idLength !== 8) { + throw new InvalidLengthException('Uruguay CI', '8', $idLength); + } + + return $id; + } +} diff --git a/src/Exceptions/InvalidLengthException.php b/src/Exceptions/InvalidLengthException.php index ae4e298..0bc9a50 100644 --- a/src/Exceptions/InvalidLengthException.php +++ b/src/Exceptions/InvalidLengthException.php @@ -4,13 +4,6 @@ class InvalidLengthException extends \LogicException { - /** - * The name of the National Identification Number. - * - * @var string - */ - private $designation; - /** * Description of the number of characters the National Identification Number should have. * @@ -28,15 +21,12 @@ class InvalidLengthException extends \LogicException /** * Create a new InvalidLengthException instance. * - * @param string $designation - * @param string $requiredCharacters - * @param string $givenCharacters - * @return void + * @param string $designation The National Identification Number designation + * @param string $requiredCharacters The correct number of characters as string + * @param string $givenCharacters The characters that the user has given us */ public function __construct(string $designation, string $requiredCharacters, string $givenCharacters) { - $this->designation = $designation; - $this->requiredCharacters = $requiredCharacters; $this->givenCharacters = $givenCharacters; diff --git a/src/Models/Citizen.php b/src/Models/Citizen.php index d7224ee..0b1ae29 100644 --- a/src/Models/Citizen.php +++ b/src/Models/Citizen.php @@ -2,6 +2,7 @@ namespace Reducktion\Socrates\Models; +use DateTime; use Carbon\Carbon; use Reducktion\Socrates\Exceptions\UnsupportedOperationException; @@ -15,9 +16,9 @@ class Citizen private $gender; /** - * The date of birth as a Carbon instance. + * The date of birth as a DateTime object. * - * @var Carbon|null + * @var DateTime|null */ private $dateOfBirth; @@ -29,7 +30,7 @@ class Citizen private $placeOfBirth; /** - * Get the gender. + * Get the citizen's gender as a string. * * @return string|null */ @@ -39,17 +40,29 @@ public function getGender(): ?string } /** - * Get the date of birth. + * Get the citizen's date of birth as a Carbon instance. * + * @deprecated v1.3.0 This method will be deprecated in the next release of Socrates. + * Please use getDateOfBirthNative() instead. * @return Carbon|null */ public function getDateOfBirth(): ?Carbon + { + return Carbon::instance($this->dateOfBirth); + } + + /** + * Get the citizen's date of birth as a DateTime object. + * + * @return DateTime|null + */ + public function getDateOfBirthNative(): ?DateTime { return $this->dateOfBirth; } /** - * Get the age. + * Get the citizen's age as a number or null. * * @return int|null */ @@ -59,11 +72,11 @@ public function getAge(): ?int throw new UnsupportedOperationException('Citizen date of birth is null.'); } - return $this->dateOfBirth->age; + return (new DateTime())->diff($this->dateOfBirth)->y; } /** - * Get the place of birth. + * Get the citizen's place of birth as a string or null. * * @return string|null */ @@ -73,7 +86,7 @@ public function getPlaceOfBirth(): ?string } /** - * Set the gender. + * Set the citizen's gender. * * @param string $gender * @return void @@ -84,18 +97,18 @@ public function setGender(string $gender): void } /** - * Set the date of birth. + * Set the citizen's date of birth. * - * @param Carbon $dateOfBirth + * @param DateTime $dateOfBirth * @return void */ - public function setDateOfBirth(Carbon $dateOfBirth): void + public function setDateOfBirth(DateTime $dateOfBirth): void { $this->dateOfBirth = $dateOfBirth; } /** - * Set the place of birth. + * Set the citizen's place of birth. * * @param string $placeOfBirth * @return void diff --git a/src/Socrates.php b/src/Socrates.php index 339c018..3a8d89e 100644 --- a/src/Socrates.php +++ b/src/Socrates.php @@ -69,16 +69,10 @@ public function validateId(string $id, string $countryCode = ''): bool */ private function formatCountryCode(string $countryCode): string { - $runningLaravel = class_exists(\Illuminate\Support\Facades\App::class); - - if (! $runningLaravel && $countryCode === '') { + if ($countryCode === '') { throw new InvalidCountryCodeException('No country code provided.'); } - if ($runningLaravel && $countryCode === '') { - $countryCode = \Illuminate\Support\Facades\App::getLocale(); - } - $countryCodeLength = strlen($countryCode); if ($countryCodeLength !== 2 && $countryCodeLength !== 5) { diff --git a/tests/CitizenInformationExtractorFactoryTest.php b/tests/CitizenInformationExtractorFactoryTest.php new file mode 100644 index 0000000..1df4cca --- /dev/null +++ b/tests/CitizenInformationExtractorFactoryTest.php @@ -0,0 +1,24 @@ +assertInstanceOf(CitizenInformationExtractor::class, $extractor); + } + + /** @test */ + public function it_throws_for_unknown_country(): void + { + $this->expectException(RuntimeException::class); + CitizenInformationExtractorFactory::getExtractor('XX'); + } +} diff --git a/tests/Feature/AlbaniaTest.php b/tests/Feature/Europe/AlbaniaTest.php similarity index 63% rename from tests/Feature/AlbaniaTest.php rename to tests/Feature/Europe/AlbaniaTest.php index ab3ed75..57ae9d3 100644 --- a/tests/Feature/AlbaniaTest.php +++ b/tests/Feature/Europe/AlbaniaTest.php @@ -1,11 +1,13 @@ [ 'nid' => 'I05101999I', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1980-01-01'), - 'age' => Carbon::createFromFormat('Y-m-d', '1980-01-01')->age, + 'dob' => new DateTime('1980-01-01'), + 'age' => $this->calculateAge(new DateTime('1980-01-01')), ], 'shufti' => [ 'nid' => 'I90201535E', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1989-02-01'), - 'age' => Carbon::createFromFormat('Y-m-d', '1989-02-01')->age, + 'dob' => new DateTime('1989-02-01'), + 'age' => $this->calculateAge(new DateTime('1989-02-01')), ], 'shyqe' => [ 'nid' => 'J45423004V', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1994-04-23'), - 'age' => Carbon::createFromFormat('Y-m-d', '1994-04-23')->age, + 'dob' => new DateTime('1994-04-23'), + 'age' => $this->calculateAge(new DateTime('1994-04-23')), ], 'elseid' => [ 'nid' => 'H71211672R', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1977-12-11'), - 'age' => Carbon::createFromFormat('Y-m-d', '1977-12-11')->age, + 'dob' => new DateTime('1977-12-11'), + 'age' => $this->calculateAge(new DateTime('1977-12-11')), ], 'hasna' => [ 'nid' => 'I85413200A', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1988-04-13'), - 'age' => Carbon::createFromFormat('Y-m-d', '1988-04-13')->age, + 'dob' => new DateTime('1988-04-13'), + 'age' => $this->calculateAge(new DateTime('1988-04-13')), ], ]; @@ -62,10 +64,10 @@ public function test_extract_behaviour(): void { foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['nid'], 'AL'); - - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -76,13 +78,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['nid'], 'AL') ); } foreach ($this->invalidIds as $invalidId) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($invalidId, 'AL') ); } diff --git a/tests/Feature/BelgiumTest.php b/tests/Feature/Europe/BelgiumTest.php similarity index 55% rename from tests/Feature/BelgiumTest.php rename to tests/Feature/Europe/BelgiumTest.php index e78bb14..2b8482e 100644 --- a/tests/Feature/BelgiumTest.php +++ b/tests/Feature/Europe/BelgiumTest.php @@ -1,16 +1,17 @@ [ 'id' => '93.05.18-223.61', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1993-05-18'), - 'age' => Carbon::createFromFormat('Y-m-d', '1993-05-18')->age, + 'dob' => new DateTime('1993-05-18'), + 'age' => $this->calculateAge(new DateTime('1993-05-18')), ], 'naoual' => [ 'id' => '730111-361-73', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1973-01-11'), - 'age' => Carbon::createFromFormat('Y-m-d', '1973-01-11')->age, + 'dob' => new DateTime('1973-01-11'), + 'age' => $this->calculateAge(new DateTime('1973-01-11')), ], 'xavi' => [ 'id' => '75.12.05-137.14', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1975-12-05'), - 'age' => Carbon::createFromFormat('Y-m-d', '1975-12-05')->age, - ], - 'ute' => [ - 'id' => '09.08.24-282.48', - 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2009-08-24'), - 'age' => Carbon::createFromFormat('Y-m-d', '2009-08-24')->age, + 'dob' => new DateTime('1975-12-05'), + 'age' => $this->calculateAge(new DateTime('1975-12-05')), ], 'kurt' => [ 'id' => '71.09.07-213.64', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1971-09-07'), - 'age' => Carbon::createFromFormat('Y-m-d', '1971-09-07')->age, + 'dob' => new DateTime('1971-09-07'), + 'age' => $this->calculateAge(new DateTime('1971-09-07')), ], + 'mark' => [ + 'id' => '40.00.01-001.33', + 'gender' => Gender::MALE, + 'dob' => new DateTime('1940-01-01'), + 'age' => $this->calculateAge(new DateTime('1940-01-01')), + ] ]; $this->invalidIds = [ @@ -55,7 +56,10 @@ public function setUp(): void '97.12.03-123.12', '01.06.18-468.99', '64.04.09-874.43', - '12.10.23-954.11' + '12.10.23-954.11', + '09.08.24-282.48', // invalid age + '01.11.16-000.06', // invalid sequence number + '01.11.16-999.74' // invalid sequence number ]; } @@ -63,9 +67,10 @@ public function test_extract_behaviour(): void { foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['id'], 'BE'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -76,13 +81,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['id'], 'BE') ); } foreach ($this->invalidIds as $id) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($id, 'BE') ); } diff --git a/tests/Feature/BosniaAndHerzegovinaTest.php b/tests/Feature/Europe/BosniaAndHerzegovinaTest.php similarity index 65% rename from tests/Feature/BosniaAndHerzegovinaTest.php rename to tests/Feature/Europe/BosniaAndHerzegovinaTest.php index 8338e1f..6342750 100644 --- a/tests/Feature/BosniaAndHerzegovinaTest.php +++ b/tests/Feature/Europe/BosniaAndHerzegovinaTest.php @@ -1,11 +1,13 @@ [ 'jmbg' => '1502957172694', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1957-02-15'), - 'age' => Carbon::createFromFormat('Y-m-d', '1957-02-15')->age, + 'dob' => new DateTime('1957-02-15'), + 'age' => $this->calculateAge(new DateTime('1957-02-15')), 'pob' => 'Sarajevo - Bosnia and Herzegovina' ], 'Imran' => [ 'jmbg' => '2508995191483', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1995-08-25'), - 'age' => Carbon::createFromFormat('Y-m-d', '1995-08-25')->age, + 'dob' => new DateTime('1995-08-25'), + 'age' => $this->calculateAge(new DateTime('1995-08-25')), 'pob' => 'Zenica - Bosnia and Herzegovina' ], 'Ajdin' => [ 'jmbg' => '1012980163603', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1980-12-10'), - 'age' => Carbon::createFromFormat('Y-m-d', '1980-12-10')->age, + 'dob' => new DateTime('1980-12-10'), + 'age' => $this->calculateAge(new DateTime('1980-12-10')), 'pob' => 'Prijedor - Bosnia and Herzegovina' ], 'Merjem' => [ 'jmbg' => '1310963145538', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1963-10-13'), - 'age' => Carbon::createFromFormat('Y-m-d', '1963-10-13')->age, + 'dob' => new DateTime('1963-10-13'), + 'age' => $this->calculateAge(new DateTime('1963-10-13')), 'pob' => 'Livno - Bosnia and Herzegovina' ], 'Eman' => [ 'jmbg' => '1806998154160', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1998-06-18'), - 'age' => Carbon::createFromFormat('Y-m-d', '1998-06-18')->age, + 'dob' => new DateTime('1998-06-18'), + 'age' => $this->calculateAge(new DateTime('1998-06-18')), 'pob' => 'Mostar - Bosnia and Herzegovina' ] ]; @@ -68,10 +70,11 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['jmbg'], 'BA'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); - $this->assertEquals($person['pob'], $citizen->getPlaceOfBirth()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['pob'], $citizen->getPlaceOfBirth()); } $this->expectException(InvalidLengthException::class); @@ -82,13 +85,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['jmbg'], 'BA') ); } foreach ($this->invalidIds as $jmbg) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($jmbg, 'BA') ); } diff --git a/tests/Feature/BulgariaTest.php b/tests/Feature/Europe/BulgariaTest.php similarity index 61% rename from tests/Feature/BulgariaTest.php rename to tests/Feature/Europe/BulgariaTest.php index c0b611a..345a207 100644 --- a/tests/Feature/BulgariaTest.php +++ b/tests/Feature/Europe/BulgariaTest.php @@ -1,11 +1,13 @@ [ 'egn' => '7523169263', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1875-03-16'), - 'age' => Carbon::createFromFormat('Y-m-d', '1875-03-16')->age, + 'dob' => new DateTime('1875-03-16'), + 'age' => $this->calculateAge(new DateTime('1875-03-16')), ], 'Lyuben' => [ 'egn' => '8032056031', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1880-12-05'), - 'age' => Carbon::createFromFormat('Y-m-d', '1880-12-05')->age, + 'dob' => new DateTime('1880-12-05'), + 'age' => $this->calculateAge(new DateTime('1880-12-05')), ], 'Bilyana' => [ 'egn' => '8001010008', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1980-01-01'), - 'age' => Carbon::createFromFormat('Y-m-d', '1980-01-01')->age, + 'dob' => new DateTime('1980-01-01'), + 'age' => $this->calculateAge(new DateTime('1980-01-01')), ], 'Kalina' => [ 'egn' => '7501020018', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1975-01-02'), - 'age' => Carbon::createFromFormat('Y-m-d', '1975-01-02')->age, + 'dob' => new DateTime('1975-01-02'), + 'age' => $this->calculateAge(new DateTime('1975-01-02')), ], 'Nedyalko' => [ 'egn' => '7552010005', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2075-12-01'), - 'age' => Carbon::createFromFormat('Y-m-d', '2075-12-01')->age, + 'dob' => new DateTime('2075-12-01'), + 'age' => $this->calculateAge(new DateTime('2075-12-01')), ], 'Tsveta' => [ 'egn' => '7542011030', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2075-02-01'), - 'age' => Carbon::createFromFormat('Y-m-d', '2075-02-01')->age, + 'dob' => new DateTime('2075-02-01'), + 'age' => $this->calculateAge(new DateTime('2075-02-01')), ] ]; @@ -68,9 +70,10 @@ public function test_extract_behaviour(): void { foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['egn'], 'BG'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -81,13 +84,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['egn'], 'BG') ); } foreach ($this->invalidIds as $egn) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($egn, 'BG') ); } diff --git a/tests/Feature/CroatiaTest.php b/tests/Feature/Europe/CroatiaTest.php similarity index 69% rename from tests/Feature/CroatiaTest.php rename to tests/Feature/Europe/CroatiaTest.php index 53207bd..8a4c777 100644 --- a/tests/Feature/CroatiaTest.php +++ b/tests/Feature/Europe/CroatiaTest.php @@ -1,16 +1,18 @@ [ 'jmbg' => '1809988305313', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1988-09-18'), - 'age' => Carbon::createFromFormat('Y-m-d', '1988-09-18')->age, + 'dob' => new DateTime('1988-09-18'), + 'age' => $this->calculateAge(new DateTime('1988-09-18')), 'pob' => 'Osijek, Slavonia region - Croatia' ], 'Ana' => [ 'jmbg' => '0808928315425', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1928-08-08'), - 'age' => Carbon::createFromFormat('Y-m-d', '1928-08-08')->age, + 'dob' => new DateTime('1928-08-08'), + 'age' => $this->calculateAge(new DateTime('1928-08-08')), 'pob' => 'Bjelovar, Virovitica, Koprivnica, Pakrac, Podravina region - Croatia' ], 'Marija' => [ 'jmbg' => '1106961359224', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1961-06-11'), - 'age' => Carbon::createFromFormat('Y-m-d', '1961-06-11')->age, + 'dob' => new DateTime('1961-06-11'), + 'age' => $this->calculateAge(new DateTime('1961-06-11')), 'pob' => 'Gospiฤ, Lika region - Croatia' ], 'Stjepan' => [ 'jmbg' => '1105951323209', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1951-05-11'), - 'age' => Carbon::createFromFormat('Y-m-d', '1951-05-11')->age, + 'dob' => new DateTime('1951-05-11'), + 'age' => $this->calculateAge(new DateTime('1951-05-11')), 'pob' => 'Varaลพdin, Meฤimurje region - Croatia' ], 'Ivan' => [ 'jmbg' => '2109971352638', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1971-09-21'), - 'age' => Carbon::createFromFormat('Y-m-d', '1971-09-21')->age, + 'dob' => new DateTime('1971-09-21'), + 'age' => $this->calculateAge(new DateTime('1971-09-21')), 'pob' => 'Gospiฤ, Lika region - Croatia' ], ]; @@ -77,10 +79,11 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['jmbg'], 'HR'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); - $this->assertEquals($person['pob'], $citizen->getPlaceOfBirth()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['pob'], $citizen->getPlaceOfBirth()); } $this->expectException(InvalidLengthException::class); @@ -95,19 +98,19 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->validIds as $oib) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($oib, 'HR') ); } foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['jmbg'], 'HR') ); } foreach ($this->invalidIds as $jmbg) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($jmbg, 'HR') ); } diff --git a/tests/Feature/CzechRepublicTest.php b/tests/Feature/Europe/CzechRepublicTest.php similarity index 62% rename from tests/Feature/CzechRepublicTest.php rename to tests/Feature/Europe/CzechRepublicTest.php index 9de7c32..4ba2db5 100644 --- a/tests/Feature/CzechRepublicTest.php +++ b/tests/Feature/Europe/CzechRepublicTest.php @@ -1,11 +1,13 @@ [ 'rc' => '990224/9258', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1999-02-24'), - 'age' => Carbon::createFromFormat('Y-m-d', '1999-02-24')->age, + 'dob' => new DateTime('1999-02-24'), + 'age' => $this->calculateAge(new DateTime('1999-02-24')), ], 'Tereza' => [ 'rc' => '0157155328', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2001-07-15'), - 'age' => Carbon::createFromFormat('Y-m-d', '2001-07-15')->age, + 'dob' => new DateTime('2001-07-15'), + 'age' => $this->calculateAge(new DateTime('2001-07-15')), ], 'Adรฉla' => [ 'rc' => '975406/2494', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1997-04-06'), - 'age' => Carbon::createFromFormat('Y-m-d', '1997-04-06')->age, + 'dob' => new DateTime('1997-04-06'), + 'age' => $this->calculateAge(new DateTime('1997-04-06')), ], 'Lucie' => [ 'rc' => '956022/6027', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1995-10-22'), - 'age' => Carbon::createFromFormat('Y-m-d', '1995-10-22')->age, + 'dob' => new DateTime('1995-10-22'), + 'age' => $this->calculateAge(new DateTime('1995-10-22')), ], 'Petr' => [ 'rc' => '960326/2955', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1996-03-26'), - 'age' => Carbon::createFromFormat('Y-m-d', '1996-03-26')->age, + 'dob' => new DateTime('1996-03-26'), + 'age' => $this->calculateAge(new DateTime('1996-03-26')), ], ]; @@ -63,9 +65,10 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['rc'], 'CZ'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -76,13 +79,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['rc'], 'CZ') ); } foreach ($this->invalidIds as $rc) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($rc, 'CZ') ); } diff --git a/tests/Feature/DenmarkTest.php b/tests/Feature/Europe/DenmarkTest.php similarity index 63% rename from tests/Feature/DenmarkTest.php rename to tests/Feature/Europe/DenmarkTest.php index 1018666..25fea52 100644 --- a/tests/Feature/DenmarkTest.php +++ b/tests/Feature/Europe/DenmarkTest.php @@ -1,11 +1,13 @@ [ 'cpr' => '090792-1395', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1992-07-09'), - 'age' => Carbon::createFromFormat('Y-m-d', '1992-07-09')->age, + 'dob' => new DateTime('1992-07-09'), + 'age' => $this->calculateAge(new DateTime('1992-07-09')), ], 'naja' => [ 'cpr' => '070593-0600', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1993-05-07'), - 'age' => Carbon::createFromFormat('Y-m-d', '1993-05-07')->age, + 'dob' => new DateTime('1993-05-07'), + 'age' => $this->calculateAge(new DateTime('1993-05-07')), ], 'rolla' => [ 'cpr' => '150437-3068', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1937-04-15'), - 'age' => Carbon::createFromFormat('Y-m-d', '1937-04-15')->age, + 'dob' => new DateTime('1937-04-15'), + 'age' => $this->calculateAge(new DateTime('1937-04-15')), ], 'thomas' => [ 'cpr' => '160888-1995', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1988-08-16'), - 'age' => Carbon::createFromFormat('Y-m-d', '1988-08-16')->age, + 'dob' => new DateTime('1988-08-16'), + 'age' => $this->calculateAge(new DateTime('1988-08-16')), ], 'mia' => [ 'cpr' => '040404-7094', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2004-04-04'), - 'age' => Carbon::createFromFormat('Y-m-d', '2004-04-04')->age, + 'dob' => new DateTime('2004-04-04'), + 'age' => $this->calculateAge(new DateTime('2004-04-04')), ], ]; @@ -63,9 +65,10 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['cpr'], 'DK'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -76,13 +79,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['cpr'], 'DK') ); } foreach ($this->invalidIds as $cpr) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($cpr, 'DK') ); } diff --git a/tests/Feature/EstoniaTest.php b/tests/Feature/Europe/EstoniaTest.php similarity index 62% rename from tests/Feature/EstoniaTest.php rename to tests/Feature/Europe/EstoniaTest.php index 66c575b..6d608fc 100644 --- a/tests/Feature/EstoniaTest.php +++ b/tests/Feature/Europe/EstoniaTest.php @@ -1,11 +1,13 @@ [ 'ik' => '48004119745', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1980-04-11'), - 'age' => Carbon::createFromFormat('Y-m-d', '1980-04-11')->age, + 'dob' => new DateTime('1980-04-11'), + 'age' => $this->calculateAge(new DateTime('1980-04-11')), ], 'Kaarel' => [ 'ik' => '50108040021', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2001-08-04'), - 'age' => Carbon::createFromFormat('Y-m-d', '2001-08-04')->age, + 'dob' => new DateTime('2001-08-04'), + 'age' => $this->calculateAge(new DateTime('2001-08-04')), ], 'Seb' => [ 'ik' => '36910180118', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1969-10-18'), - 'age' => Carbon::createFromFormat('Y-m-d', '1969-10-18')->age, + 'dob' => new DateTime('1969-10-18'), + 'age' => $this->calculateAge(new DateTime('1969-10-18')), ], 'Jakob' => [ 'ik' => '38601230129', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1986-01-23'), - 'age' => Carbon::createFromFormat('Y-m-d', '1986-01-23')->age, + 'dob' => new DateTime('1986-01-23'), + 'age' => $this->calculateAge(new DateTime('1986-01-23')), ], 'Katarina' => [ 'ik' => '60310275631', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2003-10-27'), - 'age' => Carbon::createFromFormat('Y-m-d', '2003-10-27')->age, + 'dob' => new DateTime('2003-10-27'), + 'age' => $this->calculateAge(new DateTime('2003-10-27')), ], ]; @@ -62,9 +64,10 @@ public function test_extract_behaviour(): void { foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['ik'], 'EE'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -75,13 +78,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['ik'], 'EE') ); } foreach ($this->invalidIds as $ik) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($ik, 'EE') ); } diff --git a/tests/Feature/FinlandTest.php b/tests/Feature/Europe/FinlandTest.php similarity index 63% rename from tests/Feature/FinlandTest.php rename to tests/Feature/Europe/FinlandTest.php index 423921e..d5c42ac 100644 --- a/tests/Feature/FinlandTest.php +++ b/tests/Feature/Europe/FinlandTest.php @@ -1,11 +1,13 @@ [ 'hetu' => '040560-600E', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1960-05-04'), - 'age' => Carbon::createFromFormat('Y-m-d', '1960-05-04')->age + 'dob' => new DateTime('1960-05-04'), + 'age' => $this->calculateAge(new DateTime('1960-05-04')), ], 'elias' => [ 'hetu' => '121093-275N', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1993-10-12'), - 'age' => Carbon::createFromFormat('Y-m-d', '1993-10-12')->age + 'dob' => new DateTime('1993-10-12'), + 'age' => $this->calculateAge(new DateTime('1993-10-12')), ], 'ida' => [ 'hetu' => '260555-512H', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1955-05-26'), - 'age' => Carbon::createFromFormat('Y-m-d', '1955-05-26')->age + 'dob' => new DateTime('1955-05-26'), + 'age' => $this->calculateAge(new DateTime('1955-05-26')), ], 'iiro' => [ 'hetu' => '110416A479W', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2016-04-11'), - 'age' => Carbon::createFromFormat('Y-m-d', '2016-04-11')->age + 'dob' => new DateTime('2016-04-11'), + 'age' => $this->calculateAge(new DateTime('2016-04-11')), ], 'stig' => [ 'hetu' => '040403A2676', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2003-04-04'), - 'age' => Carbon::createFromFormat('Y-m-d', '2003-04-04')->age + 'dob' => new DateTime('2003-04-04'), + 'age' => $this->calculateAge(new DateTime('2003-04-04')), ] ]; @@ -63,9 +65,10 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['hetu'], 'FI'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -76,13 +79,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['hetu'], 'FI') ); } foreach ($this->invalidIds as $hetu) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($hetu, 'FI') ); } diff --git a/tests/Feature/FranceTest.php b/tests/Feature/Europe/FranceTest.php similarity index 57% rename from tests/Feature/FranceTest.php rename to tests/Feature/Europe/FranceTest.php index c950624..af2c111 100644 --- a/tests/Feature/FranceTest.php +++ b/tests/Feature/Europe/FranceTest.php @@ -1,11 +1,13 @@ [ 'insee' => '2820819398814 09', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m', '1982-08'), - 'age' => Carbon::createFromFormat('Y-m', '1982-08')->age, + 'dob' => DateTime::createFromFormat('Y-m', '1982-08'), + 'age' => $this->calculateAge(DateTime::createFromFormat('Y-m', '1982-08')), 'pob' => 'Corrรจze' ], 'Lance' => [ 'insee' => '1350455179061 16', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m', '1935-04'), - 'age' => Carbon::createFromFormat('Y-m', '1935-04')->age, + 'dob' => DateTime::createFromFormat('Y-m', '1935-04'), + 'age' => $this->calculateAge(DateTime::createFromFormat('Y-m', '1935-04')), 'pob' => 'Meuse' ], 'Ancelote' => [ 'insee' => '2381080214568 11', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m', '1938-10'), - 'age' => Carbon::createFromFormat('Y-m', '1938-10')->age, + 'dob' => DateTime::createFromFormat('Y-m', '1938-10'), + 'age' => $this->calculateAge(DateTime::createFromFormat('Y-m', '1938-10')), 'pob' => 'Somme' ], 'Lothair' => [ 'insee' => '1880858704571 57', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m', '1988-08'), - 'age' => Carbon::createFromFormat('Y-m', '1988-08')->age, + 'dob' => DateTime::createFromFormat('Y-m', '1988-08'), + 'age' => $this->calculateAge(DateTime::createFromFormat('Y-m', '1988-08')), 'pob' => 'Niรจvre' ], 'Millard' => [ 'insee' => '1030307795669 72', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m', '2003-03'), - 'age' => Carbon::createFromFormat('Y-m', '2003-03')->age, + 'dob' => DateTime::createFromFormat('Y-m', '2003-03'), + 'age' => $this->calculateAge(DateTime::createFromFormat('Y-m', '2003-03')), 'pob' => 'Ardรจche' ], 'Geoffrey' => [ 'insee' => '1820897401154 75', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m', '1982-08'), - 'age' => Carbon::createFromFormat('Y-m', '1982-08')->age, + 'dob' => DateTime::createFromFormat('Y-m', '1982-08'), + 'age' => $this->calculateAge(DateTime::createFromFormat('Y-m', '1982-08')), 'pob' => 'Rรฉunion' ], 'Galatee' => [ 'insee' => '2041098718061 61', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m', '2004-10'), - 'age' => Carbon::createFromFormat('Y-m', '2004-10')->age, + 'dob' => DateTime::createFromFormat('Y-m', '2004-10'), + 'age' => $this->calculateAge(DateTime::createFromFormat('Y-m', '2004-10')), 'pob' => 'French Polynesia' ], 'Leal' => [ 'insee' => '1103442505781 11', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m', '2010-04'), - 'age' => Carbon::createFromFormat('Y-m', '2010-04')->age, + 'dob' => DateTime::createFromFormat('Y-m', '2010-04'), + 'age' => $this->calculateAge(DateTime::createFromFormat('Y-m', '2010-04')), 'pob' => 'Loire' ], 'Odelette' => [ 'insee' => '2115028242370 20', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y', '2011'), - 'age' => Carbon::createFromFormat('Y', '2011')->age, + 'dob' => DateTime::createFromFormat('Y', '2011'), + 'age' => $this->calculateAge(DateTime::createFromFormat('Y', '2011')), 'pob' => 'Eure-et-Loir' ], 'Roch' => [ 'insee' => '199072A228070 10', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m', '1999-07'), - 'age' => Carbon::createFromFormat('Y-m', '1999-07')->age, + 'dob' => DateTime::createFromFormat('Y-m', '1999-07'), + 'age' => $this->calculateAge(DateTime::createFromFormat('Y-m', '1999-07')), 'pob' => 'Corse-du-Sud' ], 'Nadine' => [ 'insee' => '257092B844458 87', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m', '1957-09'), - 'age' => Carbon::createFromFormat('Y-m', '1957-09')->age, + 'dob' => DateTime::createFromFormat('Y-m', '1957-09'), + 'age' => $this->calculateAge(DateTime::createFromFormat('Y-m', '1957-09')), 'pob' => 'Haute-Corse' ] ]; @@ -109,10 +111,11 @@ public function test_extract_behaviour(): void { foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['insee'], 'FR'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); - $this->assertEquals($person['pob'], $citizen->getPlaceOfBirth()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['pob'], $citizen->getPlaceOfBirth()); } $this->expectException(InvalidLengthException::class); @@ -123,13 +126,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['insee'], 'FR') ); } foreach ($this->invalidIds as $insee) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($insee, 'FR') ); } diff --git a/tests/Feature/Europe/GermanyTest.php b/tests/Feature/Europe/GermanyTest.php new file mode 100644 index 0000000..54486d3 --- /dev/null +++ b/tests/Feature/Europe/GermanyTest.php @@ -0,0 +1,61 @@ +validIds = [ + '81872495633', + '48954371207', + '55492670836', + '12345678995', + '11234567890' + ]; + + $this->invalidIds = [ + '01234567812', + '81872495631', + '48954371206', + '55492670834', + '11234567899' + ]; + } + + public function test_extract_behaviour(): void + { + $this->expectException(UnsupportedOperationException::class); + + Socrates::getCitizenDataFromId('81872495633', 'DE'); + } + + public function test_validation_behaviour(): void + { + foreach ($this->validIds as $id) { + self::assertTrue( + Socrates::validateId($id, 'DE') + ); + } + + foreach ($this->invalidIds as $invalidId) { + self::assertFalse( + Socrates::validateId($invalidId, 'DE') + ); + } + + $this->expectException(InvalidLengthException::class); + + Socrates::validateId('0123456789', 'DE'); + } +} diff --git a/tests/Feature/GreeceTest.php b/tests/Feature/Europe/GreeceTest.php similarity index 89% rename from tests/Feature/GreeceTest.php rename to tests/Feature/Europe/GreeceTest.php index 2e8a871..e25a28c 100644 --- a/tests/Feature/GreeceTest.php +++ b/tests/Feature/Europe/GreeceTest.php @@ -1,10 +1,11 @@ validIds as $id) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($id, 'GR') ); } foreach ($this->invalidIds as $invalidId) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($invalidId, 'GR') ); } diff --git a/tests/Feature/HungaryTest.php b/tests/Feature/Europe/HungaryTest.php similarity index 61% rename from tests/Feature/HungaryTest.php rename to tests/Feature/Europe/HungaryTest.php index c59f599..d352098 100644 --- a/tests/Feature/HungaryTest.php +++ b/tests/Feature/Europe/HungaryTest.php @@ -1,11 +1,13 @@ [ 'pin' => '2-720216-1673', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1972-02-16'), - 'age' => Carbon::createFromFormat('Y-m-d', '1972-02-16')->age + 'dob' => new DateTime('1972-02-16'), + 'age' => $this->calculateAge(new DateTime('1972-02-16')), ], 'Dora' => [ 'pin' => '2-690609-5528', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1969-06-09'), - 'age' => Carbon::createFromFormat('Y-m-d', '1969-06-09')->age + 'dob' => new DateTime('1969-06-09'), + 'age' => $this->calculateAge(new DateTime('1969-06-09')), ], 'Jolan' => [ 'pin' => '2-840320-0414', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1984-03-20'), - 'age' => Carbon::createFromFormat('Y-m-d', '1984-03-20')->age + 'dob' => new DateTime('1984-03-20'), + 'age' => $this->calculateAge(new DateTime('1984-03-20')), ], 'Kapolcs' => [ 'pin' => '3-101010-5646', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2010-10-10'), - 'age' => Carbon::createFromFormat('Y-m-d', '2010-10-10')->age + 'dob' => new DateTime('2010-10-10'), + 'age' => $this->calculateAge(new DateTime('2010-10-10')), ], 'Vincze' => [ 'pin' => '3-080321-8523', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2008-03-21'), - 'age' => Carbon::createFromFormat('Y-m-d', '2008-03-21')->age + 'dob' => new DateTime('2008-03-21'), + 'age' => $this->calculateAge(new DateTime('2008-03-21')), ], ]; @@ -56,9 +58,10 @@ public function test_extract_behaviour(): void { foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['pin'], 'HU'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -69,13 +72,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['pin'], 'HU') ); } foreach ($this->invalidIds as $pin) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($pin, 'BG') ); } diff --git a/tests/Feature/IcelandTest.php b/tests/Feature/Europe/IcelandTest.php similarity index 60% rename from tests/Feature/IcelandTest.php rename to tests/Feature/Europe/IcelandTest.php index d881841..837a966 100644 --- a/tests/Feature/IcelandTest.php +++ b/tests/Feature/Europe/IcelandTest.php @@ -1,10 +1,12 @@ people = [ 'andi' => [ 'kt' => '0902862349', - 'dob' => Carbon::createFromFormat('Y-m-d', '1986-02-09'), - 'age' => Carbon::createFromFormat('Y-m-d', '1986-02-09')->age, + 'dob' => new DateTime('1986-02-09'), + 'age' => $this->calculateAge(new DateTime('1986-02-09')), ], 'freyja' => [ 'kt' => '120174-3399', - 'dob' => Carbon::createFromFormat('Y-m-d', '1974-01-12'), - 'age' => Carbon::createFromFormat('Y-m-d', '1974-01-12')->age, + 'dob' => new DateTime('1974-01-12'), + 'age' => $this->calculateAge(new DateTime('1974-01-12')), ], 'nair' => [ 'kt' => '1808905059', - 'dob' => Carbon::createFromFormat('Y-m-d', '1990-08-18'), - 'age' => Carbon::createFromFormat('Y-m-d', '1990-08-18')->age, + 'dob' => new DateTime('1990-08-18'), + 'age' => $this->calculateAge(new DateTime('1990-08-18')), ], 'eva' => [ 'kt' => '2008108569', - 'dob' => Carbon::createFromFormat('Y-m-d', '1910-08-20'), - 'age' => Carbon::createFromFormat('Y-m-d', '1910-08-20')->age, + 'dob' => new DateTime('1910-08-20'), + 'age' => $this->calculateAge(new DateTime('1910-08-20')), ], 'hrafn' => [ 'kt' => '100303-4930', - 'dob' => Carbon::createFromFormat('Y-m-d', '2003-03-10'), - 'age' => Carbon::createFromFormat('Y-m-d', '2003-03-10')->age, + 'dob' => new DateTime('2003-03-10'), + 'age' => $this->calculateAge(new DateTime('2003-03-10')), ], ]; @@ -57,8 +59,9 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['kt'], 'IS'); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -69,13 +72,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['kt'], 'IS') ); } foreach ($this->invalidIds as $kt) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($kt, 'IS') ); } diff --git a/tests/Feature/IrelandTest.php b/tests/Feature/Europe/IrelandTest.php similarity index 88% rename from tests/Feature/IrelandTest.php rename to tests/Feature/Europe/IrelandTest.php index 76bb332..2e79519 100644 --- a/tests/Feature/IrelandTest.php +++ b/tests/Feature/Europe/IrelandTest.php @@ -1,10 +1,11 @@ validIds as $id) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($id, 'IE') ); } foreach ($this->invalidIds as $id) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($id, 'IE') ); } diff --git a/tests/Feature/ItalyTest.php b/tests/Feature/Europe/ItalyTest.php similarity index 65% rename from tests/Feature/ItalyTest.php rename to tests/Feature/Europe/ItalyTest.php index b2b945c..bb82037 100644 --- a/tests/Feature/ItalyTest.php +++ b/tests/Feature/Europe/ItalyTest.php @@ -1,12 +1,14 @@ [ 'fc' => 'MRTMTT25D09F205Z', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1925-04-09'), - 'age' => Carbon::createFromFormat('Y-m-d', '1925-04-09')->age, + 'dob' => new DateTime('1925-04-09'), + 'age' => $this->calculateAge(new DateTime('1925-04-09')), 'pob' => 'MILANO (MI)' ], 'samantha miller' => [ 'fc' => 'MLLSNT82P65Z404U', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1982-09-25'), - 'age' => Carbon::createFromFormat('Y-m-d', '1982-09-25')->age, + 'dob' => new DateTime('1982-09-25'), + 'age' => $this->calculateAge(new DateTime('1982-09-25')), 'pob' => 'STATI UNITI D\'AMERICA' ], 'delmo castiglione' => [ 'fc' => 'DLMCTG75B07H227Y', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1975-02-07'), - 'age' => Carbon::createFromFormat('Y-m-d', '1975-02-07')->age, + 'dob' => new DateTime('1975-02-07'), + 'age' => $this->calculateAge(new DateTime('1975-02-07')), 'pob' => 'REINO (BN)' ], 'elsa barese' => [ 'fc' => 'BRSLSE08D50H987B', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2008-04-10'), - 'age' => Carbon::createFromFormat('Y-m-d', '2008-04-10')->age, + 'dob' => new DateTime('2008-04-10'), + 'age' => $this->calculateAge(new DateTime('2008-04-10')), 'pob' => 'SAN MARTINO ALFIERI (AT)' ], 'dario marcelo' => [ 'fc' => 'MRCDRA01A13A065E', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2001-01-13'), - 'age' => Carbon::createFromFormat('Y-m-d', '2001-01-13')->age, + 'dob' => new DateTime('2001-01-13'), + 'age' => $this->calculateAge(new DateTime('2001-01-13')), 'pob' => 'AFRICO (RC)' ], 'dario marchesani' => [ 'fc' => 'MRCDRALMAMPALSRE', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2001-01-13'), - 'age' => Carbon::createFromFormat('Y-m-d', '2001-01-13')->age, + 'dob' => new DateTime('2001-01-13'), + 'age' => $this->calculateAge(new DateTime('2001-01-13')), 'pob' => 'AFRICO (RC)' ] ]; @@ -76,10 +78,11 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['fc'], 'IT'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); - $this->assertEquals($person['pob'], $citizen->getPlaceOfBirth()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['pob'], $citizen->getPlaceOfBirth()); } $this->expectException(InvalidIdException::class); @@ -90,13 +93,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['fc'], 'IT') ); } foreach ($this->invalidIds as $fc) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($fc, 'IT') ); } diff --git a/tests/Feature/KosovoTest.php b/tests/Feature/Europe/KosovoTest.php similarity index 63% rename from tests/Feature/KosovoTest.php rename to tests/Feature/Europe/KosovoTest.php index c9b9fce..05f4066 100644 --- a/tests/Feature/KosovoTest.php +++ b/tests/Feature/Europe/KosovoTest.php @@ -1,11 +1,13 @@ [ 'jmbg' => '1009950933098', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1950-09-10'), - 'age' => Carbon::createFromFormat('Y-m-d', '1950-09-10')->age, + 'dob' => new DateTime('1950-09-10'), + 'age' => $this->calculateAge(new DateTime('1950-09-10')), 'pob' => 'Peฤ region (part of Peฤ District) - Kosovo' ], 'Erblina' => [ 'jmbg' => '2601966955857', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1966-01-26'), - 'age' => Carbon::createFromFormat('Y-m-d', '1966-01-26')->age, + 'dob' => new DateTime('1966-01-26'), + 'age' => $this->calculateAge(new DateTime('1966-01-26')), 'pob' => 'Prizren region (Prizren District) - Kosovo' ], 'Ajkuna' => [ 'jmbg' => '2202962926257', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1962-02-22'), - 'age' => Carbon::createFromFormat('Y-m-d', '1962-02-22')->age, + 'dob' => new DateTime('1962-02-22'), + 'age' => $this->calculateAge(new DateTime('1962-02-22')), 'pob' => 'Kosovska Mitrovica region (Kosovska Mitrovica District) - Kosovo' ], 'Esad' => [ 'jmbg' => '1404924982109', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1924-04-14'), - 'age' => Carbon::createFromFormat('Y-m-d', '1924-04-14')->age, + 'dob' => new DateTime('1924-04-14'), + 'age' => $this->calculateAge(new DateTime('1924-04-14')), 'pob' => 'Kosovo' ], 'Guzim' => [ 'jmbg' => '2103921983019', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1921-03-21'), - 'age' => Carbon::createFromFormat('Y-m-d', '1921-03-21')->age, + 'dob' => new DateTime('1921-03-21'), + 'age' => $this->calculateAge(new DateTime('1921-03-21')), 'pob' => 'Kosovo' ], ]; @@ -62,10 +64,11 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['jmbg'], 'XK'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); - $this->assertEquals($person['pob'], $citizen->getPlaceOfBirth()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['pob'], $citizen->getPlaceOfBirth()); } $this->expectException(InvalidLengthException::class); @@ -76,13 +79,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['jmbg'], 'XK') ); } foreach ($this->invalidIds as $jmbg) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($jmbg, 'XK') ); } diff --git a/tests/Feature/LatviaTest.php b/tests/Feature/Europe/LatviaTest.php similarity index 70% rename from tests/Feature/LatviaTest.php rename to tests/Feature/Europe/LatviaTest.php index 5492673..8a6e3e0 100644 --- a/tests/Feature/LatviaTest.php +++ b/tests/Feature/Europe/LatviaTest.php @@ -1,11 +1,13 @@ supportedExtractionPeople = [ 'Agnese' => [ 'pk' => '120673-10053', - 'dob' => Carbon::createFromFormat('Y-m-d', '1973-06-12'), - 'age' => Carbon::createFromFormat('Y-m-d', '1973-06-12')->age + 'dob' => new DateTime('1973-06-12'), + 'age' => $this->calculateAge(new DateTime('1973-06-12')), ], 'Rainers' => [ 'pk' => '031098-10386', - 'dob' => Carbon::createFromFormat('Y-m-d', '1998-10-03'), - 'age' => Carbon::createFromFormat('Y-m-d', '1998-10-03')->age + 'dob' => new DateTime('1998-10-03'), + 'age' => $this->calculateAge(new DateTime('1998-10-03')), ], 'Kin' => [ 'pk' => '250302-20559', - 'dob' => Carbon::createFromFormat('Y-m-d', '2002-03-25'), - 'age' => Carbon::createFromFormat('Y-m-d', '2002-03-25')->age + 'dob' => new DateTime('2002-03-25'), + 'age' => $this->calculateAge(new DateTime('2002-03-25')), ], 'Anton' => [ 'pk' => '300863-10955', - 'dob' => Carbon::createFromFormat('Y-m-d', '1963-08-30'), - 'age' => Carbon::createFromFormat('Y-m-d', '1963-08-30')->age + 'dob' => new DateTime('1963-08-30'), + 'age' => $this->calculateAge(new DateTime('1963-08-30')), ], 'Karlis' => [ 'pk' => '171210-20739', - 'dob' => Carbon::createFromFormat('Y-m-d', '2010-12-17'), - 'age' => Carbon::createFromFormat('Y-m-d', '2010-12-17')->age + 'dob' => new DateTime('2010-12-17'), + 'age' => $this->calculateAge(new DateTime('2010-12-17')), ] ]; @@ -76,8 +78,9 @@ public function test_extract_behaviour(): void { foreach ($this->supportedExtractionPeople as $person) { $citizen = Socrates::getCitizenDataFromId($person['pk'], 'LV'); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(UnsupportedOperationException::class); @@ -93,13 +96,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->supportedExtractionPeople as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['pk'], 'LV') ); } foreach ($this->invalidIds as $pk) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($pk, 'LV') ); } diff --git a/tests/Feature/LithuaniaTest.php b/tests/Feature/Europe/LithuaniaTest.php similarity index 62% rename from tests/Feature/LithuaniaTest.php rename to tests/Feature/Europe/LithuaniaTest.php index 6ee4baf..2c83998 100644 --- a/tests/Feature/LithuaniaTest.php +++ b/tests/Feature/Europe/LithuaniaTest.php @@ -1,16 +1,17 @@ [ 'ak' => '38409152012', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1984-09-15'), - 'age' => Carbon::createFromFormat('Y-m-d', '1984-09-15')->age + 'dob' => new DateTime('1984-09-15'), + 'age' => $this->calculateAge(new DateTime('1984-09-15')), ], 'natas' => [ 'ak' => '31710058023', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1917-10-05'), - 'age' => Carbon::createFromFormat('Y-m-d', '1917-10-05')->age + 'dob' => new DateTime('1917-10-05'), + 'age' => $this->calculateAge(new DateTime('1917-10-05')), ], 'daiva' => [ 'ak' => '44804129713', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1948-04-12'), - 'age' => Carbon::createFromFormat('Y-m-d', '1948-04-12')->age + 'dob' => new DateTime('1948-04-12'), + 'age' => $this->calculateAge(new DateTime('1948-04-12')), ], 'geta' => [ 'ak' => '60607279626', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2006-07-27'), - 'age' => Carbon::createFromFormat('Y-m-d', '2006-07-27')->age + 'dob' => new DateTime('2006-07-27'), + 'age' => $this->calculateAge(new DateTime('2006-07-27')), ], 'domynikas' => [ 'ak' => '50508199254', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2005-08-19'), - 'age' => Carbon::createFromFormat('Y-m-d', '2005-08-19')->age + 'dob' => new DateTime('2005-08-19'), + 'age' => $this->calculateAge(new DateTime('2005-08-19')), ] ]; @@ -63,9 +64,10 @@ public function test_extract_behaviour(): void { foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['ak'], 'LT'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -76,13 +78,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['ak'], 'LT') ); } foreach ($this->invalidIds as $ak) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($ak, 'LT') ); } diff --git a/tests/Feature/Europe/LuxembourgTest.php b/tests/Feature/Europe/LuxembourgTest.php new file mode 100644 index 0000000..e8e6018 --- /dev/null +++ b/tests/Feature/Europe/LuxembourgTest.php @@ -0,0 +1,30 @@ +expectException(RuntimeException::class); + + Socrates::validateId('1983046783', 'LU'); + } + + public function test_extract_behaviour(): void + { + $this->expectException(UnsupportedOperationException::class); + + Socrates::getCitizenDataFromId('1983046783', 'LU'); + } +} diff --git a/tests/Feature/MoldovaTest.php b/tests/Feature/Europe/MoldovaTest.php similarity index 88% rename from tests/Feature/MoldovaTest.php rename to tests/Feature/Europe/MoldovaTest.php index 7cb7d5c..4f06180 100644 --- a/tests/Feature/MoldovaTest.php +++ b/tests/Feature/Europe/MoldovaTest.php @@ -1,10 +1,11 @@ validIds as $id) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($id, 'MD') ); } foreach ($this->invalidIds as $id) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($id, 'MD') ); } diff --git a/tests/Feature/MontenegroTest.php b/tests/Feature/Europe/MontenegroTest.php similarity index 63% rename from tests/Feature/MontenegroTest.php rename to tests/Feature/Europe/MontenegroTest.php index 57df4fc..fda622d 100644 --- a/tests/Feature/MontenegroTest.php +++ b/tests/Feature/Europe/MontenegroTest.php @@ -1,11 +1,13 @@ [ 'jmbg' => '2106941231195', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1941-06-21'), - 'age' => Carbon::createFromFormat('Y-m-d', '1941-06-21')->age, - 'pob' => 'Budva, Kotor, Tivat - Montenegro' + 'dob' => new DateTime('1941-06-21'), + 'age' => $this->calculateAge(new DateTime('1941-06-21')), + 'pob' => 'Budva, Kotor, Tivat - Montenegro', ], 'Blaลพo' => [ 'jmbg' => '1502945264054', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1945-02-15'), - 'age' => Carbon::createFromFormat('Y-m-d', '1945-02-15')->age, - 'pob' => 'Nikลกiฤ, Pluลพine, ล avnik - Montenegro' + 'dob' => new DateTime('1945-02-15'), + 'age' => $this->calculateAge(new DateTime('1945-02-15')), + 'pob' => 'Nikลกiฤ, Pluลพine, ล avnik - Montenegro', ], 'Diko' => [ 'jmbg' => '2007950274591', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1950-07-20'), - 'age' => Carbon::createFromFormat('Y-m-d', '1950-07-20')->age, - 'pob' => 'Berane, Roลพaje, Plav, Andrijevica - Montenegro' + 'dob' => new DateTime('1950-07-20'), + 'age' => $this->calculateAge(new DateTime('1950-07-20')), + 'pob' => 'Berane, Roลพaje, Plav, Andrijevica - Montenegro', ], 'Ema' => [ 'jmbg' => '1302953216612', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1953-02-13'), - 'age' => Carbon::createFromFormat('Y-m-d', '1953-02-13')->age, - 'pob' => 'Podgorica, Danilovgrad, Kolaลกin - Montenegro' + 'dob' => new DateTime('1953-02-13'), + 'age' => $this->calculateAge(new DateTime('1953-02-13')), + 'pob' => 'Podgorica, Danilovgrad, Kolaลกin - Montenegro', ], 'Draginja' => [ 'jmbg' => '0204942275271', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1942-04-02'), - 'age' => Carbon::createFromFormat('Y-m-d', '1942-04-02')->age, - 'pob' => 'Berane, Roลพaje, Plav, Andrijevica - Montenegro' + 'dob' => new DateTime('1942-04-02'), + 'age' => $this->calculateAge(new DateTime('1942-04-02')), + 'pob' => 'Berane, Roลพaje, Plav, Andrijevica - Montenegro', ], ]; @@ -68,10 +70,11 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['jmbg'], 'ME'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); - $this->assertEquals($person['pob'], $citizen->getPlaceOfBirth()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['pob'], $citizen->getPlaceOfBirth()); } $this->expectException(InvalidLengthException::class); @@ -82,13 +85,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['jmbg'], 'ME') ); } foreach ($this->invalidIds as $jmbg) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($jmbg, 'ME') ); } diff --git a/tests/Feature/NetherlandsTest.php b/tests/Feature/Europe/NetherlandsTest.php similarity index 88% rename from tests/Feature/NetherlandsTest.php rename to tests/Feature/Europe/NetherlandsTest.php index 083daa7..18bb100 100644 --- a/tests/Feature/NetherlandsTest.php +++ b/tests/Feature/Europe/NetherlandsTest.php @@ -1,10 +1,11 @@ validIds as $id) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($id, 'NL') ); } foreach ($this->invalidIds as $id) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($id, 'NL') ); } diff --git a/tests/Feature/NorthMacedoniaTest.php b/tests/Feature/Europe/NorthMacedoniaTest.php similarity index 57% rename from tests/Feature/NorthMacedoniaTest.php rename to tests/Feature/Europe/NorthMacedoniaTest.php index dddef91..8cae981 100644 --- a/tests/Feature/NorthMacedoniaTest.php +++ b/tests/Feature/Europe/NorthMacedoniaTest.php @@ -1,11 +1,13 @@ [ 'jmbg' => '2408944448442', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1944-08-24'), - 'age' => Carbon::createFromFormat('Y-m-d', '1944-08-24')->age, - 'pob' => 'Prilep - North Macedonia' + 'dob' => new DateTime('1944-08-24'), + 'age' => $this->calculateAge(new DateTime('1944-08-24')), + 'pob' => 'Prilep - North Macedonia', ], 'Stefan' => [ 'jmbg' => '0705957463421', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1957-05-07'), - 'age' => Carbon::createFromFormat('Y-m-d', '1957-05-07')->age, - 'pob' => 'Strumica - North Macedonia' + 'dob' => new DateTime('1957-05-07'), + 'age' => $this->calculateAge(new DateTime('1957-05-07')), + 'pob' => 'Strumica - North Macedonia', ], 'Amyntas' => [ 'jmbg' => '1610936414199', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1936-10-16'), - 'age' => Carbon::createFromFormat('Y-m-d', '1936-10-16')->age, - 'pob' => 'Bitola - North Macedonia' + 'dob' => new DateTime('1936-10-16'), + 'age' => $this->calculateAge(new DateTime('1936-10-16')), + 'pob' => 'Bitola - North Macedonia', ], 'Dimitrov' => [ 'jmbg' => '1207942491481', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1942-07-12'), - 'age' => Carbon::createFromFormat('Y-m-d', '1942-07-12')->age, - 'pob' => 'ล tip - North Macedonia' + 'dob' => new DateTime('1942-07-12'), + 'age' => $this->calculateAge(new DateTime('1942-07-12')), + 'pob' => 'ล tip - North Macedonia', ], 'Kleitus' => [ 'jmbg' => '2808928401264', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1928-08-28'), - 'age' => Carbon::createFromFormat('Y-m-d', '1928-08-28')->age, - 'pob' => 'North Macedonia' + 'dob' => new DateTime('1928-08-28'), + 'age' => $this->calculateAge(new DateTime('1928-08-28')), + 'pob' => 'North Macedonia', ] ]; @@ -68,10 +70,11 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['jmbg'], 'MK'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); - $this->assertEquals($person['pob'], $citizen->getPlaceOfBirth()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['pob'], $citizen->getPlaceOfBirth()); } $this->expectException(InvalidLengthException::class); @@ -82,13 +85,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['jmbg'], 'MK') ); } foreach ($this->invalidIds as $jmbg) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($jmbg, 'MK') ); } diff --git a/tests/Feature/NorwayTest.php b/tests/Feature/Europe/NorwayTest.php similarity index 62% rename from tests/Feature/NorwayTest.php rename to tests/Feature/Europe/NorwayTest.php index 10ed992..cac9fb4 100644 --- a/tests/Feature/NorwayTest.php +++ b/tests/Feature/Europe/NorwayTest.php @@ -1,11 +1,13 @@ [ 'fn' => '05080176785', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2001-08-05'), - 'age' => Carbon::createFromFormat('Y-m-d', '2001-08-05')->age, + 'dob' => new DateTime('2001-08-05'), + 'age' => $this->calculateAge(new DateTime('2001-08-05')), ], 'astrid' => [ 'fn' => '20050761232', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2007-05-20'), - 'age' => Carbon::createFromFormat('Y-m-d', '2007-05-20')->age, + 'dob' => new DateTime('2007-05-20'), + 'age' => $this->calculateAge(new DateTime('2007-05-20')), ], 'linn' => [ 'fn' => '28094949248', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1949-09-28'), - 'age' => Carbon::createFromFormat('Y-m-d', '1949-09-28')->age, + 'dob' => new DateTime('1949-09-28'), + 'age' => $this->calculateAge(new DateTime('1949-09-28')), ], 'terje' => [ 'fn' => '14019513913', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1995-01-14'), - 'age' => Carbon::createFromFormat('Y-m-d', '1995-01-14')->age, + 'dob' => new DateTime('1995-01-14'), + 'age' => $this->calculateAge(new DateTime('1995-01-14')), ], 'heidi' => [ 'fn' => '01090749036', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1907-09-01'), - 'age' => Carbon::createFromFormat('Y-m-d', '1907-09-01')->age, + 'dob' => new DateTime('1907-09-01'), + 'age' => $this->calculateAge(new DateTime('1907-09-01')), ], ]; @@ -63,9 +65,10 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['fn'], 'NO'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -76,13 +79,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['fn'], 'NO') ); } foreach ($this->invalidIds as $fn) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($fn, 'NO') ); } diff --git a/tests/Feature/PolandTest.php b/tests/Feature/Europe/PolandTest.php similarity index 63% rename from tests/Feature/PolandTest.php rename to tests/Feature/Europe/PolandTest.php index 350ab33..f529ca6 100644 --- a/tests/Feature/PolandTest.php +++ b/tests/Feature/Europe/PolandTest.php @@ -1,11 +1,13 @@ [ 'pesel' => '91072592137', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1991-07-25'), - 'age' => Carbon::createFromFormat('Y-m-d', '1991-07-25')->age, + 'dob' => new DateTime('1991-07-25'), + 'age' => $this->calculateAge(new DateTime('1991-07-25')), ], 'Adelajda' => [ 'pesel' => '02220826789', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2002-02-08'), - 'age' => Carbon::createFromFormat('Y-m-d', '2002-02-08')->age, + 'dob' => new DateTime('2002-02-08'), + 'age' => $this->calculateAge(new DateTime('2002-02-08')), ], 'Izolda' => [ 'pesel' => '87050832348', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1987-05-08'), - 'age' => Carbon::createFromFormat('Y-m-d', '1987-05-08')->age, + 'dob' => new DateTime('1987-05-08'), + 'age' => $this->calculateAge(new DateTime('1987-05-08')), ], 'Klaudiusz' => [ 'pesel' => '87012962775', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1987-01-29'), - 'age' => Carbon::createFromFormat('Y-m-d', '1987-01-29')->age, + 'dob' => new DateTime('1987-01-29'), + 'age' => $this->calculateAge(new DateTime('1987-01-29')), ], 'Fryderyk' => [ 'pesel' => '64032906019', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1964-03-29'), - 'age' => Carbon::createFromFormat('Y-m-d', '1964-03-29')->age, + 'dob' => new DateTime('1964-03-29'), + 'age' => $this->calculateAge(new DateTime('1964-03-29')), ], ]; @@ -62,9 +64,10 @@ public function test_extract_behaviour(): void { foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['pesel'], 'PL'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -75,13 +78,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['pesel'], 'PL') ); } foreach ($this->invalidIds as $pesel) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($pesel, 'PL') ); } diff --git a/tests/Feature/PortugalTest.php b/tests/Feature/Europe/PortugalTest.php similarity index 89% rename from tests/Feature/PortugalTest.php rename to tests/Feature/Europe/PortugalTest.php index bc35ab2..a3ac17e 100644 --- a/tests/Feature/PortugalTest.php +++ b/tests/Feature/Europe/PortugalTest.php @@ -1,10 +1,11 @@ validIds as $id) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($id, 'PT') ); } foreach ($this->invalidIds as $invalidId) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($invalidId, 'PT') ); } diff --git a/tests/Feature/RomaniaTest.php b/tests/Feature/Europe/RomaniaTest.php similarity index 64% rename from tests/Feature/RomaniaTest.php rename to tests/Feature/Europe/RomaniaTest.php index fe692ca..301bfea 100644 --- a/tests/Feature/RomaniaTest.php +++ b/tests/Feature/Europe/RomaniaTest.php @@ -1,16 +1,17 @@ [ 'cnp' => '2931213173842', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1993-12-13'), - 'age' => Carbon::createFromFormat('Y-m-d', '1993-12-13')->age, + 'dob' => new DateTime('1993-12-13'), + 'age' => $this->calculateAge(new DateTime('1993-12-13')), 'pob' => 'Galati' ], 'andrei' => [ 'cnp' => '1941003395747', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1994-10-03'), - 'age' => Carbon::createFromFormat('Y-m-d', '1994-10-03')->age, + 'dob' => new DateTime('1994-10-03'), + 'age' => $this->calculateAge(new DateTime('1994-10-03')), 'pob' => 'Vrancea' ], 'elena' => [ 'cnp' => '2870917211577', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1987-09-17'), - 'age' => Carbon::createFromFormat('Y-m-d', '1987-09-17')->age, + 'dob' => new DateTime('1987-09-17'), + 'age' => $this->calculateAge(new DateTime('1987-09-17')), 'pob' => 'Ialomita' ], 'florin' => [ 'cnp' => '1850327466200', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1985-03-27'), - 'age' => Carbon::createFromFormat('Y-m-d', '1985-03-27')->age, + 'dob' => new DateTime('1985-03-27'), + 'age' => $this->calculateAge(new DateTime('1985-03-27')), 'pob' => 'Bucuresti Sectorul 6' ], 'mihai' => [ 'cnp' => '5010318045469', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2001-03-18'), - 'age' => Carbon::createFromFormat('Y-m-d', '2001-03-18')->age, + 'dob' => new DateTime('2001-03-18'), + 'age' => $this->calculateAge(new DateTime('2001-03-18')), 'pob' => 'Bacau' ] ]; @@ -68,10 +69,11 @@ public function test_extract_behaviour(): void { foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['cnp'], 'RO'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); - $this->assertEquals($person['pob'], $citizen->getPlaceOfBirth()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['pob'], $citizen->getPlaceOfBirth()); } $this->expectException(InvalidLengthException::class); @@ -82,13 +84,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['cnp'], 'RO') ); } foreach ($this->invalidIds as $cnp) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($cnp, 'RO') ); } diff --git a/tests/Feature/SerbiaTest.php b/tests/Feature/Europe/SerbiaTest.php similarity index 63% rename from tests/Feature/SerbiaTest.php rename to tests/Feature/Europe/SerbiaTest.php index b1cac48..1bc3537 100644 --- a/tests/Feature/SerbiaTest.php +++ b/tests/Feature/Europe/SerbiaTest.php @@ -1,11 +1,13 @@ [ 'jmbg' => '0101100710006', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2100-01-01'), - 'age' => Carbon::createFromFormat('Y-m-d', '2100-01-01')->age, - 'pob' => 'Belgrade region (City of Belgrade) - Central Serbia' + 'dob' => new DateTime('2100-01-01'), + 'age' => $this->calculateAge(new DateTime('2100-01-01')), + 'pob' => 'Belgrade region (City of Belgrade) - Central Serbia', ], 'Miloje' => [ 'jmbg' => '0110951074616', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1951-10-01'), - 'age' => Carbon::createFromFormat('Y-m-d', '1951-10-01')->age, - 'pob' => 'foreigners in Serbian province of Vojvodina' + 'dob' => new DateTime('1951-10-01'), + 'age' => $this->calculateAge(new DateTime('1951-10-01')), + 'pob' => 'foreigners in Serbian province of Vojvodina', ], 'Teodora' => [ 'jmbg' => '2702937737434', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1937-02-27'), - 'age' => Carbon::createFromFormat('Y-m-d', '1937-02-27')->age, - 'pob' => 'Niลก region (Niลกava District, Pirot District and Toplica District) - Central Serbia' + 'dob' => new DateTime('1937-02-27'), + 'age' => $this->calculateAge(new DateTime('1937-02-27')), + 'pob' => 'Niลก region (Niลกava District, Pirot District and Toplica District) - Central Serbia', ], 'Jana' => [ 'jmbg' => '2606936778324', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1936-06-26'), - 'age' => Carbon::createFromFormat('Y-m-d', '1936-06-26')->age, - 'pob' => 'Podrinje and Kolubara regions (Maฤva District and Kolubara District) - Central Serbia' + 'dob' => new DateTime('1936-06-26'), + 'age' => $this->calculateAge(new DateTime('1936-06-26')), + 'pob' => 'Podrinje and Kolubara regions (Maฤva District and Kolubara District) - Central Serbia', ], 'Petra' => [ 'jmbg' => '1209992745266', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1992-09-12'), - 'age' => Carbon::createFromFormat('Y-m-d', '1992-09-12')->age, - 'pob' => 'Southern Morava region (Jablanica District and Pฤinja District) - Central Serbia' + 'dob' => new DateTime('1992-09-12'), + 'age' => $this->calculateAge(new DateTime('1992-09-12')), + 'pob' => 'Southern Morava region (Jablanica District and Pฤinja District) - Central Serbia', ] ]; @@ -68,10 +70,11 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['jmbg'], 'RS'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); - $this->assertEquals($person['pob'], $citizen->getPlaceOfBirth()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals($person['dob'], $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['pob'], $citizen->getPlaceOfBirth()); } $this->expectException(InvalidLengthException::class); @@ -82,13 +85,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['jmbg'], 'RS') ); } foreach ($this->invalidIds as $jmbg) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($jmbg, 'RS') ); } diff --git a/tests/Feature/SlovakiaTest.php b/tests/Feature/Europe/SlovakiaTest.php similarity index 61% rename from tests/Feature/SlovakiaTest.php rename to tests/Feature/Europe/SlovakiaTest.php index 161970b..a37dba1 100644 --- a/tests/Feature/SlovakiaTest.php +++ b/tests/Feature/Europe/SlovakiaTest.php @@ -1,11 +1,13 @@ [ 'rc' => '931027/3951', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1993-10-27'), - 'age' => Carbon::createFromFormat('Y-m-d', '1993-10-27')->age, + 'dob' => new DateTime('1993-10-27'), + 'age' => $this->calculateAge(new DateTime('1993-10-27')), ], 'Miroslav' => [ 'rc' => '000816/9733', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2000-08-16'), - 'age' => Carbon::createFromFormat('Y-m-d', '2000-08-16')->age, + 'dob' => new DateTime('2000-08-16'), + 'age' => $this->calculateAge(new DateTime('2000-08-16')), ], 'Natalia' => [ 'rc' => '015612/5552', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2001-06-12'), - 'age' => Carbon::createFromFormat('Y-m-d', '2001-06-12')->age, + 'dob' => new DateTime('2001-06-12'), + 'age' => $this->calculateAge(new DateTime('2001-06-12')), ], 'Victoria' => [ 'rc' => '935103/6189', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1993-01-03'), - 'age' => Carbon::createFromFormat('Y-m-d', '1993-01-03')->age, + 'dob' => new DateTime('1993-01-03'), + 'age' => $this->calculateAge(new DateTime('1993-01-03')), ], 'Joลพko' => [ 'rc' => '010722/4634', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2001-07-22'), - 'age' => Carbon::createFromFormat('Y-m-d', '2001-07-22')->age, + 'dob' => new DateTime('2001-07-22'), + 'age' => $this->calculateAge(new DateTime('2001-07-22')), ], ]; @@ -63,9 +65,10 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['rc'], 'SK'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -76,19 +79,19 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['rc'], 'SK') ); } foreach ($this->invalidIds as $rc) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($rc, 'SK') ); } $this->expectException(InvalidLengthException::class); - Socrates::validateId('88606/875', 'CZ'); + Socrates::validateId('88606/875', 'SK'); } } diff --git a/tests/Feature/SloveniaTest.php b/tests/Feature/Europe/SloveniaTest.php similarity index 58% rename from tests/Feature/SloveniaTest.php rename to tests/Feature/Europe/SloveniaTest.php index 5b96971..576bdd8 100644 --- a/tests/Feature/SloveniaTest.php +++ b/tests/Feature/Europe/SloveniaTest.php @@ -1,11 +1,13 @@ [ 'emso' => '0101006500006', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2006-01-01'), - 'age' => Carbon::createFromFormat('Y-m-d', '2006-01-01')->age, - 'pob' => 'Slovenia' + 'dob' => new DateTime('2006-01-01'), + 'age' => $this->calculateAge(new DateTime('2006-01-01')), + 'pob' => 'Slovenia', ], 'Zoja' => [ 'emso' => '0310933507830', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1933-10-03'), - 'age' => Carbon::createFromFormat('Y-m-d', '1933-10-03')->age, - 'pob' => 'Slovenia' + 'dob' => new DateTime('1933-10-03'), + 'age' => $this->calculateAge(new DateTime('1933-10-03')), + 'pob' => 'Slovenia', ], 'Jaka' => [ 'emso' => '1408984500257', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1984-08-14'), - 'age' => Carbon::createFromFormat('Y-m-d', '1984-08-14')->age, - 'pob' => 'Slovenia' + 'dob' => new DateTime('1984-08-14'), + 'age' => $this->calculateAge(new DateTime('1984-08-14')), + 'pob' => 'Slovenia', ], 'Lana' => [ 'emso' => '0205962509348', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1962-05-02'), - 'age' => Carbon::createFromFormat('Y-m-d', '1962-05-02')->age, - 'pob' => 'Slovenia' + 'dob' => new DateTime('1962-05-02'), + 'age' => $this->calculateAge(new DateTime('1962-05-02')), + 'pob' => 'Slovenia', ], 'Mia' => [ 'emso' => '1201962509788', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1962-01-12'), - 'age' => Carbon::createFromFormat('Y-m-d', '1962-01-12')->age, - 'pob' => 'Slovenia' + 'dob' => new DateTime('1962-01-12'), + 'age' => $this->calculateAge(new DateTime('1962-01-12')), + 'pob' => 'Slovenia', ], ]; @@ -68,10 +70,11 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['emso'], 'SI'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); - $this->assertEquals($person['pob'], $citizen->getPlaceOfBirth()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['pob'], $citizen->getPlaceOfBirth()); } $this->expectException(InvalidLengthException::class); @@ -82,13 +85,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['emso'], 'SI') ); } foreach ($this->invalidIds as $emso) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($emso, 'SI') ); } diff --git a/tests/Feature/SpainTest.php b/tests/Feature/Europe/SpainTest.php similarity index 88% rename from tests/Feature/SpainTest.php rename to tests/Feature/Europe/SpainTest.php index 5985efd..2c7d394 100644 --- a/tests/Feature/SpainTest.php +++ b/tests/Feature/Europe/SpainTest.php @@ -1,10 +1,11 @@ validIds as $id) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($id, 'ES') ); } foreach ($this->invalidIds as $invalidId) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($invalidId, 'ES') ); } diff --git a/tests/Feature/SwedenTest.php b/tests/Feature/Europe/SwedenTest.php similarity index 63% rename from tests/Feature/SwedenTest.php rename to tests/Feature/Europe/SwedenTest.php index ef7b0e0..53c592b 100644 --- a/tests/Feature/SwedenTest.php +++ b/tests/Feature/Europe/SwedenTest.php @@ -1,11 +1,13 @@ [ 'psn' => '550309-6447', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1955-03-09'), - 'age' => Carbon::createFromFormat('Y-m-d', '1955-03-09')->age, + 'dob' => new DateTime('1955-03-09'), + 'age' => $this->calculateAge(new DateTime('1955-03-09')), ], 'otto' => [ 'psn' => '001020-1895', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2000-10-20'), - 'age' => Carbon::createFromFormat('Y-m-d', '2000-10-20')->age, + 'dob' => new DateTime('2000-10-20'), + 'age' => $this->calculateAge(new DateTime('2000-10-20')), ], 'lowa' => [ 'psn' => '751208-0222', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1975-12-08'), - 'age' => Carbon::createFromFormat('Y-m-d', '1975-12-08')->age, + 'dob' => new DateTime('1975-12-08'), + 'age' => $this->calculateAge(new DateTime('1975-12-08')), ], 'edward' => [ 'psn' => '19771211-2775', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1977-12-11'), - 'age' => Carbon::createFromFormat('Y-m-d', '1977-12-11')->age, + 'dob' => new DateTime('1977-12-11'), + 'age' => $this->calculateAge(new DateTime('1977-12-11')), ], 'maia' => [ 'psn' => '380519-7807', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1938-05-19'), - 'age' => Carbon::createFromFormat('Y-m-d', '1938-05-19')->age, + 'dob' => new DateTime('1938-05-19'), + 'age' => $this->calculateAge(new DateTime('1938-05-19')), ], ]; @@ -63,9 +65,10 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['psn'], 'SE'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -76,13 +79,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['psn'], 'SE') ); } foreach ($this->invalidIds as $invalidId) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($invalidId, 'SE') ); } diff --git a/tests/Feature/SwitzerlandTest.php b/tests/Feature/Europe/SwitzerlandTest.php similarity index 90% rename from tests/Feature/SwitzerlandTest.php rename to tests/Feature/Europe/SwitzerlandTest.php index 163e381..70dea64 100644 --- a/tests/Feature/SwitzerlandTest.php +++ b/tests/Feature/Europe/SwitzerlandTest.php @@ -1,10 +1,11 @@ validIds as $id) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($id, 'CH') ); } foreach ($this->invalidIds as $invalidId) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($invalidId, 'CH') ); } diff --git a/tests/Feature/TurkeyTest.php b/tests/Feature/Europe/TurkeyTest.php similarity index 89% rename from tests/Feature/TurkeyTest.php rename to tests/Feature/Europe/TurkeyTest.php index d7b57d7..4d541f4 100644 --- a/tests/Feature/TurkeyTest.php +++ b/tests/Feature/Europe/TurkeyTest.php @@ -1,10 +1,11 @@ validIds as $id) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($id, 'TR') ); } foreach ($this->invalidIds as $invalidId) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($invalidId, 'TR') ); } diff --git a/tests/Feature/UkraineTest.php b/tests/Feature/Europe/UkraineTest.php similarity index 62% rename from tests/Feature/UkraineTest.php rename to tests/Feature/Europe/UkraineTest.php index 26c2433..cd5f59a 100644 --- a/tests/Feature/UkraineTest.php +++ b/tests/Feature/Europe/UkraineTest.php @@ -1,11 +1,13 @@ [ 'id' => '4031090675', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2010-05-13'), - 'age' => Carbon::createFromFormat('Y-m-d', '2010-05-13')->age + 'dob' => new DateTime('2010-05-13'), + 'age' => $this->calculateAge(new DateTime('2010-05-13')), ], 'vadym' => [ 'id' => '3292197434', 'gender' => Gender::MALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1990-02-18'), - 'age' => Carbon::createFromFormat('Y-m-d', '1990-02-18')->age + 'dob' => new DateTime('1990-02-18'), + 'age' => $this->calculateAge(new DateTime('1990-02-18')), ], 'veronika' => [ 'id' => '2023599602', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '1955-05-27'), - 'age' => Carbon::createFromFormat('Y-m-d', '1955-05-27')->age + 'dob' => new DateTime('1955-05-27'), + 'age' => $this->calculateAge(new DateTime('1955-05-27')), ], 'ruslana' => [ 'id' => '4247134484', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2016-04-12'), - 'age' => Carbon::createFromFormat('Y-m-d', '2016-04-12')->age + 'dob' => new DateTime('2016-04-12'), + 'age' => $this->calculateAge(new DateTime('2016-04-12')), ], 'zoya' => [ 'id' => '3771509002', 'gender' => Gender::FEMALE, - 'dob' => Carbon::createFromFormat('Y-m-d', '2003-04-05'), - 'age' => Carbon::createFromFormat('Y-m-d', '2003-04-05')->age + 'dob' => new DateTime('2003-04-05'), + 'age' => $this->calculateAge(new DateTime('2003-04-05')), ] ]; @@ -63,9 +65,10 @@ public function test_extract_behaviour(): void foreach ($this->people as $person) { $citizen = Socrates::getCitizenDataFromId($person['id'], 'UA'); - $this->assertEquals($person['gender'], $citizen->getGender()); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); } $this->expectException(InvalidLengthException::class); @@ -76,13 +79,13 @@ public function test_extract_behaviour(): void public function test_validation_behaviour(): void { foreach ($this->people as $person) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($person['id'], 'UA') ); } foreach ($this->invalidIds as $id) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($id, 'UA') ); } diff --git a/tests/Feature/UnitedKingdomTest.php b/tests/Feature/Europe/UnitedKingdomTest.php similarity index 90% rename from tests/Feature/UnitedKingdomTest.php rename to tests/Feature/Europe/UnitedKingdomTest.php index c71ff95..eb9cb27 100644 --- a/tests/Feature/UnitedKingdomTest.php +++ b/tests/Feature/Europe/UnitedKingdomTest.php @@ -1,10 +1,11 @@ validIds as $id) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($id, 'GB') ); } foreach ($this->invalidIds as $invalidId) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($invalidId, 'GB') ); } diff --git a/tests/Feature/FeatureTest.php b/tests/Feature/FeatureTest.php index 2d30d6b..1298408 100644 --- a/tests/Feature/FeatureTest.php +++ b/tests/Feature/FeatureTest.php @@ -2,6 +2,7 @@ namespace Reducktion\Socrates\Tests\Feature; +use DateTime; use Reducktion\Socrates\Tests\TestCase; abstract class FeatureTest extends TestCase @@ -9,4 +10,9 @@ abstract class FeatureTest extends TestCase abstract public function test_extract_behaviour(): void; abstract public function test_validation_behaviour(): void; + + public function calculateAge(DateTime $dateOfBirth): int + { + return (new DateTime())->diff($dateOfBirth)->y; + } } diff --git a/tests/Feature/LuxembourgTest.php b/tests/Feature/LuxembourgTest.php deleted file mode 100644 index 3133b8a..0000000 --- a/tests/Feature/LuxembourgTest.php +++ /dev/null @@ -1,86 +0,0 @@ -people = [ - 'Gabriel' => [ - 'nin' => '1983081246783', - 'dob' => Carbon::createFromFormat('Y-m-d', '1983-08-12'), - 'age' => Carbon::createFromFormat('Y-m-d', '1983-08-12')->age, - ], - 'Emma' => [ - 'nin' => '2003042581931', - 'dob' => Carbon::createFromFormat('Y-m-d', '2003-04-25'), - 'age' => Carbon::createFromFormat('Y-m-d', '2003-04-25')->age, - ], - 'Leo' => [ - 'nin' => '1971110258746', - 'dob' => Carbon::createFromFormat('Y-m-d', '1971-11-02'), - 'age' => Carbon::createFromFormat('Y-m-d', '1971-11-02')->age, - ], - 'Lara' => [ - 'nin' => '2012051469336', - 'dob' => Carbon::createFromFormat('Y-m-d', '2012-05-14'), - 'age' => Carbon::createFromFormat('Y-m-d', '2012-05-14')->age, - ], - 'Noah' => [ - 'nin' => '1994092874551', - 'dob' => Carbon::createFromFormat('Y-m-d', '1994-09-28'), - 'age' => Carbon::createFromFormat('Y-m-d', '1994-09-28')->age, - ], - ]; - - $this->invalidIds = [ - '1994789587182', - '5971654782313', - '2055101054879', - '1997053045687', - '2001111123654', - ]; - } - - public function test_extract_behaviour(): void - { - foreach ($this->people as $person) { - $citizen = Socrates::getCitizenDataFromId($person['nin'], 'LU'); - $this->assertEquals($person['dob'], $citizen->getDateOfBirth()); - $this->assertEquals($person['age'], $citizen->getAge()); - } - - $this->expectException(InvalidLengthException::class); - - Socrates::getCitizenDataFromId('1983046783', 'LU'); - } - - public function test_validation_behaviour(): void - { - foreach ($this->people as $person) { - $this->assertTrue( - Socrates::validateId($person['nin'], 'LU') - ); - } - - foreach ($this->invalidIds as $nin) { - $this->assertFalse( - Socrates::validateId($nin, 'LU') - ); - } - - $this->expectException(InvalidLengthException::class); - - Socrates::validateId('1983046783', 'LU'); - } -} diff --git a/tests/Feature/NorthAmerica/CanadaTest.php b/tests/Feature/NorthAmerica/CanadaTest.php new file mode 100644 index 0000000..ddc0bf9 --- /dev/null +++ b/tests/Feature/NorthAmerica/CanadaTest.php @@ -0,0 +1,61 @@ +validIds = [ + '046 454 286', + '671 143 899', + '002 371 920', + '501 343 719', + '912 046 737', + ]; + + $this->invalidIds = [ + '512 917 638', + '322 710 094', + '761 999 512', + '061 003 528', + '654 789 093', + ]; + } + + public function test_extract_behaviour(): void + { + $this->expectException(UnsupportedOperationException::class); + + Socrates::getCitizenDataFromId('85 712 123', 'CA'); + } + + public function test_validation_behaviour(): void + { + foreach ($this->validIds as $id) { + self::assertTrue( + Socrates::validateId($id, 'CA') + ); + } + + foreach ($this->invalidIds as $invalidId) { + self::assertFalse( + Socrates::validateId($invalidId, 'CA') + ); + } + + $this->expectException(InvalidLengthException::class); + + Socrates::validateId('781 211 2231', 'CA'); + } +} diff --git a/tests/Feature/NorthAmerica/MexicoTest.php b/tests/Feature/NorthAmerica/MexicoTest.php new file mode 100644 index 0000000..f63d30e --- /dev/null +++ b/tests/Feature/NorthAmerica/MexicoTest.php @@ -0,0 +1,115 @@ +people = [ + 'juan' => [ + 'curp' => 'MAAR790213HMNRLF03', + 'gender' => Gender::MALE, + 'dob' => new DateTime('1979-2-13'), + 'age' => $this->calculateAge(new DateTime('1979-2-13')), + ], + 'letitia' => [ + 'curp' => 'HEGG560427MVZRRL04', + 'gender' => Gender::FEMALE, + 'dob' => new DateTime('1956-4-27'), + 'age' => $this->calculateAge(new DateTime('1956-4-27')), + ], + 'augustin' => [ + 'curp' => 'BOXW010820HNERXNA1', + 'gender' => Gender::MALE, + 'dob' => new DateTime('2001-8-20'), + 'age' => $this->calculateAge(new DateTime('2001-8-20')), + ], + 'diego' => [ + 'curp' => 'TUAZ080213HMNRLFA3', + 'gender' => Gender::MALE, + 'dob' => new DateTime('2008-2-13'), + 'age' => $this->calculateAge(new DateTime('2008-2-13')), + ], + ]; + + $this->validIds = [ + 'MAAR790213HMNRLF03', + 'HEGG560427MVZRRL04', + 'BOXW310820HNERXN09', + 'TUAZ790213HMNRLF02', + 'TUAZ040213MCLRLFA7', + 'JIAZ040213MCLRLFA6', + 'XOAZ980927MCLRLFA6', + ]; + + $this->invalidIds = [ + '1AAR790213HMNRLF03', + 'MRAR790213HMNRLF03', + 'MAARA90213HMNRLF03', + 'MAAR7A0213HMNRLF03', + 'MAAR79A213HMNRLF03', + 'MAAR790A13HMNRLF03', + 'MAAR7902A3HMNRLF03', + 'MAAR79021AHMNRLF03', + 'MRAR791313HMNRLF03', + 'MRAR791332HMNRLF03', + 'MRAR7913321MNRLF03', + 'MRAR791332AMNRLF03', + 'MRAR7902131MNRLF03', + 'MRAR790213ZMNRLF03', + 'MAAR790213HZORLF03', + 'MAAR790213HZ1RLF03', + 'MAAR790213HMNRLF04', + 'MAAR790213HMNRLF0A', + ]; + } + + public function test_extract_behaviour(): void + { + foreach ($this->people as $person) { + $citizen = Socrates::getCitizenDataFromId($person['curp'], 'MX'); + + self::assertEquals($person['gender'], $citizen->getGender()); + self::assertEquals(Carbon::instance($person['dob']), $citizen->getDateOfBirth()); + self::assertEquals($person['dob'], $citizen->getDateOfBirthNative()); + self::assertEquals($person['age'], $citizen->getAge()); + } + + $this->expectException(InvalidLengthException::class); + + Socrates::getCitizenDataFromId('69218938062', 'MX'); + } + + public function test_validation_behaviour(): void + { + foreach ($this->validIds as $id) { + self::assertTrue( + Socrates::validateId($id, 'MX') + ); + } + + foreach ($this->invalidIds as $invalidId) { + self::assertFalse( + Socrates::validateId($invalidId, 'MX') + ); + } + + $this->expectException(InvalidLengthException::class); + + Socrates::validateId('1621543419643', 'MX'); + } +} diff --git a/tests/Feature/NorthAmerica/UnitedStatesTest.php b/tests/Feature/NorthAmerica/UnitedStatesTest.php new file mode 100644 index 0000000..9649951 --- /dev/null +++ b/tests/Feature/NorthAmerica/UnitedStatesTest.php @@ -0,0 +1,64 @@ +validIds = [ + '167-38-1265', + '536-22-8726', + '536-22-5232', + '574-22-7664', + '671-26-9121' + ]; + + $this->invalidIds = [ + '078-05-1120', + '219-09-9999', + '457-55-5462', + '666-91-8271', + '000-12-7652', + '167-00-6321', + '167-11-0000', + '981-76-1521', + ]; + } + + public function test_extract_behaviour(): void + { + $this->expectException(UnsupportedOperationException::class); + + Socrates::getCitizenDataFromId('145565258ZZY', 'US'); + } + + public function test_validation_behaviour(): void + { + foreach ($this->validIds as $id) { + self::assertTrue( + Socrates::validateId($id, 'US') + ); + } + + foreach ($this->invalidIds as $invalidId) { + self::assertFalse( + Socrates::validateId($invalidId, 'US') + ); + } + + $this->expectException(InvalidLengthException::class); + + Socrates::validateId('11084129 8 ZX', 'US'); + } +} diff --git a/tests/Feature/SouthAmerica/ArgentinaTest.php b/tests/Feature/SouthAmerica/ArgentinaTest.php new file mode 100644 index 0000000..a8894a8 --- /dev/null +++ b/tests/Feature/SouthAmerica/ArgentinaTest.php @@ -0,0 +1,68 @@ +validIds = [ + '20319731467', + '23129006544', + '20385823631', + '20123456786', + '33711146259', + '30716762145', + '30629357552', + '27061822238', + '20028314788', + '23010852064', + '27083496970', + ]; + + $this->invalidIds = [ + '42319731467', + '20999999997', + '20349735981', + '20123456785', + '30010852064', + ]; + } + + public function test_extract_behaviour(): void + { + $this->expectException(UnsupportedOperationException::class); + + Socrates::getCitizenDataFromId('', 'AR'); + } + + public function test_validation_behaviour(): void + { + foreach ($this->validIds as $id) { + self::assertTrue( + Socrates::validateId($id, 'AR'), + $id + ); + } + + foreach ($this->invalidIds as $id) { + self::assertFalse( + Socrates::validateId($id, 'AR') + ); + } + + $this->expectException(InvalidLengthException::class); + + Socrates::validateId('123456', 'AR'); + } +} diff --git a/tests/Feature/BrazilTest.php b/tests/Feature/SouthAmerica/BrazilTest.php similarity index 91% rename from tests/Feature/BrazilTest.php rename to tests/Feature/SouthAmerica/BrazilTest.php index ef066de..c7164b6 100644 --- a/tests/Feature/BrazilTest.php +++ b/tests/Feature/SouthAmerica/BrazilTest.php @@ -1,10 +1,11 @@ validIds as $id) { - $this->assertTrue( + self::assertTrue( Socrates::validateId($id, 'BR'), $id ); } foreach ($this->invalidIds as $id) { - $this->assertFalse( + self::assertFalse( Socrates::validateId($id, 'BR') ); } diff --git a/tests/Feature/SouthAmerica/ChileTest.php b/tests/Feature/SouthAmerica/ChileTest.php new file mode 100644 index 0000000..f2d2d07 --- /dev/null +++ b/tests/Feature/SouthAmerica/ChileTest.php @@ -0,0 +1,69 @@ +validIds = [ + '19132031-K', + '5587600-2', + '22269631-3', + '11881570-K', + '12067530-3', + '14674646-2', + '14928530-K', + '22931750-4', + '5258230-K', + '11077954-2', + '30.686.957-4', + ]; + + $this->invalidIds = [ + '19132031-k', //number valid but lower "k" + '11111112-9', + '55185352-8', + '19947727-K', + '12345689-2', + '11111111-L', + '22931750-K', + ]; + } + + public function test_extract_behaviour(): void + { + $this->expectException(UnsupportedOperationException::class); + + Socrates::getCitizenDataFromId('', 'CL'); + } + + public function test_validation_behaviour(): void + { + foreach ($this->validIds as $id) { + self::assertTrue( + Socrates::validateId($id, 'CL'), + $id + ); + } + + foreach ($this->invalidIds as $id) { + self::assertFalse( + Socrates::validateId($id, 'CL') + ); + } + + $this->expectException(InvalidLengthException::class); + + Socrates::validateId('1234567', 'CL'); + } +} diff --git a/tests/Feature/SouthAmerica/EcuadorTest.php b/tests/Feature/SouthAmerica/EcuadorTest.php new file mode 100644 index 0000000..d608e1b --- /dev/null +++ b/tests/Feature/SouthAmerica/EcuadorTest.php @@ -0,0 +1,63 @@ +validIds = [ + '0926687856', + '1159944279', + '0350442026', + '1930773021', + '2010123061', + '1710034065', + ]; + + $this->invalidIds = [ + '1234567890', + '2510034065', + '1770034065', + '1570111029', + '0126987368', + ]; + } + + public function test_extract_behaviour(): void + { + $this->expectException(UnsupportedOperationException::class); + + Socrates::getCitizenDataFromId('', 'EC'); + } + + public function test_validation_behaviour(): void + { + foreach ($this->validIds as $id) { + self::assertTrue( + Socrates::validateId($id, 'EC'), + $id + ); + } + + foreach ($this->invalidIds as $id) { + self::assertFalse( + Socrates::validateId($id, 'EC') + ); + } + + $this->expectException(InvalidLengthException::class); + + Socrates::validateId('123456789', 'EC'); + } +} diff --git a/tests/Feature/SouthAmerica/PeruTest.php b/tests/Feature/SouthAmerica/PeruTest.php new file mode 100644 index 0000000..9387d63 --- /dev/null +++ b/tests/Feature/SouthAmerica/PeruTest.php @@ -0,0 +1,72 @@ +validIds = [ + '447690450', + '858845624', + '09189667B', + '739184148', + '83015889H', + '178011460', + '101174102', + '43451826-7', + '10117410-G', + '06460698K', + ]; + + $this->invalidIds = [ + '101174103', + '10117410H', + '178A11460', + '83015889B', + '09189667V', + '85884562D', + '55833222D', + '64708232D', + '64015292J', + '73918414W', + ]; + } + + public function test_extract_behaviour(): void + { + $this->expectException(UnsupportedOperationException::class); + + Socrates::getCitizenDataFromId('', 'PE'); + } + + public function test_validation_behaviour(): void + { + foreach ($this->validIds as $id) { + self::assertTrue( + Socrates::validateId($id, 'PE'), + $id + ); + } + + foreach ($this->invalidIds as $id) { + self::assertFalse( + Socrates::validateId($id, 'PE') + ); + } + + $this->expectException(InvalidLengthException::class); + + Socrates::validateId('1234567', 'PE'); + } +} diff --git a/tests/Feature/SouthAmerica/UruguayTest.php b/tests/Feature/SouthAmerica/UruguayTest.php new file mode 100644 index 0000000..48edc46 --- /dev/null +++ b/tests/Feature/SouthAmerica/UruguayTest.php @@ -0,0 +1,75 @@ +validIds = [ + '9.814.898-7', + '99609297', + '7.019.519.6', + '2.797.689 6', + '24895354', + '40008026', + '60125711', + '11111111', + '22222222', + '52574582', + '96072455', + '70505244', + ]; + + $this->invalidIds = [ + '46057422', + '90224632', + '52631437', + '62634608', + '23801966', + '27452675', + '32311266', + '94448560', + '59672227', + '28441574', + '11111112', + ]; + } + + public function test_extract_behaviour(): void + { + $this->expectException(UnsupportedOperationException::class); + + Socrates::getCitizenDataFromId('', 'UY'); + } + + public function test_validation_behaviour(): void + { + foreach ($this->validIds as $id) { + self::assertTrue( + Socrates::validateId($id, 'UY'), + $id + ); + } + + foreach ($this->invalidIds as $id) { + self::assertFalse( + Socrates::validateId($id, 'UY') + ); + } + + $this->expectException(InvalidLengthException::class); + + Socrates::validateId('1234567', 'UY'); + } +} diff --git a/tests/IdValidatorFactoryTest.php b/tests/IdValidatorFactoryTest.php new file mode 100644 index 0000000..24f289f --- /dev/null +++ b/tests/IdValidatorFactoryTest.php @@ -0,0 +1,24 @@ +assertInstanceOf(IdValidator::class, $validator); + } + + /** @test */ + public function it_throws_for_unknown_country(): void + { + $this->expectException(RuntimeException::class); + IdValidatorFactory::getValidator('XX'); + } +} diff --git a/tests/SocratesTest.php b/tests/SocratesTest.php index 26c4a2e..2ef6c33 100644 --- a/tests/SocratesTest.php +++ b/tests/SocratesTest.php @@ -25,18 +25,4 @@ public function it_throws_an_exception_if_a_country_code_is_in_the_wrong_format( Socrates::validateId('123123123', 'ZZZ'); } - - /** @test */ - public function it_uses_the_application_current_locale_if_none_is_provided(): void - { - App::setLocale('PT'); - - $this->assertTrue( - Socrates::validateId('11084129 8 ZX8') - ); - - $this->expectException(UnsupportedOperationException::class); - - Socrates::getCitizenDataFromId('11084129 8 ZX8'); - } }