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\plan; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\customer\Customer; |
14
|
|
|
use hiqdev\php\billing\plan\Plan; |
15
|
|
|
use hiqdev\php\billing\price\PriceInterface; |
16
|
|
|
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class PlanHydrator. |
20
|
|
|
* |
21
|
|
|
* @author Andrii Vasyliev <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class PlanHydrator extends GeneratedHydrator |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
* @param object|Plan $object |
28
|
|
|
* @throws \Exception |
29
|
|
|
*/ |
30
|
|
|
public function hydrate(array $data, $object) |
31
|
|
|
{ |
32
|
|
|
if (!empty($data['seller'])) { |
33
|
|
|
$data['seller'] = $this->hydrator->hydrate($data['seller'], Customer::class); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
$raw_prices = $data['prices']; |
36
|
|
|
unset($data['prices']); |
37
|
|
|
|
38
|
|
|
/** @var Plan $plan */ |
39
|
|
|
$plan = parent::hydrate($data, $object); |
40
|
|
|
|
41
|
|
|
if (is_array($raw_prices)) { |
42
|
|
|
$prices = []; |
43
|
|
|
foreach ($raw_prices as $key => $price) { |
44
|
|
|
if ($price instanceof PriceInterface) { |
45
|
|
|
$price->setPlan($plan); |
46
|
|
|
$prices[$key] = $price; |
47
|
|
|
} else { |
48
|
|
|
$price['plan'] = $plan; |
49
|
|
|
$prices[$key] = $this->hydrator->hydrate($price, PriceInterface::class); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
$plan->setPrices($prices); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $plan; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
* @param Plan $object |
61
|
|
|
*/ |
62
|
|
|
public function extract($object) |
63
|
|
|
{ |
64
|
|
|
$result = array_filter([ |
65
|
|
|
'id' => $object->getId(), |
66
|
|
|
'name' => $object->getName(), |
67
|
|
|
'seller' => $object->getSeller() ? $this->hydrator->extract($object->getSeller()) : null, |
68
|
|
|
'parent' => $object->getParent() ? $this->hydrator->extract($object->getParent()) : null, |
69
|
|
|
'is_grouping' => $object instanceof GroupingPlan, |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $className |
77
|
|
|
* @param array $data |
78
|
|
|
* @throws \ReflectionException |
79
|
|
|
* @return object |
80
|
|
|
*/ |
81
|
|
|
public function createEmptyInstance(string $className, array $data = []) |
82
|
|
|
{ |
83
|
|
|
if (isset($data['is_grouping']) && $data['is_grouping'] === true) { |
84
|
|
|
$className = GroupingPlan::class; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return parent::createEmptyInstance($className, $data); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|