1 | <?php declare(strict_types=1); |
||
15 | final class PriceCalculator implements IPriceCalculator |
||
16 | { |
||
17 | use SmartObject; |
||
18 | |||
19 | /** |
||
20 | * @var float |
||
21 | */ |
||
22 | protected $basePrice = 0.0; |
||
23 | |||
24 | /** |
||
25 | * @var IDiscount|NULL |
||
26 | */ |
||
27 | protected $discount; |
||
28 | |||
29 | /** |
||
30 | * @var float |
||
31 | */ |
||
32 | protected $price = 0.0; |
||
33 | |||
34 | /** |
||
35 | * @var float |
||
36 | */ |
||
37 | protected $vatRate = 0.0; |
||
38 | |||
39 | /** |
||
40 | * @var float |
||
41 | */ |
||
42 | protected $priceVat = 0.0; |
||
43 | |||
44 | /** |
||
45 | * @var int |
||
46 | */ |
||
47 | protected $decimalPoints = 2; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $calculateFrom = self::FROM_BASEPRICE; |
||
53 | |||
54 | public function __construct(float $vatRate = 0.0, float $basePrice = 0.0, ?IDiscount $discount = NULL) |
||
55 | { |
||
56 | $this->setVatRate($vatRate); |
||
57 | $this->setBasePrice($basePrice); |
||
58 | $this->setDiscount($discount); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Get price without VAT and discount. |
||
63 | */ |
||
64 | public function getBasePrice(): float |
||
65 | { |
||
66 | return $this->basePrice; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Set price without VAT and discount. |
||
71 | */ |
||
72 | public function setBasePrice(float $value): self |
||
73 | { |
||
74 | $this->basePrice = $value; |
||
75 | $this->calculateFrom = self::FROM_BASEPRICE; |
||
76 | return $this; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Get discount in percent without VAT. |
||
81 | */ |
||
82 | public function getDiscount(): IDiscount |
||
86 | |||
87 | /** |
||
88 | * Set discount instance. |
||
89 | */ |
||
90 | public function setDiscount(?IDiscount $discount): self |
||
95 | |||
96 | /** |
||
97 | * Set amount discount. |
||
98 | */ |
||
99 | public function setAmountDiscount(float $value): self |
||
104 | |||
105 | /** |
||
106 | * Set percent discount. |
||
107 | */ |
||
108 | public function setPercentDiscount(float $value): self |
||
113 | |||
114 | /** |
||
115 | * Get price after discount without VAT. |
||
116 | */ |
||
117 | public function getPrice(): float |
||
121 | |||
122 | /** |
||
123 | * Set price after discount without VAT. |
||
124 | */ |
||
125 | public function setPrice(float $value): self |
||
131 | |||
132 | /** |
||
133 | * Get VAT rate in percent. |
||
134 | */ |
||
135 | public function getVatRate(): float |
||
139 | |||
140 | /** |
||
141 | * Set VAT rate in percent. |
||
142 | */ |
||
143 | public function setVatRate(float $value): self |
||
148 | |||
149 | /** |
||
150 | * Get price after discount with VAT. |
||
151 | */ |
||
152 | public function getPriceVat(): float |
||
156 | |||
157 | /** |
||
158 | * Set price after discount with VAT. |
||
159 | */ |
||
160 | public function setPriceVat(float $value): self |
||
166 | |||
167 | /** |
||
168 | * Get decimal point for rounding. |
||
169 | */ |
||
170 | public function getDecimalPoints(): float |
||
174 | |||
175 | /** |
||
176 | * Set decimal point for rounding. |
||
177 | */ |
||
178 | public function setDecimalPoints(int $value): self |
||
183 | |||
184 | /** |
||
185 | * Calculate prices and return result. |
||
186 | */ |
||
187 | public function calculate(): PriceCalculatorResult |
||
201 | |||
202 | private function calculateFromBasePrice(): array |
||
210 | |||
211 | private function calculateFromPrice(): array |
||
219 | |||
220 | private function calculateFromPriceVat(): array |
||
228 | |||
229 | private function round(float $price): float |
||
233 | } |
||
234 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.