Download the PHP package jeronimofagundes/php-roman-numbers without Composer
On this page you can find all versions of the php package jeronimofagundes/php-roman-numbers. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jeronimofagundes/php-roman-numbers
More information about jeronimofagundes/php-roman-numbers
Files in jeronimofagundes/php-roman-numbers
Package php-roman-numbers
Short Description A helper to convert roman numbers from/to decimal numbers.
License MIT
Informations about the package php-roman-numbers
php-roman-numbers
Support for roman numbers in PHP
Architecture
It was implemented a simple helper class, namespaced "\Jeronimofagundes\Numbers\Roman\Helper", which have two methods to convert to and from arabic numbers.
The autoloading was used following PHP FIG's PSR-4 autoloading standards.
The source code is inside the "src" directory. Dependencies are inside "vendor" directory. Tests are inside "tests" folder.
Testing
To perform unitary tests I used PHPUnit framework. There is one class contaning the tests for the Helper class.
Problem-solving
Conversion Arabic to Roman
The first thing I checked is if the arabic number fits the range [1-3999]. This range was defined by Wikipedia's Roman numerals article.
After that, I fill the input number with zeros to the left so it reaches 4 digits long. Example: 14 turns into 0014.
The next step is convert each character into its corresponding roman. These correspondences are mapped in a constant TO_ROMAN inside the Helper class. There is mappings for "ones", "tens", "hundreds" and "thousands". This way, the first 0 is ignored, the second one ignored as well, 1 turns into X and 4 turns into IV. That means the returned string is XIV.
Conversion Roman to Arabic
This conversion starts checking of the given roman number is valid. I did this using a regular expression.
I mapped some correspondences in a constant FROM_ROMAN in the Helper class. Using this, I read the roman number in the following way:
- I start from the most left character.
- I read two characters. If this correspondence is mapped in the FROM_ROMAN array, it means it is a subtractive correspondence (like IV, XC or CM), and it gets converted. We move the pointer two characters right and go to step 2 again.
- If there is no correspondence in the FROM_ROMAN array, it means it is not a subtractive correspondence. So, we just read one character. That character will have a correspondence in the array, and that value is added to the final result. We move one character to the right and go back to the step two.
- The processing ends when the roman number is finished.
How to use this library
In order to one use this library, he/she can require it with composer, the standard PHP library package manager.
and use the following class in his/her code:
I included a file named example.php showing how to use the library. To run it, follow this steps:
-
Clone this repository and cd into it
-
. Pull the docker image
-
Install the dependencies with composer
- Run the file example.php:
How to test it
We need to use PHPUnit to execute the tests, and we will do it using docker. Follow this steps:
-
Clone this repository and cd into it
-
Pull the docker image
- Run PHPUnit