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\plan\PlanInterface; |
14
|
|
|
use hiqdev\php\billing\price\SinglePrice; |
15
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
16
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
17
|
|
|
use hiqdev\php\units\QuantityInterface; |
18
|
|
|
use Money\Money; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class TemplatePrice. |
22
|
|
|
* |
23
|
|
|
* @author Dmytro Naumenko <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class TemplatePrice extends SinglePrice |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Money[] |
29
|
|
|
*/ |
30
|
|
|
protected $subprices = []; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
$id, |
34
|
|
|
TypeInterface $type, |
35
|
|
|
TargetInterface $target, |
36
|
|
|
PlanInterface $plan = null, |
37
|
|
|
QuantityInterface $prepaid, |
38
|
|
|
Money $price, |
39
|
|
|
array $subprices |
40
|
|
|
) { |
41
|
|
|
parent::__construct($id, $type, $target, $plan, $prepaid, $price); |
42
|
|
|
|
43
|
|
|
$this->subprices = $subprices; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function jsonSerialize() |
47
|
|
|
{ |
48
|
|
|
return array_merge(parent::jsonSerialize(), [ |
|
|
|
|
49
|
|
|
'subprices' => $this->subprices, |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return Money[] |
55
|
|
|
*/ |
56
|
|
|
public function getSubprices(): array |
57
|
|
|
{ |
58
|
|
|
return $this->subprices; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $currencyCode |
63
|
|
|
* @return string the subprice in decimal format (e.g. `10.25`) |
64
|
|
|
*/ |
65
|
|
|
public function getSubprice($currencyCode): string |
66
|
|
|
{ |
67
|
|
|
return $this->subprices[strtoupper($currencyCode)] ?? '0'; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.