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\charge; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\action\Action; |
14
|
|
|
use hiqdev\php\billing\bill\Bill; |
15
|
|
|
use hiqdev\php\billing\charge\Charge; |
16
|
|
|
use hiqdev\php\billing\charge\ChargeInterface; |
17
|
|
|
use hiqdev\php\billing\charge\ChargeState; |
18
|
|
|
use hiqdev\php\billing\price\PriceInterface; |
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
|
|
|
* Charge Hydrator. |
27
|
|
|
* |
28
|
|
|
* @author Andrii Vasyliev <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class ChargeHydrator extends GeneratedHydrator |
31
|
|
|
{ |
32
|
|
|
/** {@inheritdoc} */ |
33
|
|
|
public function hydrate(array $data, $object) |
34
|
|
|
{ |
35
|
|
|
$data['type'] = $this->hydrator->create($data['type'], Type::class); |
|
|
|
|
36
|
|
|
$data['target'] = $this->hydrator->create($data['target'], Target::class); |
37
|
|
|
$data['action'] = $this->hydrator->create($data['action'], Action::class); |
38
|
|
|
$data['usage'] = $this->hydrator->create($data['usage'], Quantity::class); |
39
|
|
|
$data['sum'] = $this->hydrator->create($data['sum'], Money::class); |
40
|
|
|
if (isset($data['price'])) { |
41
|
|
|
$data['price'] = $this->hydrator->create($data['price'], PriceInterface::class); |
42
|
|
|
} |
43
|
|
|
if (isset($data['bill'])) { |
44
|
|
|
if (\count($data['bill']) > 1) { // If relation is actually populated |
45
|
|
|
$data['bill'] = $this->hydrator->create($data['bill'], Bill::class); |
46
|
|
|
} else { |
47
|
|
|
unset($data['bill']); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
if (isset($data['state'])) { |
51
|
|
|
$data['state'] = $this->hydrator->create($data['state'], ChargeState::class); |
52
|
|
|
} |
53
|
|
|
if (isset($data['parent'])) { |
54
|
|
|
if (\count($data['parent']) > 1) { // If relation is actually populated |
55
|
|
|
$data['parent'] = $this->hydrate($data['parent'], $this->createEmptyInstance(ChargeInterface::class)); |
56
|
|
|
} else { |
57
|
|
|
unset($data['parent']); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return parent::hydrate($data, $object); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
* @param object|Charge $object |
67
|
|
|
*/ |
68
|
|
|
public function extract($object) |
69
|
|
|
{ |
70
|
|
|
$result = array_filter([ |
71
|
|
|
'id' => $object->getId(), |
72
|
|
|
'type' => $this->hydrator->extract($object->getType()), |
73
|
|
|
'target' => $this->hydrator->extract($object->getTarget()), |
74
|
|
|
'action' => $this->hydrator->extract($object->getAction()), |
75
|
|
|
'price' => $this->hydrator->extract($object->getPrice()), |
76
|
|
|
'usage' => $this->hydrator->extract($object->getUsage()), |
77
|
|
|
'sum' => $this->hydrator->extract($object->getSum()), |
78
|
|
|
'bill' => $object->getBill() ? $this->hydrator->extract($object->getBill()) : null, |
79
|
|
|
'state' => $object->getState() ? $this->hydrator->extract($object->getState()) : null, |
80
|
|
|
'comment' => $object->getComment(), |
81
|
|
|
], static function ($value): bool { |
82
|
|
|
return $value !== null; |
83
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
84
|
|
|
|
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $className |
90
|
|
|
* @param array $data |
91
|
|
|
* @throws \ReflectionException |
92
|
|
|
* @return object |
93
|
|
|
*/ |
94
|
|
|
public function createEmptyInstance(string $className, array $data = []) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
return parent::createEmptyInstance(Charge::class, $data); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|