xref: /webtrees/app/Services/RomanNumeralsService.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
14ba350e3SGreg Roach<?php
24ba350e3SGreg Roach/**
34ba350e3SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
54ba350e3SGreg Roach * This program is free software: you can redistribute it and/or modify
64ba350e3SGreg Roach * it under the terms of the GNU General Public License as published by
74ba350e3SGreg Roach * the Free Software Foundation, either version 3 of the License, or
84ba350e3SGreg Roach * (at your option) any later version.
94ba350e3SGreg Roach * This program is distributed in the hope that it will be useful,
104ba350e3SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
114ba350e3SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
124ba350e3SGreg Roach * GNU General Public License for more details.
134ba350e3SGreg Roach * You should have received a copy of the GNU General Public License
144ba350e3SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
154ba350e3SGreg Roach */
164ba350e3SGreg Roachdeclare(strict_types=1);
174ba350e3SGreg Roach
184ba350e3SGreg Roachnamespace Fisharebest\Webtrees\Services;
194ba350e3SGreg Roach
204ba350e3SGreg Roach/**
214ba350e3SGreg Roach * Convert to and from Roman Numerals
224ba350e3SGreg Roach */
234ba350e3SGreg Roachclass RomanNumeralsService
244ba350e3SGreg Roach{
254ba350e3SGreg Roach    // Convert numbers to/from roman numerals
264ba350e3SGreg Roach    const ROMAN_NUMERALS = [
274ba350e3SGreg Roach        1000 => 'M',
284ba350e3SGreg Roach        900  => 'CM',
294ba350e3SGreg Roach        500  => 'D',
304ba350e3SGreg Roach        400  => 'CD',
314ba350e3SGreg Roach        100  => 'C',
324ba350e3SGreg Roach        90   => 'XC',
334ba350e3SGreg Roach        50   => 'L',
344ba350e3SGreg Roach        40   => 'XL',
354ba350e3SGreg Roach        10   => 'X',
364ba350e3SGreg Roach        9    => 'IX',
374ba350e3SGreg Roach        5    => 'V',
384ba350e3SGreg Roach        4    => 'IV',
394ba350e3SGreg Roach        1    => 'I',
404ba350e3SGreg Roach    ];
414ba350e3SGreg Roach
424ba350e3SGreg Roach    /**
434ba350e3SGreg Roach     * Convert a decimal number to roman numerals
444ba350e3SGreg Roach     *
454ba350e3SGreg Roach     * @param int $number
464ba350e3SGreg Roach     *
474ba350e3SGreg Roach     * @return string
484ba350e3SGreg Roach     */
494ba350e3SGreg Roach    public function numberToRomanNumerals(int $number): string
504ba350e3SGreg Roach    {
514ba350e3SGreg Roach        if ($number < 1) {
524ba350e3SGreg Roach            // Cannot convert zero/negative numbers
534ba350e3SGreg Roach            return (string) $number;
544ba350e3SGreg Roach        }
554ba350e3SGreg Roach        $roman = '';
564ba350e3SGreg Roach        foreach (self::ROMAN_NUMERALS as $key => $value) {
574ba350e3SGreg Roach            while ($number >= $key) {
584ba350e3SGreg Roach                $roman  .= $value;
594ba350e3SGreg Roach                $number -= $key;
604ba350e3SGreg Roach            }
614ba350e3SGreg Roach        }
624ba350e3SGreg Roach
634ba350e3SGreg Roach        return $roman;
644ba350e3SGreg Roach    }
654ba350e3SGreg Roach
664ba350e3SGreg Roach    /**
674ba350e3SGreg Roach     * Convert a roman numeral to decimal
684ba350e3SGreg Roach     *
694ba350e3SGreg Roach     * @param string $roman
704ba350e3SGreg Roach     *
714ba350e3SGreg Roach     * @return int
724ba350e3SGreg Roach     */
734ba350e3SGreg Roach    public function romanNumeralsToNumber(string $roman): int
744ba350e3SGreg Roach    {
754ba350e3SGreg Roach        $num = 0;
764ba350e3SGreg Roach        foreach (self::ROMAN_NUMERALS as $key => $value) {
774ba350e3SGreg Roach            while (strpos($roman, $value) === 0) {
784ba350e3SGreg Roach                $num += $key;
794ba350e3SGreg Roach                $roman = substr($roman, strlen($value));
804ba350e3SGreg Roach            }
814ba350e3SGreg Roach        }
824ba350e3SGreg Roach
834ba350e3SGreg Roach        return $num;
844ba350e3SGreg Roach    }
854ba350e3SGreg Roach}
86