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\Plan; |
14
|
|
|
use hiqdev\php\billing\formula\FormulaInterface; |
15
|
|
|
use hiqdev\php\billing\price\PriceFactoryInterface; |
16
|
|
|
use hiqdev\php\billing\target\Target; |
17
|
|
|
use hiqdev\php\billing\type\Type; |
18
|
|
|
use hiqdev\php\units\Quantity; |
19
|
|
|
use hiqdev\php\units\Unit; |
20
|
|
|
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator; |
21
|
|
|
use Money\Currency; |
22
|
|
|
use Money\Money; |
23
|
|
|
use yii\helpers\Json; |
24
|
|
|
use Zend\Hydrator\HydratorInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class PriceHydrator. |
28
|
|
|
* |
29
|
|
|
* @author Andrii Vasyliev <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class PriceHydrator extends GeneratedHydrator |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var PriceFactoryInterface|PriceFactory |
35
|
|
|
*/ |
36
|
|
|
protected $priceFactory; |
37
|
|
|
|
38
|
3 |
|
public function __construct( |
39
|
|
|
HydratorInterface $hydrator, |
40
|
|
|
PriceFactoryInterface $priceFactory |
41
|
|
|
) { |
42
|
3 |
|
parent::__construct($hydrator); |
43
|
3 |
|
$this->priceFactory = $priceFactory; |
44
|
3 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
* @param object|Plan $object |
49
|
|
|
*/ |
50
|
4 |
|
public function hydrate(array $row, $object) |
51
|
|
|
{ |
52
|
4 |
|
$row['target'] = $this->hydrator->hydrate($row['target'], Target::class); |
53
|
4 |
|
$row['type'] = $this->hydrator->hydrate($row['type'], Type::class); |
54
|
4 |
|
if (isset($row['prepaid']['unit'])) { |
55
|
4 |
|
$row['unit'] = Unit::create($row['prepaid']['unit']); |
56
|
|
|
} |
57
|
4 |
|
if (isset($row['unit']) && isset($row['prepaid']['quantity'])) { |
58
|
2 |
|
$row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']); |
|
|
|
|
59
|
|
|
} |
60
|
4 |
|
if (isset($row['price']['currency'])) { |
61
|
4 |
|
$row['currency'] = new Currency(strtoupper($row['price']['currency'])); |
62
|
|
|
} |
63
|
4 |
|
if (isset($row['currency']) && isset($row['price']['amount'])) { |
64
|
2 |
|
$row['price'] = new Money($row['price']['amount'], $row['currency']); |
65
|
|
|
} |
66
|
4 |
View Code Duplication |
if (isset($row['data'])) { |
|
|
|
|
67
|
2 |
|
$data = is_array($row['data']) ? $row['data'] : Json::decode($row['data']); |
|
|
|
|
68
|
|
|
} |
69
|
4 |
|
if (isset($data['formula'])) { |
70
|
|
|
$row['formula'] = $this->hydrator->hydrate([$data['formula']], FormulaInterface::class); |
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
$row['sums'] = empty($data['sums']) ? [] : $data['sums']; |
74
|
4 |
|
$row['subprices'] = $data['subprices'] ?? null; |
75
|
|
|
|
76
|
4 |
|
return parent::hydrate($row, $object); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
* @param object|Plan $object |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function extract($object) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$result = array_filter([ |
86
|
|
|
'id' => $object->getId(), |
87
|
|
|
'name' => $object->getName(), |
88
|
|
|
'parent_id' => $object->parent->getid(), |
89
|
|
|
'seller_id' => $object->seller->getid(), |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
return $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $className |
97
|
|
|
* @param array $data |
98
|
|
|
* @throws \ReflectionException |
99
|
|
|
* @return object |
100
|
|
|
*/ |
101
|
2 |
|
public function createEmptyInstance(string $className, array $data = []) |
102
|
|
|
{ |
103
|
2 |
View Code Duplication |
if (isset($data['data']) && !is_array($data['data'])) { |
|
|
|
|
104
|
1 |
|
$additionalData = Json::decode($data['data']); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
$className = $this->priceFactory->findClassForTypes([ |
|
|
|
|
108
|
2 |
|
$additionalData['class'] ?? null, |
109
|
2 |
|
$data['type']['name'], |
110
|
|
|
]); |
111
|
|
|
|
112
|
2 |
|
return parent::createEmptyInstance($className, $data); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: