Document::fromRaw() теперь парсит входящий json как массив, а не объект

pull/2/head
Anthony Axenov 2020-05-28 22:55:27 +08:00
parent 1fec446ce0
commit 12b98dcdac
1 changed files with 33 additions and 36 deletions

View File

@ -338,60 +338,57 @@ class Document extends Entity
*/ */
public static function fromRaw(string $json) public static function fromRaw(string $json)
{ {
$object = json_decode($json); $array = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) { if (json_last_error() !== JSON_ERROR_NONE) {
throw new AtolInvalidJsonException(); throw new AtolInvalidJsonException();
} }
$doc = new self(); $doc = new self();
if ($object->company) { if (isset($array['company'])) {
$doc->setCompany(new Company( $doc->setCompany(new Company(
$object->company->sno ?? null, $array['company']['sno'] ?? null,
$object->company->inn ?? null, $array['company']['inn'] ?? null,
$object->company->payment_address ?? null, $array['company']['payment_address'] ?? null,
$object->company->email ?? null $array['company']['email'] ?? null
)); ));
} }
if ($object->client) { if (isset($array['client'])) {
$doc->setClient(new Client( $doc->setClient(new Client(
$object->client->name ?? null, $array['client']['name'] ?? null,
$object->client->phone ?? null, $array['client']['phone'] ?? null,
$object->client->email ?? null, $array['client']['email'] ?? null,
$object->client->inn ?? null $array['client']['inn'] ?? null
)); ));
} }
if ($object->items) { if (isset($array['items'])) {
foreach ($object->items as $obj_item) { foreach ($array['items'] as $ar_item) {
$item = new Item( $item = new Item(
$obj_item->name ?? null, $ar_item['name'] ?? null,
$obj_item->price ?? null, $ar_item['price'] ?? null,
$obj_item->quantity ?? null, $ar_item['quantity'] ?? null,
$obj_item->measurement_unit ?? null, $ar_item['measurement_unit'] ?? null,
$obj_item->vat->type ?? null, $ar_item['vat']['type'] ?? null,
$obj_item->payment_object ?? null, $ar_item['payment_object'] ?? null,
$obj_item->payment_method ?? null $ar_item['payment_method'] ?? null
); );
if (!empty($obj_item->user_data)) { if (!empty($ar_item['user_data'])) {
$item->setUserData($obj_item->user_data ?? null); $item->setUserData($ar_item['user_data'] ?? null);
} }
$doc->addItem($item); $doc->addItem($item);
} }
} }
if ($object->payments) { if (isset($array['payments'])) {
foreach ($object->payments as $obj_payment) { foreach ($array['payments'] as $ar_payment) {
$doc->payments->add(new Payment( $payment = new Payment();
$obj_payment->type, if (isset($ar_payment['type'])) {
$obj_payment->sum $payment->setType($ar_payment['type']);
)); }
if (isset($ar_payment['sum'])) {
$payment->setSum($ar_payment['sum']);
}
$doc->payments->add($payment);
} }
} }
//if ($object->vats) { if ($array['total'] != $doc->calcTotal()) {
// 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'); throw new AtolException('Real total sum not equals to provided in JSON one');
} }
return $doc; return $doc;