<?php
/**
* @author: Jörg Reinholz, fastix WebDesign & Consult, Kassel - http://www.fastix.org/
* @version: 1.0.9
* @licence: https://code.fastix.org/lizenz.php
**/

class romanNumbers

{
    private $hash;
    public  $getAsEntitiesDefault = false;
    public  $getShortDefault      = true;

    function __construct () {
        $this -> roman['h'] = 100000;
        $this -> roman['a'] = 50000;
        $this -> roman['z'] = 10000;
        $this -> roman['f']  = 5000;
        $this -> roman['Z']  = 2000; #old
        $this -> roman['M']  = 1000;
        $this -> roman['A']  = 500; # old
        $this -> roman['D']  = 500;
        $this -> roman['Q']  = 500; #old
        $this -> roman['G']  = 400; #old
        $this -> roman['P']  = 400; #old
        $this -> roman['B']  = 300; #old
        $this -> roman['E']  = 250; #old
        $this -> roman['H']  = 200; #old
        $this -> roman['T']  = 160; #old
        $this -> roman['K']  = 151; #old
        $this -> roman['Y']  = 150; #old
        $this -> roman['C']  = 100;
        $this -> roman['N']  = 90; #old
        $this -> roman['R']  = 80; #old
        $this -> roman['S']  = 70; #old
        $this -> roman['L']  = 50;
        $this -> roman['F']  = 40; #old
        $this -> roman['O']  = 11; #old
        $this -> roman['X']  = 10;
        $this -> roman['V']  = 5;
        $this -> roman['I']  = 1;
     }

    function getDec($str) {
        $str = $this -> toEntities($str);

        $arSearch = array(
            'CCCI&#8579;&#8579;&#8579;',  '(((I)))', '&#8584;',  # 100.000
            'I&#8579;&#8579;&#8579;',     'I)))',    '&#8583;',  # 50.000
            'CCI&#8579;&#8579;',          '((I))',   '&#8578;',  # 10.000
            'I&#8579',                    'I))',     '&#8577;',  # 5.000
            'CI&#8579;',                  '(I)',     '&#8576;', '&Phi;',  # 1.000
            'X/',                   # 15
            '&bull;M','*M','&#8845;', ' N ',   # Tausender-Trennzeichen
            '&#1055;',
            '&#1064;',
            '&#1059;',
            '1',
            ' '
        );

        $arReplace = array(
            'h','h', 'h',    # 100.000
            'a','a', 'a',    # 50.000
            'z','z', 'z',    # 10.000
            'f','f', 'f',    # 5.000
            'M','M', 'M', 'M',  # 1.000
            'XV',            # 15
            '$', '$', '$', '$', # Tausender-Trennzeichen
            'II',
            'III',
            'V',
            'I',
            ''
        );

        $str = str_replace($arSearch, $arReplace, $str);

        if (strpos ($str, '$')  ) {
            $parts = explode('$', $str);
            $ret=0;
            $faktor = pow(1000, count($parts) - 1 );
            for ( $i = 0; $i < count($parts); $i++ ) {
                $z = $this -> getDec($parts[$i]) * $faktor;
                $faktor = $faktor / 1000;
                $ret += $z;
            }
            return $ret;
        }

        $strLat = '';
        for ( $i=0; $i < strlen($str); $i++ ) {
            $c = $str[$i];
            if ($c) {
                if ( isset( $this -> roman[$c] ) ) {
                    $strLat .= $c;
                } else {
                    #trigger_error('class romanNumbers, function getDec: Unknown char "'.$c.'" ignored.', E_USER_NOTICE );
                }
            }
        }


        $str  = strrev($strLat);
        $roman = $this -> roman; # Benutze Kopie!
        $dec  = 0;
        for ($i = 0; $i < strlen($str); $i++ ) {
            $c = $str[$i];
            if (! isset($roman[$c])) { echo " $str \n";}
            $dec += $roman[$c];
            if ( 'V' == $c || 'X' == $c )       $roman['I'] = -1;
            elseif ( 'C' == $c || 'L' == $c )   $roman['X'] = -10;
            elseif ( 'M' == $c || 'D' == $c )   $roman['C'] = -100;
            elseif ( 'z' == $c || 'f' == $c )   $roman['M'] = -1000;
            elseif ( 'h' == $c || 'a' == $c )   $roman['z'] = -10000;
        }
        return $dec;
    }

    function getRoman ($int, $getAsEntities=false, $getLong=false) {
        if ( $int != floor($int) ) {
            trigger_error('class romanMumbers, function getRoman: Fatal error: Romaniens don\'t know floats!', E_USER_NOTICE);
            return false;
        }
        if ( $int < 0 ) {
            trigger_error('class romanMumbers, function getRoman: Fatal error: Romaniens don\'t know negative Numbers!', E_USER_NOTICE);
            return false;
        }
        if ( $int < 0 ) {
            trigger_error('class romanMumbers, function getRoman: Fatal error: Its true, the Romaniens don\'t know the zero!', E_USER_NOTICE);
            return false;
        }
        if ( $int > PHP_INT_MAX ) {
            trigger_error ( 'class: romanMumbers, function: getString :: Es wurde eine zu große Zahl ('.$int.') übergeben. Maximum ist '. PHP_INT_MAX , E_USER_ERROR );
        }
        $str = '';
        while ( $int >= 100000 ) {
            $str .= 'ↈ';
            $int = $int - 100000;
        }
        while ( $int >= 50000 ) {
            $str .= 'ↇ';
            $int = $int - 50000;
        }
        while ( $int >= 10000 ) {
            $str .= 'ↂ';
            $int = $int - 10000;
        }
        while ( $int >= 5000 ) {
            $str .= 'ↁ';
            $int = $int - 5000;
        }
        while ( $int >= 1000 ) {
            $str .= 'M';
            $int = $int - 1000;
        }
        while ( $int >= 500 ) {
            $str .= 'D';
            $int = $int - 500;
        }
        while ( $int >= 100 ) {
            $str .= 'C';
            $int = $int - 100;
        }
        while ( $int >= 50 ) {
            $str .= 'L';
            $int = $int - 50;
        }
        while ( $int >= 10 ) {
            $str .= 'X';
            $int = $int - 10;
        }
        while ( $int >= 5 ) {
            $str .= 'V';
            $int = $int - 5;
        }
        while ( $int >= 1 ) {
            $str .= 'I';
            $int = $int - 1;
        }


        if ( $this->getShortDefault and ! $getLong ) {
            $arSearch = array (
                'VIIII',    # 9
                'IIII',     # 4
                'LXXXX',    # 90
                'XXXX',     # 40
                'DCCCC',    # 900
                'CCCC',     # 400
                'ↁMMMM',    # 9000
                'MMMM',     # 4000
                'ↇↂↂↂↂ', # 90000
                'ↂↂↂↂ'  # 40000
            );

            $arReplace = array (
                'IX',       #9
                'IV',       #4
                'XC',       #90
                'XL',       #40
                'CM',       #900
                'CD',       #400
                'Mↂ',      #9000
                'Mↁ',       #4000
                'ↂↈ',      #90000
                'ↂↇ'       #40000
            );
            $str = str_replace( $arSearch, $arReplace, $str );
        }
        if ( $getAsEntities or $this -> getAsEntitiesDefault ) {
            $str = $this -> toEntities($str);
        }
        return $str;
    }


    private function toEntities($str) {
        if ( function_exists( 'recode_string' ) )               return recode_string("utf-8..html", $str);
        elseif (function_exists( 'mb_convert_encoding' ) )      return mb_convert_encoding($str, 'HTML-ENTITIES', 'UTF-8' );
        else                                                    return htmlentities( $str, ENT_SUBSTITUTE );
    }
}