Test Failed
Push — master ( 7fb687...840826 )
by Gabriel
12:03
created

MoneyFormatterTrait::formatByAggregate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace ByTIC\Money\Traits;
4
5
use Money\Currencies;
6
use Money\Currencies\BitcoinCurrencies;
7
use Money\Formatter\AggregateMoneyFormatter;
8
use Money\Formatter\BitcoinMoneyFormatter;
9
use Money\Formatter\DecimalMoneyFormatter;
10
use Money\Formatter\IntlLocalizedDecimalFormatter;
11
use Money\Formatter\IntlMoneyFormatter;
12
use Money\MoneyFormatter;
13
use NumberFormatter;
14
15
/**
16
 * Trait MoneyFormatterTrait
17
 * @package ByTIC\Money\Traits
18
 */
19
trait MoneyFormatterTrait
20
{
21
22
    /**
23
     * Format.
24
     *
25
     * @param string|null $locale
26
     * @param \Money\Currencies $currencies
27
     * @param int $style
28
     *
29
     * @return string
30
     */
31
    public function format($locale = null, Currencies $currencies = null, $style = NumberFormatter::CURRENCY)
32
    {
33
        return $this->formatByIntl($locale, $currencies, $style);
34
    }
35
36
    /**
37
     * Format by aggregate.
38
     *
39
     * @param MoneyFormatter[] $formatters
40
     *
41
     * @return string
42
     */
43
    public function formatByAggregate(array $formatters)
44
    {
45
        $formatter = new AggregateMoneyFormatter($formatters);
46
47
        return $this->formatByFormatter($formatter);
48
    }
49
50
    /**
51
     * Format by bitcoin.
52
     *
53
     * @param int $fractionDigits
54
     * @param \Money\Currencies $currencies
55
     *
56
     * @return string
57
     */
58
    public function formatByBitcoin($fractionDigits = 2, Currencies $currencies = null)
59
    {
60
        $formatter = new BitcoinMoneyFormatter($fractionDigits, $currencies ?: new BitcoinCurrencies());
61
62
        return $this->formatByFormatter($formatter);
63
    }
64
65
    /**
66
     * Format by decimal.
67
     *
68
     * @param \Money\Currencies $currencies
69
     *
70
     * @return string
71
     */
72
    public function formatByDecimal(Currencies $currencies = null)
73
    {
74
        $formatter = new DecimalMoneyFormatter($currencies ?: static::getCurrencies());
75
76
        return $this->formatByFormatter($formatter);
77
    }
78
79
    /**
80
     * Format by Formater
81
     * @return string
82
     */
83
    public function formatBy($formatter)
84
    {
85
        $formatter = money_formatter()->get($formatter);
86
87
        return $this->formatByFormatter($formatter);
88
    }
89
90
    /**
91
     * Format by intl.
92
     *
93
     * @param string|null $locale
94
     * @param \Money\Currencies $currencies
95
     * @param int $style
96
     *
97
     * @return string
98
     */
99
    public function formatByIntl($locale = null, Currencies $currencies = null, $style = NumberFormatter::CURRENCY)
100
    {
101
        $numberFormatter = new NumberFormatter($locale ?: static::getLocale(), $style);
102
        $formatter = new IntlMoneyFormatter($numberFormatter, $currencies ?: static::getCurrencies());
103
104
        return $this->formatByFormatter($formatter);
105
    }
106
107
    /**
108
     * Format by intl localized decimal.
109
     *
110
     * @param string|null $locale
111
     * @param \Money\Currencies $currencies
112
     * @param int $style
113
     *
114
     * @return string
115
     */
116
    public function formatByIntlLocalizedDecimal(
117
        $locale = null,
118
        Currencies $currencies = null,
119
        $style = NumberFormatter::CURRENCY
120
    ) {
121
        $numberFormatter = new NumberFormatter($locale ?: static::getLocale(), $style);
122
        $formatter = new IntlLocalizedDecimalFormatter($numberFormatter, $currencies ?: static::getCurrencies());
123
124
        return $this->formatByFormatter($formatter);
125
    }
126
127
    /**
128
     * Format by formatter.
129
     *
130
     * @param \Money\MoneyFormatter $formatter
131
     *
132
     * @return string
133
     */
134
    public function formatByFormatter(MoneyFormatter $formatter)
135
    {
136
        return $formatter->format($this->money);
137
    }
138
}