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\price; |
12
|
|
|
|
13
|
|
|
use hiqdev\billing\hiapi\models\Price; |
14
|
|
|
use hiqdev\php\billing\formula\FormulaInterface; |
15
|
|
|
use hiqdev\php\billing\plan\Plan; |
16
|
|
|
use hiqdev\php\billing\price\PriceFactoryInterface; |
17
|
|
|
use hiqdev\php\billing\target\Target; |
18
|
|
|
use hiqdev\php\billing\type\Type; |
19
|
|
|
use hiqdev\php\units\Quantity; |
20
|
|
|
use hiqdev\php\units\Unit; |
21
|
|
|
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator; |
22
|
|
|
use Money\Currency; |
23
|
|
|
use Money\Money; |
24
|
|
|
use yii\helpers\Json; |
25
|
|
|
use Zend\Hydrator\HydratorInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class PriceHydrator. |
29
|
|
|
* |
30
|
|
|
* @author Andrii Vasyliev <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class PriceHydrator extends GeneratedHydrator |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var PriceFactoryInterface|PriceFactory |
36
|
|
|
*/ |
37
|
|
|
protected $priceFactory; |
38
|
|
|
|
39
|
3 |
|
public function __construct( |
40
|
|
|
HydratorInterface $hydrator, |
41
|
|
|
PriceFactoryInterface $priceFactory |
42
|
|
|
) { |
43
|
3 |
|
parent::__construct($hydrator); |
44
|
3 |
|
$this->priceFactory = $priceFactory; |
45
|
3 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
* @param object|Plan $object |
50
|
|
|
*/ |
51
|
4 |
|
public function hydrate(array $row, $object) |
52
|
|
|
{ |
53
|
4 |
|
$row['target'] = $this->hydrator->hydrate($row['target'] ?? [], Target::class); |
|
|
|
|
54
|
4 |
|
$row['type'] = $this->hydrator->hydrate($row['type'], Type::class); |
55
|
4 |
|
if (isset($row['prepaid']['unit'])) { |
56
|
4 |
|
$row['unit'] = Unit::create($row['prepaid']['unit']); |
57
|
|
|
} |
58
|
4 |
|
if (isset($row['unit'], $row['prepaid']['quantity'])) { |
59
|
2 |
|
$row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']); |
60
|
|
|
} |
61
|
4 |
|
if (isset($row['price']['currency'])) { |
62
|
4 |
|
$row['currency'] = new Currency(strtoupper($row['price']['currency'])); |
63
|
|
|
} |
64
|
4 |
|
if (isset($row['currency'], $row['price']['amount'])) { |
65
|
2 |
|
$row['price'] = new Money($row['price']['amount'], $row['currency']); |
66
|
|
|
} |
67
|
4 |
|
if (!empty($row['plan'])) { |
68
|
|
|
$row['plan'] = $this->hydrator->create($row['plan'], Plan::class); |
|
|
|
|
69
|
|
|
} |
70
|
4 |
|
if (isset($row['data'])) { |
71
|
2 |
|
$data = is_array($row['data']) ? $row['data'] : Json::decode($row['data']); |
72
|
|
|
} |
73
|
4 |
|
if (!empty($data['formula'])) { |
74
|
|
|
$row['modifier'] = $this->hydrator->hydrate([$data['formula']], FormulaInterface::class); |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
$row['sums'] = empty($data['sums']) ? [] : $data['sums']; |
78
|
4 |
|
$row['subprices'] = $data['subprices'] ?? null; |
79
|
|
|
|
80
|
4 |
|
return parent::hydrate($row, $object); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
* @param object|Price $object |
86
|
|
|
*/ |
87
|
|
|
public function extract($object) |
88
|
|
|
{ |
89
|
|
|
return array_filter([ |
90
|
|
|
'id' => $object->getId(), |
|
|
|
|
91
|
|
|
'type' => $this->hydrator->extract($object->getType()), |
|
|
|
|
92
|
|
|
'target' => $this->hydrator->extract($object->getTarget()), |
|
|
|
|
93
|
|
|
'plan' => $object->getPlan() ? $this->hydrator->extract($object->getPlan()) : null, |
|
|
|
|
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $className |
99
|
|
|
* @param array $data |
100
|
|
|
* @throws \ReflectionException |
101
|
|
|
* @return object |
102
|
|
|
*/ |
103
|
2 |
|
public function createEmptyInstance(string $className, array $data = []) |
|
|
|
|
104
|
|
|
{ |
105
|
2 |
|
if (isset($data['data']) && !is_array($data['data'])) { |
106
|
1 |
|
$additionalData = Json::decode($data['data']); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
$className = $this->priceFactory->findClassForTypes([ |
110
|
2 |
|
$additionalData['class'] ?? null, |
111
|
2 |
|
$data['type']['name'], |
112
|
|
|
]); |
113
|
|
|
|
114
|
2 |
|
return parent::createEmptyInstance($className, $data); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|