Completed
Push — dx/improve-discount-rendering ( 0a756e...235e71 )
by Kamil
62:07 queued 27:17
created

MoneyFormatter::format()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 8
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\MoneyBundle\Formatter;
13
14
use Webmozart\Assert\Assert;
15
16
/**
17
 * @author Łukasz Chruściel <[email protected]>
18
 */
19
final class MoneyFormatter implements MoneyFormatterInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function format($amount, $currency, $locale = 'en')
25
    {
26
        $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
27
28
        $result = $formatter->formatCurrency(abs($amount / 100), $currency);
29
        Assert::notSame(
30
            false,
31
            $result,
32
            sprintf('The amount "%s" of type %s cannot be formatted to currency "%s".', $amount, gettype($amount), $currency)
33
        );
34
35
        return $amount >= 0 ? $result : '-' . $result;
36
    }
37
}
38