1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Money\Formatter; |
4
|
|
|
|
5
|
|
|
use Money\Currencies; |
6
|
|
|
use Money\Money; |
7
|
|
|
use Money\MoneyFormatter; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class HtmlFormatter |
11
|
|
|
* @package ByTIC\Money\Formatter |
12
|
|
|
*/ |
13
|
|
|
class HtmlFormatter implements MoneyFormatter |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Currencies |
18
|
|
|
*/ |
19
|
|
|
private $currencies; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Currencies $currencies |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Currencies $currencies) |
25
|
|
|
{ |
26
|
|
|
$this->currencies = $currencies; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Formats a Money object as string. |
31
|
|
|
* |
32
|
|
|
* @param Money $money |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
* |
36
|
|
|
* Exception\FormatterException |
37
|
|
|
*/ |
38
|
|
|
public function format(Money $money) |
39
|
|
|
{ |
40
|
|
|
$currencyCode = $money->getCurrency()->getCode(); |
41
|
|
|
list($integer, $decimal, $negative) = $this->parseAmount($money); |
42
|
|
|
$output = $this->integer($integer); |
43
|
|
|
$output .= $this->decimal($decimal); |
44
|
|
|
$output .= $this->currency($currencyCode); |
45
|
|
|
$class = $negative ? ' negative' : ''; |
46
|
|
|
return '<span class="price' . $class . '" content="' . $integer . '.' . $decimal . '">' |
47
|
|
|
. $output |
48
|
|
|
. '</span>'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $money |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
protected function parseAmount($money) |
56
|
|
|
{ |
57
|
|
|
$valueBase = $money->getAmount(); |
58
|
|
|
$negative = false; |
59
|
|
|
|
60
|
|
|
if ($valueBase[0] === '-') { |
61
|
|
|
$negative = true; |
62
|
|
|
$valueBase = substr($valueBase, 1); |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
$subunit = $this->currencies->subunitFor($money->getCurrency()); |
66
|
|
|
$valueLength = strlen($valueBase); |
67
|
|
|
|
68
|
|
|
if ($valueLength > $subunit) { |
69
|
|
|
$integer = substr($valueBase, 0, $valueLength - $subunit); |
70
|
|
|
$decimal = substr($valueBase, $valueLength - $subunit); |
71
|
|
|
} else { |
72
|
|
|
$integer = 0; |
73
|
|
|
$decimal = str_pad('', $subunit - $valueLength, '0') . $valueBase; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return [$integer, $decimal, $negative]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $code |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
protected function currency($code) |
84
|
|
|
{ |
85
|
|
|
return '<span class="money-currency">' . $code . '</span>'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $value |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function integer($value) |
93
|
|
|
{ |
94
|
|
|
return '<span class="money-int">' . number_format($value) . '</span>'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $value |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function decimal($value) |
102
|
|
|
{ |
103
|
|
|
$value = str_pad($value, 2, '0', STR_PAD_LEFT); |
104
|
|
|
return '<sup class="money-decimal">.' . $value . '</sup>'; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|