From 04307c554e91706d787776a35058a3cc89b57901 Mon Sep 17 00:00:00 2001 From: Vitaliy IIIFX Khomenko Date: Tue, 29 Nov 2016 16:54:36 +0200 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=82=D0=BE=D1=82=D0=B8=D0=BF,=20=D0=BF=D0=B0?= =?UTF-8?q?=D1=80=D1=81=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 25 ++++-- composer.json | 13 ++- source/.gitkeep | 1 - source/Builder.php | 11 +++ source/Parser.php | 213 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 250 insertions(+), 13 deletions(-) delete mode 100644 source/.gitkeep create mode 100644 source/Builder.php create mode 100644 source/Parser.php diff --git a/README.md b/README.md index 451b8af..5125436 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,36 @@ -# ukraine identification number +# Identification Number: Ukraine -... +Парсер идентификационного номера налогоплательщика Украины ## Установка Используя Composer: ``` bash -$ composer require "...:1.*" +$ composer require "iiifx-production/ukraine-identification-number:0.*" ``` ## Использование -... +``` php +use iiifx\Identification\Ukraine\Parser; -## Примеры +# Создаем парсер ИНН +$parser = Parser::create( '0123456789' ); -... +# Получаем все данные +$parser->getNumber(); # '0123456789' +$parser->isValid(); # true +$parser->getSex(); # 'M' +$parser->isMale(); # true +$parser->getAge(); # 32 +$parser->getBirthDatetime()->format( 'd.m.Y' ); # '02.05.1984' +``` ## Тесты -... +@TODO ## Лицензия -... +MIT diff --git a/composer.json b/composer.json index cf47dcd..284da86 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,13 @@ { "name": "iiifx-production/ukraine-identification-number", "type": "extension", - "description": "...", + "description": "Ukraine Identification ", "keywords": [ - "..." + "Ukraine", + "INN", + "VAT", + "Identification", + "Number" ], "homepage": "https://github.com/iiifx-production/ukraine-identification-number", "license": "MIT", @@ -21,7 +25,7 @@ }, "autoload": { "psr-4": { - "iiifx\\LazyInit\\": "source/" + "iiifx\\Identification\\Ukraine\\": "source/" } }, "scripts": { @@ -29,7 +33,8 @@ }, "extra": { "branch-alias": { - "dev-master": "1.x-dev" + "dev-develop": "0.x-dev", + "dev-master": "0.x-dev" } } } diff --git a/source/.gitkeep b/source/.gitkeep deleted file mode 100644 index f59ec20..0000000 --- a/source/.gitkeep +++ /dev/null @@ -1 +0,0 @@ -* \ No newline at end of file diff --git a/source/Builder.php b/source/Builder.php new file mode 100644 index 0000000..73e74c3 --- /dev/null +++ b/source/Builder.php @@ -0,0 +1,11 @@ +number = $number; + } + + /** + * @return int|string + */ + public function getNumber () + { + return $this->number; + } + + /** + * Определить корректный ли номер ИНН + * + * @return bool + */ + public function isValid () + { + if ( $this->valid === null ) { + if ( $this->parseNumber() ) { + $this->valid = $this->getControlDigit() === (int) $this->parts[ 9 ]; + } + } + return $this->valid; + } + + /** + * Получить пол владельца номера ИНН + * + * @return string + */ + public function getSex () + { + if ( $this->sex === null ) { + $this->sex = false; + if ( $this->isValid() ) { + $this->sex = ( $this->parts[ 8 ] % 2 ) ? 'M' : 'F'; + } + } + return $this->sex; + } + + /** + * Мужчина ли владелец номера ИНН + * + * @return bool + */ + public function isMale () + { + return $this->getSex() === 'M'; + } + + /** + * Женщина ли владелец номера ИНН + * + * @return bool + */ + public function isFemale () + { + return $this->getSex() === 'F'; + } + + /** + * Получить возраст владельца номера ИНН + * + * @param DateTimeInterface|null $now + * + * @return int + */ + public function getAge ( DateTimeInterface $now = null ) + { + if ( $now === null ) { + $now = new DateTime( 'now', new DateTimeZone( 'UTC' ) ); + $now->modify( 'midnight' ); + } + return (int) $now->diff( $this->getBirthDatetime() )->y; + } + + /** + * Получить дату рождения владельца ИНН + * + * @return DateTimeImmutable|false + */ + public function getBirthDatetime () + { + if ( $this->datetime === null ) { + $this->datetime = false; + if ( $this->isValid() ) { + $days = substr( $this->number, 0, 5 ) - 1; + $datetime = new DateTimeImmutable( + '1900-01-01 00:00:00', + new DateTimeZone( 'UTC' ) + ); + $this->datetime = $datetime->modify( "+ {$days} days" ); + } + } + return $this->datetime; + } + + /** + * Распарсить номер ИНН + * + * @return bool + */ + protected function parseNumber () + { + if ( $this->parts === null ) { + $this->parts = []; + if ( preg_match( '/^\d{10}$/', $this->number ) === 1 ) { + $this->parts = str_split( $this->number ); + } + } + return (bool) $this->parts; + } + + /** + * Получить контрольное число номера ИНН + * + * @return int + */ + protected function getControlDigit () + { + if ( $this->control === null ) { + $this->control = false; + if ( $this->parseNumber() ) { + $summ = + $this->parts[ 0 ] * -1 + + $this->parts[ 1 ] * 5 + + $this->parts[ 2 ] * 7 + + $this->parts[ 3 ] * 9 + + $this->parts[ 4 ] * 4 + + $this->parts[ 5 ] * 6 + + $this->parts[ 6 ] * 10 + + $this->parts[ 7 ] * 5 + + $this->parts[ 8 ] * 7; + $this->control = $summ % 11; + } + } + return $this->control; + } +}