Test Failed
Push — master ( 646e1f...19314b )
by Gabriel
02:47
created

HtmlFormatter::format()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 9.9666
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