a1essandro/roman-numbers

Easy conversion int <-> roman numbers

dev-master 2016-02-07 17:19 UTC

README

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License

Easy conversion int <=> roman numbers

##Requirements This package is guaranteed supported on PHP 5.3 and above.

##Installing ###Composer See more getcomposer.org.

Execute command

composer require a1essandro/roman-numbers

##Usage

###Basic


use NumberFormatter\RomanNumber;

$romanFromInt = new RomanNumber(12);
echo (string)$romanFromInt; // XII
echo $romanFromInt->toInt(); // 12

$romanFromRomat = new RomanNumber('XIX');
echo (string)$romanFromRoman; // XIX
echo $romanFromRoman->toInt(); // 19

###Validation


try {
    new RomanNumber($someVariable);
} catch (InvalidStringException $e) {
    //if $someVariable is invalid string (not correct roman number)
} catch (InvalidIntException $e) {
    //if $someVariable is invalid int (for example -1)
} catch (InvalidArgumentException $e) {
    //if $someVariable is not int or string (invalid type)
}

//Another way to check validity:

$validRomanNumber = 'XXIX';
RomanNumber::isValidRomanNumber($validRomanNumber); // true

$invalidRomanNumber = 'XDX';
RomanNumber::isValidRomanNumber($invalidRomanNumber); // false