You are here

public function TocFormatter::convertNumberToRomanNumeral in TOC API 8

Convert a number to a roman numeral.

References

Parameters

int $number: A number.

Return value

string The number converted to a roman numeral.

Overrides TocFormatterInterface::convertNumberToRomanNumeral

1 call to TocFormatter::convertNumberToRomanNumeral()
TocFormatter::convertNumberToListTypeValue in src/TocFormatter.php
Convert a number to a selected type (alpha or roman).

File

src/TocFormatter.php, line 74

Class

TocFormatter
Defines a service for formatting a table of content's headers, numbering, and ids..

Namespace

Drupal\toc_api

Code

public function convertNumberToRomanNumeral($number) {
  $roman_numerals = [
    '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,
  ];
  $result = '';
  foreach ($roman_numerals as $roman_numeral => $roman_number) {
    $matches = intval($number / $roman_number);
    $result .= str_repeat($roman_numeral, $matches);
    $number = $number % $roman_number;
  }
  return $result;
}