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, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\action; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\charge\Charge; |
14
|
|
|
use hiqdev\php\billing\price\PriceInterface; |
15
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
16
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
17
|
|
|
use hiqdev\php\billing\customer\CustomerInterface; |
18
|
|
|
use DateTime; |
19
|
|
|
use hiqdev\php\units\QuantityInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Chargable Action. |
23
|
|
|
* |
24
|
|
|
* @see ActionInterface |
25
|
|
|
* |
26
|
|
|
* @author Andrii Vasyliev <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractAction implements ActionInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $id; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var TypeInterface |
37
|
|
|
*/ |
38
|
|
|
protected $type; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var TargetInterface |
42
|
|
|
*/ |
43
|
|
|
protected $target; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var QuantityInterface |
47
|
|
|
*/ |
48
|
|
|
protected $quantity; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var CustomerInterface |
52
|
|
|
*/ |
53
|
|
|
protected $customer; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var DateTime |
57
|
|
|
*/ |
58
|
|
|
protected $time; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param CustomerInterface $customer |
62
|
|
|
* @param TargetInterface $target |
63
|
|
|
* @param QuantityInterface $quantity |
64
|
|
|
* @param DateTime $time |
65
|
|
|
*/ |
66
|
3 |
|
public function __construct( |
67
|
|
|
$id, |
68
|
|
|
TypeInterface $type, |
69
|
|
|
TargetInterface $target, |
70
|
|
|
QuantityInterface $quantity, |
71
|
|
|
CustomerInterface $customer = null, |
72
|
|
|
DateTime $time = null |
73
|
|
|
) { |
74
|
3 |
|
$this->id = $id; |
75
|
3 |
|
$this->type = $type; |
76
|
3 |
|
$this->target = $target; |
77
|
3 |
|
$this->quantity = $quantity; |
78
|
3 |
|
$this->customer = $customer; |
79
|
3 |
|
$this->time = $time; |
80
|
3 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function getCustomer() |
86
|
|
|
{ |
87
|
|
|
return $this->customer; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getTarget() |
94
|
|
|
{ |
95
|
|
|
return $this->target; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
3 |
|
public function getType() |
102
|
|
|
{ |
103
|
3 |
|
return $this->type; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
3 |
|
public function getQuantity() |
110
|
|
|
{ |
111
|
3 |
|
return $this->quantity; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function getTime() |
118
|
|
|
{ |
119
|
|
|
return $this->time; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function jsonSerialize() |
126
|
|
|
{ |
127
|
|
|
return get_object_vars($this); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
3 |
|
public function calculateCharge(PriceInterface $price) |
134
|
|
|
{ |
135
|
3 |
|
if (!$this->isApplicable($price)) { |
136
|
1 |
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
3 |
|
$usage = $price->calculateUsage($this->getQuantity()); |
140
|
3 |
|
if ($usage === null) { |
141
|
1 |
|
return null; |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
$sum = $price->calculateSum($this->getQuantity()); |
145
|
2 |
|
if ($sum === null) { |
146
|
|
|
return null; |
147
|
|
|
} |
148
|
|
|
|
149
|
2 |
|
return new Charge(null, $this, $price->getType(), $price->getTarget(), $usage, $sum); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
|
|
abstract public function isApplicable(PriceInterface $price); |
156
|
|
|
} |
157
|
|
|
|
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.