From 600a505c83bb363b9d59f9e6c70b9ddef3c6a247 Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Thu, 28 May 2020 00:41:58 +0800 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=B0=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BA=D1=83=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D1=81=D1=8B=D1=80=D0=BE=D0=B9=20json-=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=BE=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AtolOnline/Entities/Document.php | 86 ++++++++++++++++++- .../Exceptions/AtolInvalidJsonException.php | 32 +++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/AtolOnline/Exceptions/AtolInvalidJsonException.php diff --git a/src/AtolOnline/Entities/Document.php b/src/AtolOnline/Entities/Document.php index 1a6d71b..302812a 100644 --- a/src/AtolOnline/Entities/Document.php +++ b/src/AtolOnline/Entities/Document.php @@ -10,6 +10,8 @@ namespace AtolOnline\Entities; use AtolOnline\Exceptions\AtolCashierTooLongException; +use AtolOnline\Exceptions\AtolException; +use AtolOnline\Exceptions\AtolInvalidJsonException; /** * Класс, описывающий документ @@ -315,7 +317,89 @@ class Document extends Entity } /** - * @inheritDoc + * Собирает объект документа из сырой json-строки + * + * @param string $json + * @return \AtolOnline\Entities\Document + * @throws \AtolOnline\Exceptions\AtolEmailTooLongException + * @throws \AtolOnline\Exceptions\AtolEmailValidateException + * @throws \AtolOnline\Exceptions\AtolException + * @throws \AtolOnline\Exceptions\AtolInnWrongLengthException + * @throws \AtolOnline\Exceptions\AtolInvalidJsonException + * @throws \AtolOnline\Exceptions\AtolNameTooLongException + * @throws \AtolOnline\Exceptions\AtolPaymentAddressTooLongException + * @throws \AtolOnline\Exceptions\AtolPhoneTooLongException + * @throws \AtolOnline\Exceptions\AtolPriceTooHighException + * @throws \AtolOnline\Exceptions\AtolTooManyException + * @throws \AtolOnline\Exceptions\AtolTooManyItemsException + * @throws \AtolOnline\Exceptions\AtolTooManyPaymentsException + * @throws \AtolOnline\Exceptions\AtolUnitTooLongException + * @throws \AtolOnline\Exceptions\AtolUserdataTooLongException + */ + public static function fromRaw(string $json) + { + $object = json_decode($json); + if (json_last_error() !== JSON_ERROR_NONE) { + throw new AtolInvalidJsonException(); + } + $doc = new self(); + if ($object->company) { + $doc->setCompany(new Company( + $object->company->sno ?? null, + $object->company->inn ?? null, + $object->company->payment_address ?? null, + $object->company->email ?? null + )); + } + if ($object->client) { + $doc->setClient(new Client( + $object->client->name ?? null, + $object->client->phone ?? null, + $object->client->email ?? null, + $object->client->inn ?? null + )); + } + if ($object->items) { + foreach ($object->items as $obj_item) { + $item = new Item( + $obj_item->name ?? null, + $obj_item->price ?? null, + $obj_item->quantity ?? null, + $obj_item->measurement_unit ?? null, + $obj_item->vat->type ?? null, + $obj_item->payment_object ?? null, + $obj_item->payment_method ?? null + ); + if (!empty($obj_item->user_data)) { + $item->setUserData($obj_item->user_data ?? null); + } + $doc->addItem($item); + } + } + if ($object->payments) { + foreach ($object->payments as $obj_payment) { + $doc->payments->add(new Payment( + $obj_payment->type, + $obj_payment->sum + )); + } + } + //if ($object->vats) { + // foreach ($object->vats as $obj_vat) { + // $doc->vats->add(new Vat( + // $obj_vat->type + // )); + // } + //} + if ($object->total != $doc->calcTotal()) { + throw new AtolException('Real total sum not equals to provided in JSON one'); + } + return $doc; + } + + /** + * Возвращает массив для кодирования в json + * * @throws \Exception */ public function jsonSerialize() diff --git a/src/AtolOnline/Exceptions/AtolInvalidJsonException.php b/src/AtolOnline/Exceptions/AtolInvalidJsonException.php new file mode 100644 index 0000000..4c53a5a --- /dev/null +++ b/src/AtolOnline/Exceptions/AtolInvalidJsonException.php @@ -0,0 +1,32 @@ +