Passed
Push — master ( 2d5d0a...7d78c5 )
by Adrien
02:23
created

Format::money()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix;
6
7
use Money\Currencies\ISOCurrencies;
8
use Money\Formatter\DecimalMoneyFormatter;
9
use Money\Money;
10
11
abstract class Format
12
{
13
    /**
14
     * Truncate a string and append '…' at the end
15
     *
16
     * @param string $string
17
     * @param int $maxLength
18
     * @param string $ellipsis the string to indicate truncation happened
19
     *
20
     * @return string truncated string
21
     */
22 5
    public static function truncate(string $string, int $maxLength, string $ellipsis = '…'): string
23
    {
24 5
        if (mb_strlen($string) > $maxLength) {
25 3
            $string = mb_substr($string, 0, $maxLength - mb_strlen($ellipsis));
26 3
            $string .= $ellipsis;
27
        }
28
29 5
        return $string;
30
    }
31
32
    /**
33
     * Shortcut to format money
34
     *
35
     * @param Money $money
36
     *
37
     * @return string
38
     */
39
    public static function money(Money $money): string
40
    {
41
        $currencies = new ISOCurrencies();
42
        $moneyFormatter = new DecimalMoneyFormatter($currencies);
43
44
        return $moneyFormatter->format($money);
45
    }
46
}
47