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\php\billing\price\PriceFactoryInterface; |
14
|
|
|
use hiqdev\php\billing\target\Target; |
15
|
|
|
use hiqdev\php\billing\type\Type; |
16
|
|
|
use hiqdev\php\units\Quantity; |
17
|
|
|
use hiqdev\php\units\Unit; |
18
|
|
|
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator; |
19
|
|
|
use Money\Currency; |
20
|
|
|
use Money\Money; |
21
|
|
|
use yii\helpers\Json; |
22
|
|
|
use Zend\Hydrator\HydratorInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class PriceHydrator. |
26
|
|
|
* |
27
|
|
|
* @author Andrii Vasyliev <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class PriceHydrator extends GeneratedHydrator |
30
|
|
|
{ |
31
|
|
|
protected $priceFactory; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
HydratorInterface $hydrator, |
35
|
|
|
PriceFactoryInterface $priceFactory |
36
|
|
|
) { |
37
|
|
|
parent::__construct($hydrator); |
38
|
|
|
$this->priceFactory = $priceFactory; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
* @param object|Plan $object |
44
|
|
|
*/ |
45
|
|
|
public function hydrate(array $row, $object) |
46
|
|
|
{ |
47
|
|
|
$row['target'] = $this->hydrator->hydrate($row['target'], Target::class); |
48
|
|
|
$row['type'] = $this->hydrator->hydrate($row['type'], Type::class); |
49
|
|
|
if (isset($row['prepaid']['unit'])) { |
50
|
|
|
$row['unit'] = Unit::create($row['prepaid']['unit']); |
51
|
|
|
} |
52
|
|
|
if (isset($row['unit']) && isset($row['prepaid']['quantity'])) { |
53
|
|
|
$row['prepaid'] = Quantity::create($row['unit'], $row['prepaid']['quantity']); |
54
|
|
|
} |
55
|
|
|
if (isset($row['price']['currency'])) { |
56
|
|
|
$row['currency'] = new Currency(strtoupper($row['price']['currency'])); |
57
|
|
|
} |
58
|
|
|
if (isset($row['currency']) && isset($row['price']['amount'])) { |
59
|
|
|
$row['price'] = new Money($row['price']['amount'], $row['currency']); |
60
|
|
|
} |
61
|
|
|
if (isset($row['data'])) { |
62
|
|
|
$data = Json::decode($row['data']); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
$row['sums'] = empty($data['sums']) ? [] : $data['sums']; |
65
|
|
|
|
66
|
|
|
return parent::hydrate($row, $object); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
* @param object|Plan $object |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function extract($object) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$result = array_filter([ |
76
|
|
|
'id' => $object->getId(), |
77
|
|
|
'name' => $object->getName(), |
78
|
|
|
'parent_id' => $object->parent->getid(), |
79
|
|
|
'seller_id' => $object->seller->getid(), |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
return $result; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $className |
87
|
|
|
* @throws \ReflectionException |
88
|
|
|
* @return object |
89
|
|
|
*/ |
90
|
|
|
public function createEmptyInstance(string $className, array $data = []) |
91
|
|
|
{ |
92
|
|
|
$className = $this->priceFactory->findClassForTypes([$data['type']['name']]); |
93
|
|
|
|
94
|
|
|
return parent::createEmptyInstance($className, $data); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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: