1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP Billing Library |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/php-billing |
6
|
|
|
* @package php-billing |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\charge; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\bill\Bill; |
14
|
|
|
use hiqdev\php\billing\bill\BillInterface; |
15
|
|
|
use hiqdev\php\billing\customer\CustomerInterface; |
16
|
|
|
use hiqdev\php\billing\plan\PlanInterface; |
17
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
18
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
19
|
|
|
use hiqdev\php\units\QuantityInterface; |
20
|
|
|
use Money\Money; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Andrii Vasyliev <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Generalizer implements GeneralizerInterface |
26
|
|
|
{ |
27
|
1 |
|
public function createBill(ChargeInterface $charge): BillInterface |
28
|
|
|
{ |
29
|
1 |
|
return new Bill( |
30
|
1 |
|
null, |
31
|
1 |
|
$this->generalizeType($charge), |
32
|
1 |
|
$this->generalizeTime($charge), |
33
|
1 |
|
$this->generalizeSum($charge), |
34
|
1 |
|
$this->generalizeQuantity($charge), |
35
|
1 |
|
$this->generalizeCustomer($charge), |
36
|
1 |
|
$this->generalizeTarget($charge), |
37
|
1 |
|
$this->generalizePlan($charge), |
38
|
1 |
|
[$charge] |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function generalizeType(ChargeInterface $charge): TypeInterface |
43
|
|
|
{ |
44
|
1 |
|
return $charge->getType(); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function generalizeTime(ChargeInterface $charge): \DateTimeImmutable |
48
|
|
|
{ |
49
|
1 |
|
return $charge->getAction()->getTime(); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function generalizeSum(ChargeInterface $charge): Money |
53
|
|
|
{ |
54
|
1 |
|
return $charge->getSum()->negative(); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
public function generalizeQuantity(ChargeInterface $charge): QuantityInterface |
58
|
|
|
{ |
59
|
1 |
|
return $charge->getUsage(); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
public function generalizeCustomer(ChargeInterface $charge): CustomerInterface |
63
|
|
|
{ |
64
|
1 |
|
return $charge->getAction()->getCustomer(); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function generalizeTarget(ChargeInterface $charge): TargetInterface |
68
|
|
|
{ |
69
|
1 |
|
return $charge->getTarget(); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function generalizePlan(ChargeInterface $charge): ?PlanInterface |
73
|
|
|
{ |
74
|
1 |
|
return $charge->getPrice()->getPlan(); |
75
|
|
|
} |
76
|
|
|
|
77
|
17 |
|
public function specializeType(TypeInterface $first, TypeInterface $other): TypeInterface |
78
|
|
|
{ |
79
|
17 |
|
return $first; |
80
|
|
|
} |
81
|
|
|
|
82
|
17 |
|
public function specializeTarget(TargetInterface $first, TargetInterface $other): TargetInterface |
83
|
|
|
{ |
84
|
17 |
|
return $first; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|