From d533164d1b80988703d403ca7ed4f8a9725b50f8 Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Fri, 3 Dec 2021 20:09:14 +0800 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6?= =?UTF-8?q?=D0=BA=D0=B0=20`correction=5Finfo`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Constants/Constraints.php | 5 + src/Constants/Ffd105Tags.php | 5 + src/Entities/Company.php | 8 +- src/Entities/CorrectionInfo.php | 157 ++++++++++++++++++ src/Entities/todo_CorrectionInfo.php | 134 --------------- src/Enums/Enum.php | 3 +- .../EmptyCorrectionNumberException.php | 23 +++ .../InvalidCorrectionDateException.php | 33 ++++ .../Tests/Entities/CorrectionInfoTest.php | 106 ++++++++++++ 9 files changed, 332 insertions(+), 142 deletions(-) create mode 100644 src/Entities/CorrectionInfo.php delete mode 100644 src/Entities/todo_CorrectionInfo.php create mode 100644 src/Exceptions/EmptyCorrectionNumberException.php create mode 100644 src/Exceptions/InvalidCorrectionDateException.php create mode 100644 tests/AtolOnline/Tests/Entities/CorrectionInfoTest.php diff --git a/src/Constants/Constraints.php b/src/Constants/Constraints.php index aa4bc0e..1606bbd 100644 --- a/src/Constants/Constraints.php +++ b/src/Constants/Constraints.php @@ -142,6 +142,11 @@ final class Constraints */ const MAX_LENGTH_ITEM_CODE = 32; + /** + * Формат даты документа коррекции + */ + const CORRECTION_DATE_FORMAT = 'd.m.Y'; + /** * Регулярное выражение для валидации строки ИНН * diff --git a/src/Constants/Ffd105Tags.php b/src/Constants/Ffd105Tags.php index e7ed2b0..32a015a 100644 --- a/src/Constants/Ffd105Tags.php +++ b/src/Constants/Ffd105Tags.php @@ -162,6 +162,11 @@ final class Ffd105Tags */ const CORRECTION_TYPE = 1173; + /** + * Дата документа основания для коррекции + */ + const CORRECTION_DATE = 1178; + /** * Сумма по чеку (БСО) наличными */ diff --git a/src/Entities/Company.php b/src/Entities/Company.php index 9ce6bfe..1470d7d 100644 --- a/src/Entities/Company.php +++ b/src/Entities/Company.php @@ -66,10 +66,7 @@ final class Company extends Entity string $inn, string $payment_address, ) { - $this->setEmail($email); - $this->setSno($sno); - $this->setInn($inn); - $this->setPaymentAddress($payment_address); + $this->setEmail($email)->setSno($sno)->setInn($inn)->setPaymentAddress($payment_address); } /** @@ -92,8 +89,7 @@ final class Company extends Entity public function setSno(string $sno): self { $sno = trim($sno); - SnoTypes::isValid($sno); - $this->sno = $sno; + SnoTypes::isValid($sno) && $this->sno = $sno; return $this; } diff --git a/src/Entities/CorrectionInfo.php b/src/Entities/CorrectionInfo.php new file mode 100644 index 0000000..5579c94 --- /dev/null +++ b/src/Entities/CorrectionInfo.php @@ -0,0 +1,157 @@ +setType($type)->setDate($date)->setNumber($number); + } + + /** + * Возвращает тип коррекции + * + * @return string|null + */ + public function getType(): ?string + { + return $this->type; + } + + /** + * Устанавливает тип коррекции + * + * @param string $type + * @return $this + * @throws InvalidEnumValueException + */ + public function setType(string $type): self + { + $type = trim($type); + CorrectionTypes::isValid($type) && $this->type = $type; + return $this; + } + + /** + * Возвращает дату документа основания для коррекции + * + * @return string|null + */ + public function getDate(): ?string + { + return $this->date; + } + + /** + * Устанавливает дату документа основания для коррекции + * + * @param DateTime|string $date Строковая дата в формате d.m.Y либо объект DateTime с датой + * @return $this + * @throws InvalidCorrectionDateException + */ + public function setDate(DateTime|string $date): self + { + try { + if (is_string($date)) { + $date = new DateTime(trim($date)); + } + $this->date = $date->format(Constraints::CORRECTION_DATE_FORMAT); + } catch (Exception $e) { + throw new InvalidCorrectionDateException($date, $e->getMessage()); + } + return $this; + } + + /** + * Возвращает установленный номер документа основания для коррекции + * + * @return string|null + */ + public function getNumber(): ?string + { + return $this->number; + } + + /** + * Устанавливает номер документа основания для коррекции + * + * @param string $number + * @return $this + * @throws EmptyCorrectionNumberException + */ + public function setNumber(string $number): self + { + $number = trim($number); + empty($number) && throw new EmptyCorrectionNumberException(); + $this->number = $number; + return $this; + } + + /** + * @inheritDoc + */ + #[Pure] + #[ArrayShape(['type' => 'string', 'base_date' => 'string', 'base_number' => 'string'])] + public function jsonSerialize(): array + { + return [ + 'type' => $this->getType(), + 'base_date' => $this->getDate(), + 'base_number' => $this->getNumber(), + ]; + } +} diff --git a/src/Entities/todo_CorrectionInfo.php b/src/Entities/todo_CorrectionInfo.php deleted file mode 100644 index f404f17..0000000 --- a/src/Entities/todo_CorrectionInfo.php +++ /dev/null @@ -1,134 +0,0 @@ -setType($type); - $base_date && $this->setDate($base_date); - $base_number && $this->setNumber($base_number); - } - - /** - * Возвращает номер документа основания для коррекции. - * Тег ФФД - 1179. - * - * @return string|null - */ - public function getNumber(): ?string - { - return $this->base_number; - } - - /** - * Устанавливает номер документа основания для коррекции. - * Тег ФФД - 1179. - * - * @param string $number - * @return $this - */ - public function setNumber(string $number): todoCorrectionInfo - { - $this->base_number = trim($number); - return $this; - } - - /** - * Возвращает дату документа основания для коррекции. - * Тег ФФД - 1178. - * - * @return string|null - */ - public function getDate(): ?string - { - return $this->base_date; - } - - /** - * Устанавливает дату документа основания для коррекции. - * Тег ФФД - 1178. - * - * @param string $date Строка в формате d.m.Y - * @return $this - */ - public function setDate(string $date): todoCorrectionInfo - { - $this->base_date = $date; - return $this; - } - - /** - * Возвращает тип коррекции. - * Тег ФФД - 1173. - * - * @return string|null - */ - public function getType(): ?string - { - return $this->type; - } - - /** - * Устанавливает тип коррекции. - * Тег ФФД - 1173. - * - * @param string $type - * @return $this - */ - public function setType(string $type): todoCorrectionInfo - { - $this->type = $type; - return $this; - } - - /** - * @inheritDoc - */ - public function jsonSerialize(): array - { - return [ - 'type' => $this->getType() ?? '', - 'base_date' => $this->getDate() ?? '', - 'base_number' => $this->getNumber() ?? '', - ]; - } -} diff --git a/src/Enums/Enum.php b/src/Enums/Enum.php index 84803ab..3a178f0 100644 --- a/src/Enums/Enum.php +++ b/src/Enums/Enum.php @@ -24,8 +24,7 @@ abstract class Enum extends \MyCLabs\Enum\Enum */ public static function isValid($value) { - return parent::isValid($value) - ?: throw new InvalidEnumValueException(static::class, $value); + return parent::isValid($value) ?: throw new InvalidEnumValueException(static::class, $value); } /** diff --git a/src/Exceptions/EmptyCorrectionNumberException.php b/src/Exceptions/EmptyCorrectionNumberException.php new file mode 100644 index 0000000..1dedbb0 --- /dev/null +++ b/src/Exceptions/EmptyCorrectionNumberException.php @@ -0,0 +1,23 @@ +assertAtolable( + new CorrectionInfo(CorrectionTypes::SELF, '01.01.2021', $number = Helpers::randomStr()), + [ + 'type' => CorrectionTypes::SELF, + 'base_date' => '01.01.2021', + 'base_number' => $number, + ] + ); + } + + /** + * Тестирует исключение при некорректном типе + * + * @covers \AtolOnline\Entities\CorrectionInfo + * @covers \AtolOnline\Entities\CorrectionInfo::setType + * @covers \AtolOnline\Enums\CorrectionTypes::isValid + * @covers \AtolOnline\Exceptions\InvalidEnumValueException + * @return void + * @throws EmptyCorrectionNumberException + * @throws InvalidCorrectionDateException + * @throws InvalidEnumValueException + */ + public function testInvalidEnumValueException(): void + { + $this->expectException(InvalidEnumValueException::class); + $this->expectExceptionMessage('Некорректное значение AtolOnline\Enums\CorrectionTypes::wrong_value'); + new CorrectionInfo('wrong_value', '01.01.2021', Helpers::randomStr()); + } + + /** + * Тестирует исключение при некорректной дате + * + * @covers \AtolOnline\Entities\CorrectionInfo + * @covers \AtolOnline\Entities\CorrectionInfo::setDate + * @covers \AtolOnline\Enums\CorrectionTypes::isValid + * @covers \AtolOnline\Exceptions\InvalidCorrectionDateException + * @return void + * @throws EmptyCorrectionNumberException + * @throws InvalidCorrectionDateException + * @throws InvalidEnumValueException + */ + public function testInvalidCorrectionDateException(): void + { + $this->expectException(InvalidCorrectionDateException::class); + new CorrectionInfo(CorrectionTypes::SELF, Helpers::randomStr(), Helpers::randomStr()); + } + + /** + * Тестирует исключение при некорректной дате + * + * @covers \AtolOnline\Entities\CorrectionInfo + * @covers \AtolOnline\Entities\CorrectionInfo::setNumber + * @covers \AtolOnline\Enums\CorrectionTypes::isValid + * @covers \AtolOnline\Exceptions\EmptyCorrectionNumberException + * @return void + */ + public function testEmptyCorrectionNumberException(): void + { + $this->expectException(EmptyCorrectionNumberException::class); + new CorrectionInfo(CorrectionTypes::SELF, '01.01.2021', "\n\r\t\0"); + } +}