From 12b98dcdac813c2b34df4accfedbc6b0c8bd99b5 Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Thu, 28 May 2020 22:55:27 +0800 Subject: [PATCH] =?UTF-8?q?Document::fromRaw()=20=D1=82=D0=B5=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D1=8C=20=D0=BF=D0=B0=D1=80=D1=81=D0=B8=D1=82=20=D0=B2?= =?UTF-8?q?=D1=85=D0=BE=D0=B4=D1=8F=D1=89=D0=B8=D0=B9=20json=20=D0=BA?= =?UTF-8?q?=D0=B0=D0=BA=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2,=20=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=B5=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AtolOnline/Entities/Document.php | 69 +++++++++++++--------------- 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/src/AtolOnline/Entities/Document.php b/src/AtolOnline/Entities/Document.php index 302812a..5e98211 100644 --- a/src/AtolOnline/Entities/Document.php +++ b/src/AtolOnline/Entities/Document.php @@ -338,60 +338,57 @@ class Document extends Entity */ public static function fromRaw(string $json) { - $object = json_decode($json); + $array = json_decode($json, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new AtolInvalidJsonException(); } $doc = new self(); - if ($object->company) { + if (isset($array['company'])) { $doc->setCompany(new Company( - $object->company->sno ?? null, - $object->company->inn ?? null, - $object->company->payment_address ?? null, - $object->company->email ?? null + $array['company']['sno'] ?? null, + $array['company']['inn'] ?? null, + $array['company']['payment_address'] ?? null, + $array['company']['email'] ?? null )); } - if ($object->client) { + if (isset($array['client'])) { $doc->setClient(new Client( - $object->client->name ?? null, - $object->client->phone ?? null, - $object->client->email ?? null, - $object->client->inn ?? null + $array['client']['name'] ?? null, + $array['client']['phone'] ?? null, + $array['client']['email'] ?? null, + $array['client']['inn'] ?? null )); } - if ($object->items) { - foreach ($object->items as $obj_item) { + if (isset($array['items'])) { + foreach ($array['items'] as $ar_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 + $ar_item['name'] ?? null, + $ar_item['price'] ?? null, + $ar_item['quantity'] ?? null, + $ar_item['measurement_unit'] ?? null, + $ar_item['vat']['type'] ?? null, + $ar_item['payment_object'] ?? null, + $ar_item['payment_method'] ?? null ); - if (!empty($obj_item->user_data)) { - $item->setUserData($obj_item->user_data ?? null); + if (!empty($ar_item['user_data'])) { + $item->setUserData($ar_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 (isset($array['payments'])) { + foreach ($array['payments'] as $ar_payment) { + $payment = new Payment(); + if (isset($ar_payment['type'])) { + $payment->setType($ar_payment['type']); + } + if (isset($ar_payment['sum'])) { + $payment->setSum($ar_payment['sum']); + } + $doc->payments->add($payment); } } - //if ($object->vats) { - // foreach ($object->vats as $obj_vat) { - // $doc->vats->add(new Vat( - // $obj_vat->type - // )); - // } - //} - if ($object->total != $doc->calcTotal()) { + if ($array['total'] != $doc->calcTotal()) { throw new AtolException('Real total sum not equals to provided in JSON one'); } return $doc;