Генерация объекта документа по сырой json-строке

This commit is contained in:
Anthony Axenov 2020-05-28 00:41:58 +08:00
parent 4fccf7809d
commit 600a505c83
2 changed files with 117 additions and 1 deletions

View File

@ -10,6 +10,8 @@
namespace AtolOnline\Entities; namespace AtolOnline\Entities;
use AtolOnline\Exceptions\AtolCashierTooLongException; 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 * @throws \Exception
*/ */
public function jsonSerialize() public function jsonSerialize()

View File

@ -0,0 +1,32 @@
<?php
/**
* Copyright (c) Антон Аксенов (aka Anthony Axenov)
*
* This code is licensed under MIT.
* Этот код распространяется по лицензии MIT.
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/
namespace AtolOnline\Exceptions;
use Throwable;
/**
* Исключение, возникающее при работе с невалидным JSON
*
* @package AtolOnline\Exceptions
*/
class AtolInvalidJsonException extends AtolException
{
/**
* AtolInnWrongLengthException constructor.
*
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message ?: 'Invalid JSON: ['.json_last_error().'] '.json_last_error_msg(), $code, $previous);
}
}