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

NumberUtil::arab2roman()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 14
rs 10
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