Skip to content

Commit

Permalink
Merge branch 'release/v1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy IIIFX Khomenko committed Dec 4, 2016
2 parents f367e07 + ad038f4 commit a59a6a6
Show file tree
Hide file tree
Showing 5 changed files with 523 additions and 151 deletions.
62 changes: 52 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Identification Number: Ukraine

Парсер идентификационного номера налогоплательщика Украины
Идентификационный номер налогоплательщика Украины. Парсер и генератор ИНН.

[![SensioLabsInsight](https://insight.sensiolabs.com/projects/746be262-9725-4d22-9ce1-e7eb07dc4858/big.png)](https://insight.sensiolabs.com/projects/746be262-9725-4d22-9ce1-e7eb07dc4858)

Expand All @@ -17,19 +17,61 @@ $ composer require "iiifx-production/ukraine-identification-number:0.*"

## Использование

Парсер ИНН:

``` php
use iiifx\Identification\Ukraine\Parser;

# Создаем парсер ИНН
$parser = Parser::create( '0123456789' );
# Номер ИНН
$number = '2245134075';

# Создаем парсер
$parser = Parser::create( $number );
# Или так
$parser = new Parser( $number );

# Проверяем правильность ИНН
if ( $parser->isValidNumber() ) {

$parser->getNumber(); # 2245134075

# Определяем пол владельца ИНН
$parser->getPersonSex(); # Parser::SEX_MALE
$parser->isPersonMale(); # true
$parser->isPersonFemale(); # false

# Определяем возраст и дату рождения
$parser->getPersonAge(); # 55
$parser->getPersonBirth( 'Y-m-d' ); # 1961-06-20
$parser->getPersonBirthDatetime()->format( 'd.m.Y H:i:s' ); # 20.06.1961 00:00:00

# Контрольная сумма и число
$parser->getControlSumm(); # 192
$parser->getControlDigit(); # 5
}
```

Генератор ИНН:

``` php
use iiifx\Identification\Ukraine\Builder;

# Создаем генератор
$builder = new Builder();
# Или вот так
$builder = Builder::create( Builder::SEX_MALE, new DateTime( '2010-05-12' ) );

# Указывам пол
$builder->setPersonSex( Builder::SEX_MALE );
$builder->setPersonMale();
$builder->setPersonFemale();

# Указываем возраст
$builder->setPersonAge( 55 );
$builder->setPersonBirthDatetime( new DateTime( '1962-11-03' ) );

# Получаем все данные
$parser->getNumber(); # '0123456789'
$parser->isValid(); # true
$parser->getSex(); # 'M'
$parser->isMale(); # true
$parser->getAge(); # 32
$parser->getBirthDatetime()->format( 'd.m.Y' ); # '02.05.1984'
# Генерируем ИНН
$builder->createNumber(); # 2295209520
```

## Тесты
Expand Down
180 changes: 178 additions & 2 deletions source/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,186 @@

namespace iiifx\Identification\Ukraine;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use InvalidArgumentException;

class Builder
{
public function __construct ()
/**
* Male
*/
const SEX_MALE = 1;

/**
* Female
*/
const SEX_FEMALE = 2;

/**
* @var int
*/
protected $personSex;

/**
* @var DateTimeImmutable
*/
protected $birthDatetime;

/**
* @param int|null $sex
* @param DateTimeInterface|null $datetime
*
* @return static
*/
public static function create ( $sex = null, DateTimeInterface $datetime = null )
{
$builder = new static();
if ( $sex ) {
$builder->setPersonSex( $sex );
}
if ( $datetime ) {
$builder->setPersonBirthDatetime( $datetime );
}
return $builder;
}

/**
* @param $sex
*
* @return $this
*
* @throws InvalidArgumentException
*/
public function setPersonSex ( $sex )
{
$allow = [
static::SEX_MALE,
static::SEX_FEMALE,
];
if ( in_array( $sex, $allow, true ) ) {
$this->personSex = $sex;
return $this;
}
throw new InvalidArgumentException( '$sex must be Builder::SEX_MALE or Builder::SEX_FEMALE' );
}

/**
* @return $this
*/
public function setPersonMale ()
{
$this->personSex = static::SEX_MALE;
return $this;
}

/**
* @return $this
*/
public function setPersonFemale ()
{
$this->personSex = static::SEX_FEMALE;
return $this;
}

/**
* @param int $age
*
* @return $this
*
* @throws InvalidArgumentException
*/
public function setPersonAge ( $age )
{
if ( is_int( $age ) && $age >= 0 ) {
$datetime = ( new DateTimeImmutable( 'now' ) )->modify( "-{$age} years -1 day" );
return $this->setPersonBirthDatetime( $datetime );
}
throw new InvalidArgumentException( '$age must be positive integer' );
}

/**
* @param DateTimeInterface $datetime
*
* @return $this
*
* @throws InvalidArgumentException
*/
public function setPersonBirthDatetime ( DateTimeInterface $datetime )
{
if ( $datetime->format( 'Y' ) >= 1900 ) {
if ( !$datetime instanceof DateTimeImmutable ) {
$datetime = new DateTimeImmutable( $datetime->format( 'Y-m-d H:i:s' ) );
}
$this->birthDatetime = $datetime;
return $this;
}
throw new InvalidArgumentException( 'Birthday must be equal or greater than 1900-01-01' );
}

/**
* Format: DDDDDXXXSC
*
* DDDDD - Days from 1900-01-01
* XXX - Any nambers
* S - Person sex
* C - Control digit
*
* @return string
*/
public function createNumber ()
{
if ( $this->birthDatetime ) {
$days = (int) $this->birthDatetime
->diff( new DateTime( '1900-01-01 00:00:00' ) )
->days;
} else {
$max = (int) ( new DateTime( '1900-01-01 00:00:00' ) )
->diff( new DateTime() )
->days;
$days = mt_rand( 0, $max );
}
$DDDDD = sprintf( '%1$05d', $days + 2 );
$XXX = sprintf( '%1$03d', mt_rand( 0, 999 ) );
if ( !( $sex = $this->personSex ) ) {
$list = [ static::SEX_MALE => 0, static::SEX_FEMALE => 0 ];
$sex = array_rand( $list );
}
if ( $sex === static::SEX_FEMALE ) {
$list = [ 0 => 0, 2 => 0, 4 => 0, 6 => 0, 8 => 0 ];
} else {
$list = [ 1 => 0, 3 => 0, 5 => 0, 7 => 0, 9 => 0 ];
}
$S = array_rand( $list );
$number = "{$DDDDD}{$XXX}{$S}";
$summ =
( $number{0} * -1 ) +
( $number{1} * 5 ) +
( $number{2} * 7 ) +
( $number{3} * 9 ) +
( $number{4} * 4 ) +
( $number{5} * 6 ) +
( $number{6} * 10 ) +
( $number{7} * 5 ) +
( $number{8} * 7 );
$C = ( $summ % 11 ) % 10;
return "{$number}{$C}";
}

/**
* @return int|null
*/
public function getPersonSex ()
{
return $this->personSex;
}

/**
* @return DateTimeImmutable|null
*/
public function getPersonBirthDatetime ()
{
throw new \LogicException( 'Not implemented' );
return $this->birthDatetime;
}
}
Loading

0 comments on commit a59a6a6

Please sign in to comment.