Failed Conditions
Push — master ( ff7329...d32c3b )
by Sylvain
02:29
created

Format::truncate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 8
ccs 5
cts 5
cp 1
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 $ellipsis the string to indicate truncation happened
17
     *
18
     * @return string truncated string
19
     */
20 5
    public static function truncate(string $string, int $maxLength, string $ellipsis = '…'): string
21
    {
22 5
        if (mb_strlen($string) > $maxLength) {
23 3
            $string = mb_substr($string, 0, $maxLength - mb_strlen($ellipsis));
24 3
            $string .= $ellipsis;
25
        }
26
27 5
        return $string;
28
    }
29
30
    /**
31
     * Shortcut to format money
32
     */
33
    public static function money(Money $money): string
34
    {
35
        $currencies = new ISOCurrencies();
36
        $moneyFormatter = new DecimalMoneyFormatter($currencies);
37
38
        return $moneyFormatter->format($money);
39
    }
40
}
41