|
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'] ?? null; |
|
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
|
|
|
], static function ($value): bool { |
|
71
|
|
|
return $value !== null; |
|
72
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
|
73
|
|
|
|
|
74
|
|
|
return $result; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $className |
|
79
|
|
|
* @param array $data |
|
80
|
|
|
* @throws \ReflectionException |
|
81
|
|
|
* @return object |
|
82
|
|
|
*/ |
|
83
|
|
|
public function createEmptyInstance(string $className, array $data = []) |
|
84
|
|
|
{ |
|
85
|
|
|
if (isset($data['is_grouping']) && $data['is_grouping'] === true) { |
|
86
|
|
|
$className = GroupingPlan::class; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return parent::createEmptyInstance($className, $data); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|