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