1 | <?php |
||
18 | abstract class AbstractQuantity implements QuantityInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var UnitInterface |
||
22 | */ |
||
23 | private $unit; |
||
24 | |||
25 | /** |
||
26 | * @var float|int|string |
||
27 | */ |
||
28 | private $quantity; |
||
29 | |||
30 | /** |
||
31 | * @var UnitInterface |
||
32 | * @var float|int|string $quantity |
||
33 | */ |
||
34 | 10 | private function __construct(UnitInterface $unit, $quantity) |
|
39 | |||
40 | /** |
||
41 | * Creates quantity with same unit. |
||
42 | * Optimized to return this if same quantity. |
||
43 | * @var float|int|string |
||
44 | */ |
||
45 | 5 | final protected function create($quantity) |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 4 | private function getCalculator() |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | 6 | final public function getQuantity() |
|
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 1 | final public function getUnit() |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 2 | final public function compare(QuantityInterface $other) |
|
78 | { |
||
79 | 2 | $arg = $other->convert($this->unit)->getQuantity(); |
|
80 | |||
81 | 2 | return $this->getCalculator()->compare($this->quantity, $arg); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 1 | final public function equals(QuantityInterface $other) |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 1 | final public function isPositive() |
|
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 1 | final public function isNegative() |
|
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | 1 | final public function isConvertible(UnitInterface $unit) |
|
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | 5 | final public function convert(UnitInterface $unit) |
|
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | 1 | final public function add(QuantityInterface $addend) |
|
130 | { |
||
131 | 1 | $arg = $addend->convert($this->unit)->getQuantity(); |
|
132 | 1 | $res = $this->getCalculator()->add($this->quantity, $arg); |
|
133 | |||
134 | 1 | return $this->create($res); |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | 1 | final public function subtract(QuantityInterface $subtrahend) |
|
141 | { |
||
142 | 1 | $arg = $subtrahend->convert($this->unit)->getQuantity(); |
|
143 | 1 | $res = $this->getCalculator()->subtract($this->quantity, $arg); |
|
144 | |||
145 | 1 | return $this->create($res); |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | final public function multiply($multiplier) |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | final public function divide($divisor) |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | final public function ceil() |
||
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | */ |
||
181 | final public function floor() |
||
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | final public function round($roundingMode) |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | final public function absolute() |
||
207 | |||
208 | 10 | final public static function __callStatic($unit, $args) |
|
212 | |||
213 | /** |
||
214 | * Returns unit for given unit name. |
||
215 | * The only function to change in child classes. |
||
216 | * XXX Should be defined as abstract but `abstract static` is not supported in PHP5. |
||
217 | * @param string $name |
||
218 | * @throws InvalidConfigException |
||
219 | * @return UnitInterface |
||
220 | */ |
||
221 | protected static function findUnit($unit) |
||
225 | } |
||
226 |
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.