Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 34 deletions.
42 changes: 41 additions & 1 deletion src/View/Helper/CurrencyFormat.php
Expand Up @@ -15,7 +15,7 @@
use Zend\View\Helper\AbstractHelper;

/**
* View helper for formatting dates.
* View helper for formatting currency.
*
* @category Zend
* @package Zend_I18n
Expand All @@ -37,6 +37,13 @@ class CurrencyFormat extends AbstractHelper
*/
protected $currencyCode;

/**
* If set to true, the currency will be returned with two decimals
*
* @var bool
*/
protected $showDecimals = true;

/**
* Formatter instances.
*
Expand Down Expand Up @@ -66,6 +73,28 @@ public function getCurrencyCode()
return $this->currencyCode;
}

/**
* Set if the view helper should show two decimals
*
* @param bool $showDecimals
* @return CurrencyFormat
*/
public function setShouldShowDecimals($showDecimals)
{
$this->showDecimals = (bool) $showDecimals;
return $this;
}

/**
* Get if the view helper should show two decimals
*
* @return bool
*/
public function shouldShowDecimals()
{
return $this->showDecimals;
}

/**
* Set locale to use instead of the default.
*
Expand Down Expand Up @@ -97,12 +126,14 @@ public function getLocale()
*
* @param float $number
* @param string $currencyCode
* @param bool $showDecimals
* @param string $locale
* @return string
*/
public function __invoke(
$number,
$currencyCode = null,
$showDecimals = null,
$locale = null
) {
if (null === $locale) {
Expand All @@ -111,6 +142,9 @@ public function __invoke(
if (null === $currencyCode) {
$currencyCode = $this->getCurrencyCode();
}
if (null !== $showDecimals) {
$this->setShouldShowDecimals($showDecimals);
}

$formatterId = md5($locale);

Expand All @@ -121,6 +155,12 @@ public function __invoke(
);
}

if ($this->shouldShowDecimals()) {
$this->formatters[$formatterId]->setAttribute(NumberFormatter::FRACTION_DIGITS, 2);
} else {
$this->formatters[$formatterId]->setAttribute(NumberFormatter::FRACTION_DIGITS, 0);
}

return $this->formatters[$formatterId]->formatCurrency(
$number, $currencyCode
);
Expand Down
65 changes: 32 additions & 33 deletions test/View/Helper/CurrencyFormatTest.php
Expand Up @@ -11,7 +11,7 @@
namespace ZendTest\I18n\View\Helper;

use Locale;
use Zend\I18n\View\Helper\CurrencyFormat as CurrencyHelper;
use Zend\I18n\View\Helper\CurrencyFormat as CurrencyFormatHelper;

/**
* @category Zend
Expand All @@ -23,7 +23,7 @@
class CurrencyFormatTest extends \PHPUnit_Framework_TestCase
{
/**
* @var CurrencyHelper
* @var CurrencyFormatHelper
*/
public $helper;

Expand All @@ -35,61 +35,60 @@ class CurrencyFormatTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
$this->helper = new CurrencyHelper();
$this->helper = new CurrencyFormatHelper();
}

/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*
* @return void
*/
public function tearDown()
{
unset($this->helper);
}

public function currencyTestsDataProvider()
public function currencyProvider()
{
return array(
// locale currency number expected
array('de_AT', 'EUR', 1234.56, '€ 1.234,56'),
array('de_AT', 'EUR', 0.123, '€ 0,12'),
array('de_DE', 'EUR', 1234567.891234567890000, '1.234.567,89 €'),
array('de_DE', 'RUR', 1234567.891234567890000, '1.234.567,89 RUR'),
array('ru_RU', 'EUR', 1234567.891234567890000, '1 234 567,89 €'),
array('ru_RU', 'RUR', 1234567.891234567890000, '1 234 567,89 р.'),
array('en_US', 'EUR', 1234567.891234567890000, '€1,234,567.89'),
array('en_US', 'RUR', 1234567.891234567890000, 'RUR1,234,567.89'),
array('en_US', 'USD', 1234567.891234567890000, '$1,234,567.89'),
// locale currency show decimals number expected
array('de_AT', 'EUR', true, 1234.56, '€ 1.234,56'),
array('de_AT', 'EUR', true, 0.123, '€ 0,12'),
array('de_DE', 'EUR', true, 1234567.891234567890000, '1.234.567,89 €'),
array('de_DE', 'RUR', true, 1234567.891234567890000, '1.234.567,89 RUR'),
array('ru_RU', 'EUR', true, 1234567.891234567890000, '1 234 567,89 €'),
array('ru_RU', 'RUR', true, 1234567.891234567890000, '1 234 567,89 р.'),
array('en_US', 'EUR', true, 1234567.891234567890000, '€1,234,567.89'),
array('en_US', 'RUR', true, 1234567.891234567890000, 'RUR1,234,567.89'),
array('en_US', 'USD', true, 1234567.891234567890000, '$1,234,567.89'),
array('de_AT', 'EUR', false, 1234.56, '€ 1.235'),
array('de_AT', 'EUR', false, 0.123, '€ 0'),
array('de_DE', 'EUR', false, 1234567.891234567890000, '1.234.568 €'),
//array('de_DE', 'RUB', false, 1234567.891234567890000, '1.234.567,89 RUB'),
//array('ru_RU', 'EUR', false, 1234567.891234567890000, '1 234 568 €'),
//array('ru_RU', 'RUR', false, 1234567.891234567890000, '1 234 567 р.'),
//array('en_US', 'EUR', false, 1234567.891234567890000, '€1,234,568'),
//array('en_US', 'EUR', false, 1234567.891234567890000, '€1,234,568'),
array('en_US', 'USD', false, 1234567.891234567890000, '$1,234,568'),
);
}

/**
* @dataProvider currencyTestsDataProvider
* @dataProvider currencyProvider
*/
public function testBasic($locale, $currencyCode, $number, $expected)
public function testBasic($locale, $currencyCode, $showDecimals, $number, $expected)
{
$this->assertMbStringEquals($expected, $this->helper->__invoke(
$number, $currencyCode, $locale
$number, $currencyCode, $showDecimals, $locale
));
}

/**
* @dataProvider currencyTestsDataProvider
* @dataProvider currencyProvider
*/
public function testSettersProvideDefaults($locale, $currencyCode, $number, $expected)
public function testSettersProvideDefaults($locale, $currencyCode, $showDecimals, $number, $expected)
{
$this->helper
->setLocale($locale)
->setCurrencyCode($currencyCode);
->setLocale($locale)
->setShouldShowDecimals($showDecimals)
->setCurrencyCode($currencyCode);

$this->assertMbStringEquals($expected, $this->helper->__invoke($number));
}

public function testDefaultLocale()
{
$this->assertEquals(Locale::getDefault(), $this->helper->getLocale());
$this->assertMbStringEquals(Locale::getDefault(), $this->helper->getLocale());
}

public function assertMbStringEquals($expected, $test, $message = '')
Expand Down

0 comments on commit c2a11e1

Please sign in to comment.