Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| PriceFormatter | |
100.00% |
9 / 9 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| canonicaliseIso4217Code | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| format | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Alistaircol\Hta\Domain\Basket; |
| 6 | |
| 7 | use Alistaircol\Hta\Domain\Basket\Exceptions\BasketTotalFormattingForIso4217NotImplementedException; |
| 8 | use Alistaircol\Hta\Domain\Basket\Exceptions\BasketTotalFormattingInvalidIso4217CodeException; |
| 9 | |
| 10 | class PriceFormatter |
| 11 | { |
| 12 | public string $iso4217; |
| 13 | |
| 14 | public function __construct(string $iso4217 = 'GBP') |
| 15 | { |
| 16 | $code = $this->canonicaliseIso4217Code($iso4217); |
| 17 | |
| 18 | if (strlen($code) !== 3) { |
| 19 | throw BasketTotalFormattingInvalidIso4217CodeException::code($code); |
| 20 | } |
| 21 | |
| 22 | $this->iso4217 = $code; |
| 23 | } |
| 24 | |
| 25 | private function canonicaliseIso4217Code(string $iso4217): string |
| 26 | { |
| 27 | return strtoupper(trim($iso4217)); |
| 28 | } |
| 29 | |
| 30 | public function format(int $units): string |
| 31 | { |
| 32 | // TODO: install an ISO-4217 helper to format when necessary |
| 33 | |
| 34 | switch ($this->iso4217) { |
| 35 | case 'GBP': |
| 36 | return '£' . number_format(($units / 100), 2); |
| 37 | default: |
| 38 | throw BasketTotalFormattingForIso4217NotImplementedException::format($units, $this->iso4217); |
| 39 | } |
| 40 | } |
| 41 | } |