Completed
Push — master ( 0349ad...e37ca9 )
by Dispositif
02:28
created

NumberUtil   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A arab2roman() 0 14 4
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 © Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
11
namespace App\Domain\Utils;
12
13
14
class NumberUtil
15
{
16
    // tome et volume en romain
17
    const ROMAN_GLYPH
18
        = [
19
            1000 => 'M',
20
            900 => 'CM',
21
            500 => 'D',
22
            400 => 'CD',
23
            100 => 'C',
24
            90 => 'XC',
25
            50 => 'L',
26
            40 => 'XL',
27
            10 => 'X',
28
            9 => 'IX',
29
            5 => 'V',
30
            4 => 'IV',
31
            1 => 'I',
32
        ];
33
34
    public static function arab2roman(int $number): ?string
35
    {
36
        if ($number <= 0) {
37
            return null;
38
        }
39
        $result = '';
40
        foreach (static::ROMAN_GLYPH as $limit => $glyph) {
41
            while ($number >= $limit) {
42
                $result .= $glyph;
43
                $number -= $limit;
44
            }
45
        }
46
47
        return $result;
48
    }
49
50
}
51