1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* API for Billing |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/billing-hiapi |
6
|
|
|
* @package billing-hiapi |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\billing\hiapi\bill; |
12
|
|
|
|
13
|
|
|
use DateTimeImmutable; |
14
|
|
|
use hiqdev\php\billing\bill\Bill; |
15
|
|
|
use hiqdev\php\billing\bill\BillState; |
16
|
|
|
use hiqdev\php\billing\charge\ChargeInterface; |
17
|
|
|
use hiqdev\php\billing\customer\Customer; |
18
|
|
|
use hiqdev\php\billing\plan\Plan; |
19
|
|
|
use hiqdev\php\billing\target\Target; |
20
|
|
|
use hiqdev\php\billing\type\Type; |
21
|
|
|
use hiqdev\php\units\Quantity; |
22
|
|
|
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator; |
23
|
|
|
use Money\Money; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Bill Hydrator. |
27
|
|
|
* |
28
|
|
|
* @author Andrii Vasyliev <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class BillHydrator extends GeneratedHydrator |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
* @param object|Bill $object |
35
|
|
|
*/ |
36
|
|
|
public function hydrate(array $row, $object) |
37
|
|
|
{ |
38
|
|
|
$row['type'] = $this->hydrator->create($row['type'], Type::class); |
|
|
|
|
39
|
|
|
$row['time'] = $this->hydrator->create($row['time'], DateTimeImmutable::class); |
40
|
|
|
$row['sum'] = $this->hydrator->create($row['sum'], Money::class); |
41
|
|
|
$row['quantity'] = $this->hydrator->create($row['quantity'], Quantity::class); |
42
|
|
|
$row['customer'] = $this->hydrator->create($row['customer'], Customer::class); |
43
|
|
|
if (isset($row['target'])) { |
44
|
|
|
$row['target'] = $this->hydrator->create($row['target'], Target::class); |
45
|
|
|
} |
46
|
|
|
if (isset($row['plan'])) { |
47
|
|
|
$row['plan'] = $this->hydrator->create($row['plan'], Plan::class); |
48
|
|
|
} |
49
|
|
|
if (isset($row['state'])) { |
50
|
|
|
$row['state'] = $this->hydrator->create($row['state'], BillState::class); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$raw_charges = $row['charges']; |
54
|
|
|
unset($row['charges']); |
55
|
|
|
|
56
|
|
|
/** @var Bill $bill */ |
57
|
|
|
$bill = parent::hydrate($row, $object); |
58
|
|
|
|
59
|
|
|
if (\is_array($raw_charges)) { |
60
|
|
|
$charges = []; |
61
|
|
|
foreach ($raw_charges as $key => $charge) { |
62
|
|
|
if ($charge instanceof ChargeInterface) { |
63
|
|
|
$charge->setBill($bill); |
64
|
|
|
$charges[$key] = $charge; |
65
|
|
|
} else { |
66
|
|
|
$charge['bill'] = $bill; |
67
|
|
|
$charges[$key] = $this->hydrator->hydrate($charge, ChargeInterface::class); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
$bill->setCharges($charges); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $bill; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
* @param object|Bill $object |
79
|
|
|
*/ |
80
|
|
|
public function extract($object) |
81
|
|
|
{ |
82
|
|
|
return array_filter([ |
83
|
|
|
'id' => $object->getId(), |
84
|
|
|
'type' => $this->hydrator->extract($object->getType()), |
85
|
|
|
'time' => $this->hydrator->extract($object->getTime()), |
86
|
|
|
'sum' => $this->hydrator->extract($object->getSum()), |
87
|
|
|
'quantity' => $this->hydrator->extract($object->getQuantity()), |
88
|
|
|
'customer' => $this->hydrator->extract($object->getCustomer()), |
89
|
|
|
'target' => $object->getTarget() ? $this->hydrator->extract($object->getTarget()) : null, |
90
|
|
|
'plan' => $object->getPlan() ? $this->hydrator->extract($object->getPlan()) : null, |
91
|
|
|
'charges' => $this->hydrator->extractAll($object->getCharges()), |
|
|
|
|
92
|
|
|
'state' => $object->getState() ? $this->hydrator->extract($object->getState()) : null, |
93
|
|
|
], static function ($value): bool { |
94
|
|
|
return $value !== null; |
95
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|