Skip to content

Commit

Permalink
Первый прототип, парсер
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy IIIFX Khomenko committed Nov 29, 2016
1 parent 8264a9d commit 04307c5
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 13 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -21,15 +25,16 @@
},
"autoload": {
"psr-4": {
"iiifx\\LazyInit\\": "source/"
"iiifx\\Identification\\Ukraine\\": "source/"
}
},
"scripts": {
"test": "phpunit tests"
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
"dev-develop": "0.x-dev",
"dev-master": "0.x-dev"
}
}
}
1 change: 0 additions & 1 deletion source/.gitkeep

This file was deleted.

11 changes: 11 additions & 0 deletions source/Builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace iiifx\Identification\Ukraine;

class Builder
{
public function __construct ()
{
throw new \LogicException( 'Not implemented' );
}
}
213 changes: 213 additions & 0 deletions source/Parser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
<?php

namespace iiifx\Identification\Ukraine;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;

class Parser
{
/**
* Номер ИНН
*
* @var string
*/
protected $number;

/**
* Части номера, по цифрам
*
* @var string[]
*/
protected $parts;

/**
* Корректный ли номер ИНН
*
* @var bool
*/
protected $valid;

/**
* Контрольное число
*
* @var int|false
*/
protected $control;

/**
* Пол владельца
*
* @var string|false
*/
protected $sex;

/**
* Дата рождения владельца
*
* @var DateTimeImmutable|false
*/
protected $datetime;

/**
* Фабрика
*
* @param string|int $number
*
* @return Parser
*/
public static function create ( $number )
{
return new Parser( $number );
}

/**
* Базовый конструктор
*
* @param string|int $number
*/
public function __construct ( $number )
{
$this->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;
}
}

0 comments on commit 04307c5

Please sign in to comment.