Page Menu
Home
Code
Search
Configure Global Search
Log In
Files
F210366
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Subscribers
None
View Options
diff --git a/Engines/Templates/Smarty/Plugins/modifier.romanize.php b/Engines/Templates/Smarty/Plugins/modifier.romanize.php
index 1d23131..4fa5f0b 100644
--- a/Engines/Templates/Smarty/Plugins/modifier.romanize.php
+++ b/Engines/Templates/Smarty/Plugins/modifier.romanize.php
@@ -1,101 +1,28 @@
<?php
/**
* Smarty plugin
*
* @package Smarty
* @subpackage PluginsModifier
*/
+use Keruald\OmniTools\Culture\Rome\RomanNumerals;
+
/**
* Smarty romanize modifier plugin
*
* Type: modifier<br>
* Name: romanize<br>
* Purpose: prints a number in roman
*
* @param int $
* @return string
*/
function smarty_modifier_romanize ($number) : string {
- return romanize($number);
-}
-
-function romanize ($number) : string {
- switch ($number) {
- case 1:
- return 'i';
- case 2:
- return 'ii';
- case 3:
- return 'iii';
- case 4:
- return 'iv';
- case 5:
- return 'v';
- case 6:
- return 'vi';
- case 7:
- return 'vii';
- case 8:
- return 'viii';
- case 9:
- return 'ix';
- case 10:
- return 'x';
-
- case 50:
- return 'l';
- case 100:
- return 'c';
- case 500:
- return 'd';
- case 1000:
- return 'm';
-
- default:
- if ($number < 21) {
- return 'x' . romanize($number - 10);
- } elseif ($number < 30) {
- return 'xx' . romanize($number - 20);
- } elseif ($number < 40) {
- return 'xxx' . romanize($number - 30);
- } elseif ($number < 50) {
- return 'xl' . romanize($number - 40);
- } elseif ($number < 60) {
- return 'l' . romanize($number - 50);
- } elseif ($number < 70) {
- return 'lx' . romanize($number - 60);
- } elseif ($number < 80) {
- return 'lxx' . romanize($number - 70);
- } elseif ($number < 90) {
- return 'lxxx' . romanize($number - 80);
- } elseif ($number < 100) {
- return 'xc' . romanize($number - 90);
- } elseif ($number < 200) {
- return 'c' . romanize($number - 100);
- } elseif ($number < 300) {
- return 'cc' . romanize($number - 200);
- } elseif ($number < 400) {
- return 'ccc' . romanize($number - 300);
- } elseif ($number < 500) {
- return 'cd' . romanize($number - 400);
- } elseif ($number < 600) {
- return 'd' . romanize($number - 500);
- } elseif ($number < 700) {
- return 'dc' . romanize($number - 600);
- } elseif ($number < 800) {
- return 'dcc' . romanize($number - 700);
- } elseif ($number < 900) {
- return 'dccc' . romanize($number - 800);
- } elseif ($number < 1000) {
- return 'cm' . romanize($number - 800);
- } elseif (is_numeric($number)) {
- $m = floor($number / 1000);
-
- return str_repeat('m', $m) . romanize($number - $m);
- }
-
- // Not a arab number, return as is
- return $number;
+ try {
+ return RomanNumerals::fromHinduArabic($number);
+ } catch (Throwable $ex) {
+ // Not a strictly positive integer, don't modify
+ return $number;
}
}
diff --git a/composer.json b/composer.json
index a67451c..0f1ba0c 100644
--- a/composer.json
+++ b/composer.json
@@ -1,27 +1,29 @@
{
"name": "zed/zed",
"description": "Hypership and galaxy",
"type": "project",
"require": {
"smarty/smarty": "dev-master",
- "keruald/globalfunctions": "^0.5.1"
+ "keruald/globalfunctions": "^0.5.1",
+ "keruald/omnitools": "^0.1.0"
},
"require-dev": {
"nasqueron/codestyle": "^0.0.1",
"phan/phan": "^0.12.5",
"squizlabs/php_codesniffer": "^3.2",
"phpunit/phpunit": "^7.1"
},
"autoload": {
"psr-4": {
- "Zed\\Engines\\": "Engines"
+ "Zed\\Engines\\": "Engines",
+ "Zed\\Tests\\": "dev/tests"
}
},
"license": "BSD-2-Clause",
"authors": [
{
"name": "Sébastien Santoro",
"email": "dereckson@espace-win.org"
}
]
}
diff --git a/dev/tests/Engines/Templates/Smarty/Plugins/RomanizeModifierTest.php b/dev/tests/Engines/Templates/Smarty/Plugins/RomanizeModifierTest.php
new file mode 100644
index 0000000..724b9c7
--- /dev/null
+++ b/dev/tests/Engines/Templates/Smarty/Plugins/RomanizeModifierTest.php
@@ -0,0 +1,28 @@
+<?php
+declare(strict_types=1);
+
+namespace Zed\Tests\Engines\Templates\Smarty\Plugins;
+
+class RomanNumeralsTest extends SmartyPluginTestCase {
+
+ public function setUp () {
+ $this->requirePlugin('modifier', 'romanize');
+ }
+
+ public function testPluginWithCorrectValueAsInteger () {
+ $this->assertEquals('iv', smarty_modifier_romanize(4));
+ }
+
+ public function testPluginWithCorrectValueAsString () {
+ $this->assertEquals('iv', smarty_modifier_romanize('4'));
+ }
+
+ public function testPluginWithNonNumeric () {
+ $this->assertEquals('quux', smarty_modifier_romanize('quux'));
+ }
+
+ public function testPluginWithNegativeNumber () {
+ $this->assertSame('-4', smarty_modifier_romanize(-4));
+ }
+
+}
diff --git a/dev/tests/Engines/Templates/Smarty/Plugins/SmartyPluginTestCase.php b/dev/tests/Engines/Templates/Smarty/Plugins/SmartyPluginTestCase.php
new file mode 100644
index 0000000..cab02b0
--- /dev/null
+++ b/dev/tests/Engines/Templates/Smarty/Plugins/SmartyPluginTestCase.php
@@ -0,0 +1,20 @@
+<?php
+declare(strict_types=1);
+
+namespace Zed\Tests\Engines\Templates\Smarty\Plugins;
+
+use PHPUnit\Framework\TestCase;
+
+abstract class SmartyPluginTestCase extends TestCase {
+
+ protected function requirePlugin (string $type, string $name) {
+ require_once($this->getPluginPath($type, $name));
+ }
+
+ private function getPluginPath (string $type, string $name) {
+ return dirname(__DIR__, 6) // application root directory
+ . '/Engines/Templates/Smarty/Plugins/'
+ . "$type.$name.php";
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Nov 11, 11:13 (2 w, 3 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
21162
Default Alt Text
(6 KB)
Attached To
rZED Zed
Event Timeline
Log In to Comment