mirror of
https://github.com/anthonyaxenov/atol-online.git
synced 2024-12-22 07:04:54 +00:00
Класс Supplier
обзавёлся name
+ inn
и допокрыт тестами
This commit is contained in:
parent
790831e933
commit
95dbc3a5b7
@ -17,7 +17,7 @@ final class Ffd105Tags
|
||||
/**
|
||||
* Телефон или электронный адрес покупателя
|
||||
*/
|
||||
const CLIENT_CONTACT = 1008;
|
||||
const CLIENT_PHONE_EMAIL = 1008;
|
||||
|
||||
/**
|
||||
* Наименование организации или фамилия, имя, отчество (при наличии), серия и номер паспорта покупателя (клиента)
|
||||
@ -49,6 +49,11 @@ final class Ffd105Tags
|
||||
*/
|
||||
const RPO_PHONES = 1074;
|
||||
|
||||
/**
|
||||
* Телефон оператора перевода
|
||||
*/
|
||||
const MTO_PHONES = 1075;
|
||||
|
||||
/**
|
||||
* ИНН оператора перевода
|
||||
*/
|
||||
@ -59,6 +64,16 @@ final class Ffd105Tags
|
||||
*/
|
||||
const PAGENT_PHONE = 1073;
|
||||
|
||||
/**
|
||||
* Телефон поставщика
|
||||
*/
|
||||
const SUPPLIER_PHONES = 1171;
|
||||
|
||||
/**
|
||||
* Наименование поставщика
|
||||
*/
|
||||
const SUPPLIER_NAME = 1225;
|
||||
|
||||
/**
|
||||
* ИНН поставщика
|
||||
*/
|
||||
|
@ -12,6 +12,7 @@ declare(strict_types = 1);
|
||||
namespace AtolOnline\Entities;
|
||||
|
||||
use AtolOnline\Constants\Constraints;
|
||||
use AtolOnline\Exceptions\InvalidInnLengthException;
|
||||
use AtolOnline\Exceptions\InvalidPhoneException;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
@ -23,22 +24,62 @@ use Illuminate\Support\Collection;
|
||||
class Supplier extends Entity
|
||||
{
|
||||
/**
|
||||
* @var Collection Телефоны платёжного агента (1073)
|
||||
* @var string|null Наименование (1225)
|
||||
*/
|
||||
protected ?string $name = null;
|
||||
|
||||
/**
|
||||
* @var string|null ИНН (1226)
|
||||
*/
|
||||
protected ?string $inn = null;
|
||||
|
||||
/**
|
||||
* @var Collection Телефоны (1171)
|
||||
*/
|
||||
protected Collection $phones;
|
||||
|
||||
/**
|
||||
* Конструктор
|
||||
*
|
||||
* @param array|Collection|null $phones Телефон оператора по приёму платежей (1074)
|
||||
* @param string|null $name Наименование поставщика (1225)
|
||||
* @param string|null $inn ИНН (1226)
|
||||
* @param array|Collection|null $phones Телефоны поставщика (1171)
|
||||
* @throws InvalidInnLengthException
|
||||
* @throws InvalidPhoneException
|
||||
*/
|
||||
public function __construct(
|
||||
?string $name = null,
|
||||
?string $inn = null,
|
||||
array|Collection|null $phones = null,
|
||||
) {
|
||||
!is_null($name) && $this->setName($name);
|
||||
!is_null($inn) && $this->setInn($inn);
|
||||
$this->setPhones($phones);
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает установленное наименование поставщика
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Устанавливает наименование поставщика
|
||||
*
|
||||
* @param string|null $name
|
||||
* @return Supplier
|
||||
*/
|
||||
public function setName(?string $name): self
|
||||
{
|
||||
// критерии к длине строки не описаны ни в схеме, ни в документации
|
||||
$this->name = trim($name) ?: null;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает установленные номера телефонов
|
||||
*
|
||||
@ -73,12 +114,43 @@ class Supplier extends Entity
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает установленный ИНН
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getInn(): ?string
|
||||
{
|
||||
return $this->inn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Устанавливает ИНН
|
||||
*
|
||||
* @param string|null $inn
|
||||
* @return $this
|
||||
* @throws InvalidInnLengthException Некорректная длина ИНН
|
||||
*/
|
||||
public function setInn(?string $inn): self
|
||||
{
|
||||
if (is_string($inn)) {
|
||||
$inn = preg_replace('/[^\d]/', '', trim($inn));
|
||||
if (preg_match_all(Constraints::PATTERN_INN, $inn) === 0) {
|
||||
throw new InvalidInnLengthException($inn);
|
||||
}
|
||||
}
|
||||
$this->inn = empty($inn) ? null : $inn;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
$json = [];
|
||||
$this->getName() && $json['name'] = $this->getName();
|
||||
$this->getInn() && $json['inn'] = $this->getInn();
|
||||
!$this->getPhones()->isEmpty() && $json['phones'] = $this->getPhones()->toArray();
|
||||
return $json;
|
||||
}
|
||||
|
@ -22,6 +22,8 @@ class InvalidPhoneException extends AtolException
|
||||
Ffd105Tags::CLIENT_CONTACT,
|
||||
Ffd105Tags::PAGENT_PHONE,
|
||||
Ffd105Tags::RPO_PHONES,
|
||||
Ffd105Tags::MTO_PHONES,
|
||||
Ffd105Tags::SUPPLIER_PHONES,
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -11,6 +11,7 @@ namespace AtolOnline\Tests\Entities;
|
||||
|
||||
use AtolOnline\{
|
||||
Entities\Supplier,
|
||||
Exceptions\InvalidInnLengthException,
|
||||
Exceptions\InvalidPhoneException,
|
||||
Tests\BasicTestCase};
|
||||
|
||||
@ -35,13 +36,45 @@ class SupplierTest extends BasicTestCase
|
||||
*
|
||||
* @covers \AtolOnline\Entities\Supplier
|
||||
* @covers \AtolOnline\Entities\Supplier::jsonSerialize
|
||||
* @covers \AtolOnline\Entities\Supplier::setName
|
||||
* @covers \AtolOnline\Entities\Supplier::getName
|
||||
* @covers \AtolOnline\Entities\Supplier::setPhones
|
||||
* @covers \AtolOnline\Entities\Supplier::getPhones
|
||||
* @covers \AtolOnline\Entities\Supplier::setInn
|
||||
* @covers \AtolOnline\Entities\Supplier::getInn
|
||||
* @throws InvalidPhoneException
|
||||
* @throws InvalidInnLengthException
|
||||
*/
|
||||
public function testConstructorWithArgs(): void
|
||||
{
|
||||
$this->assertAtolable(new Supplier(['+122997365456']), ['phones' => ['+122997365456']]);
|
||||
$this->assertAtolable(new Supplier('some name'), ['name' => 'some name']);
|
||||
$this->assertAtolable(new Supplier(inn: '+fasd3\qe3fs_=nac99013928czc'), ['inn' => '3399013928']);
|
||||
$this->assertAtolable(new Supplier(phones: ['+122997365456']), ['phones' => ['+122997365456']]);
|
||||
$this->assertAtolable(new Supplier(
|
||||
'some name',
|
||||
'+fasd3\qe3fs_=nac99013928czc',
|
||||
['+122997365456'],
|
||||
), [
|
||||
'name' => 'some name',
|
||||
'inn' => '3399013928',
|
||||
'phones' => ['+122997365456'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Тестирует установку имён, которые приводятся к null
|
||||
*
|
||||
* @param mixed $name
|
||||
* @dataProvider providerNullableStrings
|
||||
* @covers \AtolOnline\Entities\Supplier
|
||||
* @covers \AtolOnline\Entities\Supplier::setName
|
||||
* @covers \AtolOnline\Entities\Supplier::getName
|
||||
* @throws InvalidPhoneException
|
||||
* @throws InvalidInnLengthException
|
||||
*/
|
||||
public function testNullableOperations(mixed $name): void
|
||||
{
|
||||
$this->assertNull((new Supplier($name))->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -66,10 +99,11 @@ class SupplierTest extends BasicTestCase
|
||||
* @covers \AtolOnline\Entities\Supplier::setPhones
|
||||
* @covers \AtolOnline\Entities\Supplier::getPhones
|
||||
* @throws InvalidPhoneException
|
||||
* @throws InvalidInnLengthException
|
||||
*/
|
||||
public function testNullablePhones(mixed $phones): void
|
||||
{
|
||||
$agent = new Supplier($phones);
|
||||
$agent = new Supplier(phones: $phones);
|
||||
$this->assertIsCollection($agent->getPhones());
|
||||
$this->assertTrue($agent->getPhones()->isEmpty());
|
||||
}
|
||||
@ -81,15 +115,58 @@ class SupplierTest extends BasicTestCase
|
||||
* @covers \AtolOnline\Entities\Supplier::setPhones
|
||||
* @covers \AtolOnline\Exceptions\InvalidPhoneException
|
||||
* @throws InvalidPhoneException
|
||||
* @throws InvalidInnLengthException
|
||||
*/
|
||||
public function testInvalidPhoneException(): void
|
||||
{
|
||||
$this->expectException(InvalidPhoneException::class);
|
||||
(new Supplier())->setPhones([
|
||||
(new Supplier(phones: [
|
||||
'12345678901234567', // good
|
||||
'+123456789012345678', // good
|
||||
'12345678901234567890', // bad
|
||||
'+12345678901234567890', // bad
|
||||
]);
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Тестирует исключение о корректной длине ИНН
|
||||
*
|
||||
* @covers \AtolOnline\Entities\Supplier
|
||||
* @covers \AtolOnline\Entities\Supplier::setInn
|
||||
* @covers \AtolOnline\Entities\Supplier::getInn
|
||||
* @throws InvalidInnLengthException
|
||||
*/
|
||||
public function testValidInn(): void
|
||||
{
|
||||
$this->assertEquals('1234567890', (new Supplier())->setInn('1234567890')->getInn());
|
||||
$this->assertEquals('123456789012', (new Supplier())->setInn('123456789012')->getInn());
|
||||
}
|
||||
|
||||
/**
|
||||
* Тестирует исключение о некорректной длине ИНН (10 цифр)
|
||||
*
|
||||
* @covers \AtolOnline\Entities\Supplier
|
||||
* @covers \AtolOnline\Entities\Supplier::setInn
|
||||
* @covers \AtolOnline\Exceptions\InvalidInnLengthException
|
||||
* @throws InvalidInnLengthException
|
||||
*/
|
||||
public function testInvalidInn10(): void
|
||||
{
|
||||
$this->expectException(InvalidInnLengthException::class);
|
||||
(new Supplier())->setInn('12345678901');
|
||||
}
|
||||
|
||||
/**
|
||||
* Тестирует исключение о некорректной длине ИНН (12 цифр)
|
||||
*
|
||||
* @covers \AtolOnline\Entities\Supplier
|
||||
* @covers \AtolOnline\Entities\Supplier::setInn
|
||||
* @covers \AtolOnline\Exceptions\InvalidInnLengthException
|
||||
* @throws InvalidInnLengthException
|
||||
*/
|
||||
public function testInvalidInn12(): void
|
||||
{
|
||||
$this->expectException(InvalidInnLengthException::class);
|
||||
(new Supplier())->setInn('1234567890123');
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user