refactor formatDate function and create test
This commit is contained in:
@@ -10,6 +10,8 @@ env:
|
|||||||
- DB_USER=root
|
- DB_USER=root
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
|
- sudo apt-get update -qq
|
||||||
|
- sudo apt-get install -y language-pack-fr language-pack-fr-base
|
||||||
- phpenv config-add travis.php.ini
|
- phpenv config-add travis.php.ini
|
||||||
- composer self-update
|
- composer self-update
|
||||||
- composer install --prefer-dist --dev
|
- composer install --prefer-dist --dev
|
||||||
|
|||||||
@@ -89,22 +89,29 @@ class Format extends AbstractSmartyPlugin
|
|||||||
|
|
||||||
$locale = $this->getParam($params,'locale', false);
|
$locale = $this->getParam($params,'locale', false);
|
||||||
|
|
||||||
if($locale === false) {
|
if (false === $locale) {
|
||||||
return $date->format($format);
|
$value = $date->format($format);
|
||||||
} else {
|
} else {
|
||||||
if(function_exists('setlocale')) {
|
$value = $this->formatDateWithLocale($date, $locale, $format);
|
||||||
// Save the current locale
|
}
|
||||||
$system_locale = setlocale('LC_TIME', 0);
|
|
||||||
setlocale('LC_TIME', $locale);
|
|
||||||
$localized_date = strftime($format, $date->getTimestamp());
|
|
||||||
// Restore the locale
|
|
||||||
setlocale('LC_TIME', $system_locale);
|
|
||||||
|
|
||||||
return $localized_date;
|
return $value;
|
||||||
} else {
|
}
|
||||||
// setlocale() function not available => error
|
|
||||||
throw new SmartyPluginException("The setlocale() function is not available on your system.");
|
private function formatDateWithLocale(\DateTime $date, $locale, $format)
|
||||||
}
|
{
|
||||||
|
if (function_exists('setlocale')) {
|
||||||
|
// Save the current locale
|
||||||
|
$systemLocale = setlocale('LC_TIME', 0);
|
||||||
|
setlocale('LC_TIME', $locale);
|
||||||
|
$localizedDate = strftime($format, $date->getTimestamp());
|
||||||
|
// Restore the locale
|
||||||
|
setlocale('LC_TIME', $systemLocale);
|
||||||
|
|
||||||
|
return $localizedDate;
|
||||||
|
} else {
|
||||||
|
// setlocale() function not available => error
|
||||||
|
throw new SmartyPluginException("The setlocale() function is not available on your system.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,4 +197,4 @@ class Format extends AbstractSmartyPlugin
|
|||||||
new SmartyPluginDescriptor("function", "format_money", $this, "formatMoney")
|
new SmartyPluginDescriptor("function", "format_money", $this, "formatMoney")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -158,6 +158,23 @@ class FormatTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEmpty($render);
|
$this->assertEmpty($render);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFormatDateWithLocale()
|
||||||
|
{
|
||||||
|
$dateTime = new \DateTime();
|
||||||
|
// 2014-06-17
|
||||||
|
$dateTime->setTimestamp(1402987842);
|
||||||
|
|
||||||
|
$formatClass = new Format($this->request);
|
||||||
|
|
||||||
|
$render = $formatClass->formatDate(array(
|
||||||
|
'date' => $dateTime,
|
||||||
|
'locale' => ['fr_FR.UTF-8', 'fr_FR'],
|
||||||
|
'format' => '%e %B %Y'
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertEquals('17 juin 2014', $render);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test formatNumber without mandatory parameters
|
* test formatNumber without mandatory parameters
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user