From 95dbc3a5b74a82298e3b7e71a8af983e1eb59fb7 Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Wed, 24 Nov 2021 18:54:48 +0800 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BB=D0=B0=D1=81=D1=81=20`Supplier`=20?= =?UTF-8?q?=D0=BE=D0=B1=D0=B7=D0=B0=D0=B2=D1=91=D0=BB=D1=81=D1=8F=20`name`?= =?UTF-8?q?=20+=20`inn`=20=D0=B8=20=D0=B4=D0=BE=D0=BF=D0=BE=D0=BA=D1=80?= =?UTF-8?q?=D1=8B=D1=82=20=D1=82=D0=B5=D1=81=D1=82=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Constants/Ffd105Tags.php | 17 +++- src/Entities/Supplier.php | 76 ++++++++++++++++- src/Exceptions/InvalidPhoneException.php | 2 + .../Tests/Entities/SupplierTest.php | 85 ++++++++++++++++++- 4 files changed, 173 insertions(+), 7 deletions(-) diff --git a/src/Constants/Ffd105Tags.php b/src/Constants/Ffd105Tags.php index c7b71de..1cd7c86 100644 --- a/src/Constants/Ffd105Tags.php +++ b/src/Constants/Ffd105Tags.php @@ -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; + /** * ИНН поставщика */ diff --git a/src/Entities/Supplier.php b/src/Entities/Supplier.php index 4d58d08..3852885 100644 --- a/src/Entities/Supplier.php +++ b/src/Entities/Supplier.php @@ -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; } diff --git a/src/Exceptions/InvalidPhoneException.php b/src/Exceptions/InvalidPhoneException.php index a2ad97f..27552d5 100644 --- a/src/Exceptions/InvalidPhoneException.php +++ b/src/Exceptions/InvalidPhoneException.php @@ -22,6 +22,8 @@ class InvalidPhoneException extends AtolException Ffd105Tags::CLIENT_CONTACT, Ffd105Tags::PAGENT_PHONE, Ffd105Tags::RPO_PHONES, + Ffd105Tags::MTO_PHONES, + Ffd105Tags::SUPPLIER_PHONES, ]; /** diff --git a/tests/AtolOnline/Tests/Entities/SupplierTest.php b/tests/AtolOnline/Tests/Entities/SupplierTest.php index d0df501..57fb253 100644 --- a/tests/AtolOnline/Tests/Entities/SupplierTest.php +++ b/tests/AtolOnline/Tests/Entities/SupplierTest.php @@ -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'); } }