Commit f6b7f8c7 authored by Thorsten Buss's avatar Thorsten Buss
Browse files

* add blank between sign and amount with option to disable this default behaviour

parent 998414e5
......@@ -344,10 +344,12 @@ class Money
* thousands_separator => overwrite the currency thousands_separator
* decimal_mark => overwrite the currency decimal_mark
* with_currency => append currency ISOCODE
* no_blank_separator => remove the blank separator between amount and symbol/currency
* @return mixed|string
*/
public function format($params = array()) {
// spearator between amount andsymbol/currency
$sep = (array_key_exists('no_blank_separator', $params) && $params['no_blank_separator']) ? '' : ' ';
// show 'free'
if ($this->amount === 0) {
if (is_string($params['display_free']))
......@@ -395,8 +397,8 @@ class Money
// combine symbol and formatted amount
if (isset($symbolValue) && !empty($symbolValue)) {
$formatted = $symbolPosition === 'before'
? "$symbolValue$formatted"
: "$formatted$symbolValue";
? "$symbolValue$sep$formatted"
: "$formatted$sep$symbolValue";
}
if (isset($params['decimal_mark']) && $params['decimal_mark'] && $params['decimal_mark'] !== $this->currency->getDecimalMark()) {
......@@ -417,9 +419,9 @@ class Money
if (isset($params['with_currency']) && $params['with_currency']) {
if (isset($params['html']) && $params['html'])
$formatted .= '<span class="currency">'. $this->currency->__toString(). '</span>';
$formatted .= $sep.'<span class="currency">'. $this->currency->__toString(). '</span>';
else
$formatted .= $this->currency->__toString();
$formatted .= $sep.$this->currency->__toString();
}
return $formatted;
......
......@@ -16,13 +16,13 @@ class MoneyFormaterTest extends MoneyTestCase {
public function testFormater() {
$m = new MoneyFormater('USD');
$this->assertEquals('$1,234.56', $m->format(123456));
$this->assertEquals('$1,234', $m->format(123456, array('no_cents' => true)));
$this->assertEquals('$ 1,234.56', $m->format(123456));
$this->assertEquals('$ 1,234', $m->format(123456, array('no_cents' => true)));
// with defaultParams
$m = new MoneyFormater('USD', array('symbol' => '~'));
$this->assertEquals('~1,234.56', $m->format(123456));
$this->assertEquals('~1,234', $m->format(123456, array('no_cents' => true)));
$this->assertEquals('~ 1,234.56', $m->format(123456));
$this->assertEquals('~ 1,234', $m->format(123456, array('no_cents' => true)));
}
public function testLegibly() {
......
......@@ -300,19 +300,28 @@ class MoneyTest extends MoneyTestCase {
/** @var Money $m2 */
$m2 = Money::USD(123400); // $1234.00
$this->assertEquals('$1,234.56', $m1->format());
$this->assertEquals('$1,234~56', $m1->format(array('decimal_mark' => '~')));
$this->assertEquals('$1_234.56', $m1->format(array('thousands_separator' => '_')));
$this->assertEquals('$1,234.56USD', $m1->format(array('with_currency' => true)));
$this->assertEquals('<span class="symbol">$</span><span class="amount">1,234.56</span><span class="currency">USD</span>', $m1->format(array('with_currency' => true, 'html' => true)));
$this->assertEquals('$1,234', $m1->format(array('no_cents' => true)));
$this->assertEquals('$1,234.56', $m1->format(array('no_cents_if_zero' => true)));
$this->assertEquals('$1,234', $m2->format(array('no_cents_if_zero' => true)));
$this->assertEquals('$1,234.56', $m1->format(array('no_cents_if_zero' => false)));
$this->assertEquals('$1,234.00', $m2->format(array('no_cents_if_zero' => false)));
$this->assertEquals('1,234.56$', $m1->format(array('symbol_position' => 'after')));
$this->assertEquals('$ 1,234.56', $m1->format());
// different options
$this->assertEquals('$ 1,234~56', $m1->format(array('decimal_mark' => '~')));
$this->assertEquals('$ 1_234.56', $m1->format(array('thousands_separator' => '_')));
$this->assertEquals('$ 1,234.56 USD', $m1->format(array('with_currency' => true)));
$this->assertEquals('<span class="symbol">$</span> <span class="amount">1,234.56</span> <span class="currency">USD</span>', $m1->format(array('with_currency' => true, 'html' => true)));
$this->assertEquals('1,234.56 $', $m1->format(array('symbol_position' => 'after')));
// without cents
$this->assertEquals('$ 1,234', $m1->format(array('no_cents' => true)));
$this->assertEquals('$ 1,234.56', $m1->format(array('no_cents_if_zero' => true)));
$this->assertEquals('$ 1,234', $m2->format(array('no_cents_if_zero' => true)));
$this->assertEquals('$ 1,234.56', $m1->format(array('no_cents_if_zero' => false)));
$this->assertEquals('$ 1,234.00', $m2->format(array('no_cents_if_zero' => false)));
// mixed with no_blank_separator
$this->assertEquals('1,234.56$', $m1->format(array('symbol_position' => 'after', 'no_blank_separator' => true)));
$this->assertEquals('$1,234.56', $m1->format(array('no_blank_separator' => true)));
$this->assertEquals('$1,234.56USD', $m1->format(array('with_currency' => true, 'no_blank_separator' => true)));
$this->assertEquals('<span class="symbol">$</span><span class="amount">1,234.56</span><span class="currency">USD</span>', $m1->format(array('with_currency' => true, 'html' => true, 'no_blank_separator' => true)));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment