<?php /* * By adding type hints and enabling strict type checking, code can become * easier to read, self-documenting and reduce the number of potential bugs. * By default, type declarations are non-strict, which means they will attempt * to change the original type to match the type specified by the * type-declaration. * * In other words, if you pass a string to a function requiring a float, * it will attempt to convert the string value to a float. * * To enable strict mode, a single declare directive must be placed at the top * of the file. * This means that the strictness of typing is configured on a per-file basis. * This directive not only affects the type declarations of parameters, but also * a function's return type. * * For more info review the Concept on strict type checking in the PHP track * <link>. * * To disable strict typing, comment out the directive below. */ declare(strict_types=1); class mapping { public int $n; public string $lit; public function __construct(int $n, string $lit) { $this->n = $n; $this->lit = $lit; } } function toRoman(int $number): string { $literals = [ new mapping(10_00, "M"), new mapping(9_00, "CM"), new mapping(5_00, "D"), new mapping(4_00, "CD"), new mapping(1_00, "C"), new mapping(90, "XC"), new mapping(50, "L"), new mapping(40, "XL"), new mapping(10, "X"), new mapping(9, "IX"), new mapping(5, "V"), new mapping(4, "IV"), new mapping(1, "I"), ]; $roman = ""; foreach ($literals as $mapping) { while ($number >= $mapping->n) { $roman .= $mapping->lit; $number -= $mapping->n; } } return $roman; }