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