Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding roman converters #3

Merged
merged 1 commit into from
Apr 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ echo Number::ordinal(1002); // "nd"
echo Number::ordinal(-111); // "th"
```

**Roman numbers**
```php
use PHPHumanizer\Number;

echo Number::toRoman(1); // "I"
echo Number::toRoman(5); // "V"
echo Number::toRoman(1300); // "MCCC"

echo Number::fromRoman("MMMCMXCIX"); // 3999
echo Number::fromRoman("V"); // 5
echo Number::fromRoman("CXXV"); // 125
```

**Binary Suffix**

```php
Expand Down
34 changes: 34 additions & 0 deletions spec/PHPHumanizer/NumberSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,38 @@ function it_throw_exception_when_converting_to_string_with_metric_suffix_non_num
$this->shouldThrow(new \InvalidArgumentException("Metric suffix converter accept only numeric values."))
->during('metricSuffix', array('as12'));
}

function it_converts_numbers_to_roman()
{
$this->toRoman(1)->shouldReturn("I");
$this->toRoman(5)->shouldReturn("V");
$this->toRoman(9)->shouldReturn("IX");
$this->toRoman(10)->shouldReturn("X");
$this->toRoman(125)->shouldReturn("CXXV");
$this->toRoman(1300)->shouldReturn("MCCC");
$this->toRoman(3999)->shouldReturn("MMMCMXCIX");
}

function it_throws_exception_when_converting_number_is_out_of_range()
{
$this->shouldThrow(new \InvalidArgumentException())->during('toRoman', array(-1));
$this->shouldThrow(new \InvalidArgumentException())->during('toRoman', array(4000));
}

function it_converts_roman_numbers_to_arabic()
{
$this->fromRoman("I")->shouldReturn(1);
$this->fromRoman("V")->shouldReturn(5);
$this->fromRoman("IX")->shouldReturn(9);
$this->fromRoman("CXXV")->shouldReturn(125);
$this->fromRoman("MCCC")->shouldReturn(1300);
$this->fromRoman("MMMCMXCIX")->shouldReturn(3999);
}

function it_throws_exception_when_converting_roman_number_is_invalid()
{
$this->shouldThrow(new \InvalidArgumentException())->during('fromRoman', array(1234));
$this->shouldThrow(new \InvalidArgumentException())->during('fromRoman', array(""));
$this->shouldThrow(new \InvalidArgumentException())->during('fromRoman', array("foobar"));
}
}
13 changes: 13 additions & 0 deletions src/PHPHumanizer/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPHumanizer;

use PHPHumanizer\Number\Ordinal;
use PHPHumanizer\Number\RomanNumeral;
use PHPHumanizer\String\BinarySuffix;
use PHPHumanizer\String\MetricSuffix;

Expand All @@ -29,4 +30,16 @@ public static function metricSuffix($number, $locale = 'en')
$binarySuffix = new MetricSuffix($number, $locale);
return $binarySuffix->convert();
}

public static function toRoman($number)
{
$romanNumeral = new RomanNumeral();
return $romanNumeral->toRoman($number);
}

public function fromRoman($number)
{
$romanNumeral = new RomanNumeral();
return $romanNumeral->fromRoman($number);
}
}
90 changes: 90 additions & 0 deletions src/PHPHumanizer/Number/RomanNumeral.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace PHPHumanizer\Number;

class RomanNumeral
{
const MIN_VALUE = 1;
const MAX_VALUE = 3999;
const ROMAN_STRING_MATCHER = "/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/";

private $map = array(
'M' => 1000,
'CM' => 900,
'D' => 500,
'CD' => 400,
'C' => 100,
'XC' => 90,
'L' => 50,
'XL' => 40,
'X' => 10,
'IX' => 9,
'V' => 5,
'IV' => 4,
'I' => 1
);


/**
* @param $number
* @return string
* @throws \InvalidArgumentException
*/
public function toRoman($number)
{
if (($number < self::MIN_VALUE) || ($number > self::MAX_VALUE)) {
throw new \InvalidArgumentException();
}

$romanString = '';

while($number > 0)
{
foreach($this->map as $key => $value)
{
if($number >= $value)
{
$romanString .= $key;
$number -= $value;
break;
}
}
}

return $romanString;
}

/**
* @param $string
* @return int
* @throws \InvalidArgumentException
*/
public function fromRoman($string)
{
if (strlen($string) === 0 || 0 === preg_match(self::ROMAN_STRING_MATCHER, $string)) {
throw new \InvalidArgumentException();
}

$total = 0;
$i = strlen($string);

while ($i > 0)
{
$digit = $this->map[$string{--$i}];

if ($i > 0) {
$previousDigit = $this->map[$string{$i - 1}];

if ($previousDigit < $digit) {
$digit -= $previousDigit;
$i--;
}
}

$total += $digit;
}

return $total;
}

}