Генерация объекта документа по сырой json-строке
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user