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

Format   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 55.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 34
ccs 5
cts 9
cp 0.5556
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A truncate() 0 8 2
A money() 0 6 1
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