|
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-2018, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\price; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\php\billing\plan\PlanInterface; |
|
14
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
|
15
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
|
16
|
|
|
use hiqdev\php\units\QuantityInterface; |
|
17
|
|
|
use hiqdev\php\units\UnitInterface; |
|
18
|
|
|
use Money\Currency; |
|
19
|
|
|
use Money\Money; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Enum Price: |
|
23
|
|
|
* - holds sums list: amount => total sum for the quantity NOT price per unit |
|
24
|
|
|
* - listed quantities only else exception. |
|
25
|
|
|
* @see PriceInterface |
|
26
|
|
|
* |
|
27
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class EnumPrice extends AbstractPrice |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var UnitInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $unit; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Currency |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $currency; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array quantity => total sum for the quantity |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $sums; |
|
45
|
|
|
|
|
46
|
2 |
|
public function __construct( |
|
47
|
|
|
$id, |
|
48
|
|
|
TypeInterface $type, |
|
49
|
|
|
TargetInterface $target, |
|
50
|
|
|
PlanInterface $plan = null, |
|
51
|
|
|
UnitInterface $unit, |
|
52
|
|
|
Currency $currency, |
|
53
|
|
|
array $sums |
|
54
|
|
|
) { |
|
55
|
2 |
|
parent::__construct($id, $type, $target, $plan); |
|
56
|
2 |
|
$this->unit = $unit; |
|
57
|
2 |
|
$this->currency = $currency; |
|
58
|
2 |
|
$this->sums = $sums; |
|
59
|
2 |
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
public function getUnit() |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->unit; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function getCurrency() |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->currency; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function getSums() |
|
72
|
|
|
{ |
|
73
|
1 |
|
return $this->sums; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
4 |
|
public function calculateSum(QuantityInterface $quantity): ?Money |
|
80
|
|
|
{ |
|
81
|
4 |
|
$usage = $this->calculateUsage($quantity)->getQuantity(); |
|
82
|
|
|
|
|
83
|
4 |
|
foreach ($this->sums as $value => $price) { |
|
84
|
4 |
|
if ((string) $value === (string) $usage) { |
|
85
|
4 |
|
return new Money($price, $this->currency); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
throw new FailedCalculatePriceException('not enumed quantity: ' . $usage); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function calculatePrice(QuantityInterface $quantity): ?Money |
|
96
|
|
|
{ |
|
97
|
|
|
$sum = $this->calculateSum($quantity); |
|
98
|
|
|
if ($sum === null) { |
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$usage = $this->calculateUsage($quantity); |
|
103
|
|
|
if ($usage === null) { |
|
104
|
|
|
return null; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $sum->divide($usage->getQuantity()); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
4 |
|
public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface |
|
114
|
|
|
{ |
|
115
|
4 |
|
return $quantity->convert($this->unit); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function jsonSerialize() |
|
119
|
|
|
{ |
|
120
|
|
|
return array_merge(parent::jsonSerialize(), [ |
|
|
|
|
|
|
121
|
|
|
'currency' => $this->currency, |
|
122
|
|
|
'sums' => $this->sums, |
|
123
|
|
|
'unit' => $this->unit, |
|
124
|
|
|
]); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.