Skip to content

Commit

Permalink
Merge pull request #3270 from dereuromark/3.0-format
Browse files Browse the repository at this point in the history
Simplify Number::format()
  • Loading branch information
lorenzo committed Apr 9, 2014
2 parents 91d0e59 + a5cb49c commit 75063e3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 50 deletions.
48 changes: 17 additions & 31 deletions src/Utility/Number.php
Expand Up @@ -188,39 +188,25 @@ public static function toPercentage($value, $precision = 2, array $options = arr
/**
* Formats a number into a currency format.
*
* @param float $value A floating point number
* @param integer $options If integer then places, if string then before, if (,.-) then use it
* or array with places and before keys
* @return string formatted number
* Options:
*
* - `places` - Number of decimal places to use. ie. 2
* - `before` - The string to place before whole numbers. ie. '['
* - `after` - The string to place after decimal numbers. ie. ']'
* - `thousands` - Thousands separator ie. ','
* - `decimals` - Decimal separator symbol ie. '.'
* - `escape` - Set to false to prevent escaping
*
* @param float $value A floating point number.
* @param array $options An array with options.
* @return string Formatted number
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
*/
public static function format($value, $options = false) {
$places = 0;
if (is_int($options)) {
$places = $options;
}

$separators = array(',', '.', '-', ':');

$before = $after = null;
if (is_string($options) && !in_array($options, $separators)) {
$before = $options;
}
$thousands = ',';
if (!is_array($options) && in_array($options, $separators)) {
$thousands = $options;
}
$decimals = '.';
if (!is_array($options) && in_array($options, $separators)) {
$decimals = $options;
}

$escape = true;
if (is_array($options)) {
$defaults = array('before' => '$', 'places' => 2, 'thousands' => ',', 'decimals' => '.');
$options += $defaults;
extract($options);
}
public static function format($value, array $options = []) {
$defaults = array('before' => '', 'after' => '', 'places' => 2,
'thousands' => ',', 'decimals' => '.', 'escape' => true);
$options += $defaults;
extract($options);

$out = $before . number_format($value, $places, $decimals, $thousands) . $after;

Expand Down
9 changes: 4 additions & 5 deletions src/View/Helper/NumberHelper.php
Expand Up @@ -133,13 +133,12 @@ public function toPercentage($number, $precision = 2, array $options = array())
*
* @see \Cake\Utility\Number::format()
*
* @param float $number A floating point number
* @param mixed $options If integer then places, if string then before, if (,.-) then use it
* or array with places and before keys
* @return string formatted number
* @param float $number A floating point number.
* @param array $options Array of options.
* @return string Formatted number
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
*/
public function format($number, $options = false) {
public function format($number, array $options = []) {
return $this->_engine->format($number, $options);
}

Expand Down
28 changes: 15 additions & 13 deletions tests/TestCase/Utility/NumberTest.php
Expand Up @@ -53,34 +53,38 @@ public function tearDown() {
public function testFormat() {
$value = '100100100';

$result = $this->Number->format($value, '#');
$expected = '#100,100,100';
$result = $this->Number->format($value);
$expected = '100,100,100.00';
$this->assertEquals($expected, $result);

$result = $this->Number->format($value, array('before' => '#'));
$expected = '#100,100,100.00';
$this->assertEquals($expected, $result);

$result = $this->Number->format($value, 3);
$result = $this->Number->format($value, array('places' => 3));
$expected = '100,100,100.000';
$this->assertEquals($expected, $result);

$result = $this->Number->format($value);
$expected = '100,100,100';
$result = $this->Number->format($value, array('thousands' => '-'));
$expected = '100-100-100.00';
$this->assertEquals($expected, $result);

$result = $this->Number->format($value, '-');
$expected = '100-100-100';
$result = $this->Number->format($value, array('decimals' => ',', 'thousands' => '.'));
$expected = '100.100.100,00';
$this->assertEquals($expected, $result);

$value = 0.00001;
$result = $this->Number->format($value, array('places' => 1));
$result = $this->Number->format($value, array('places' => 1, 'before' => '$'));
$expected = '$0.0';
$this->assertEquals($expected, $result);

$value = -0.00001;
$result = $this->Number->format($value, array('places' => 1));
$result = $this->Number->format($value, array('places' => 1, 'before' => '$'));
$expected = '$-0.0';
$this->assertEquals($expected, $result);

$value = 1.23;
$options = array('decimals' => ',', 'thousands' => '.', 'before' => '', 'after' => ' €');
$options = array('decimals' => ',', 'thousands' => '.', 'after' => ' €');
$result = $this->Number->format($value, $options);
$expected = '1,23 €';
$this->assertEquals($expected, $result);
Expand Down Expand Up @@ -142,7 +146,6 @@ public function testMultibyteFormat() {
'decimals' => '&',
'places' => 3,
'escape' => false,
'before' => '',
));
$expected = '5 199 100&001';
$this->assertEquals($expected, $result);
Expand All @@ -153,7 +156,7 @@ public function testMultibyteFormat() {
'decimals' => '.a',
'escape' => false,
));
$expected = '$1,,000.a45';
$expected = '1,,000.a45';
$this->assertEquals($expected, $result);

$value = 519919827593784.00;
Expand All @@ -172,7 +175,6 @@ public function testMultibyteFormat() {
$result = Number::format($value, array(
'thousands' => '- |-| /-\ >< () |2 -',
'decimals' => '- £€€† -',
'before' => ''
));
$expected = '13- |-| /-\ &gt;&lt; () |2 -371- |-| /-\ &gt;&lt; () |2 -337- £€€† -13';
$this->assertEquals($expected, $result);
Expand Down
8 changes: 7 additions & 1 deletion tests/TestCase/View/Helper/NumberHelperTest.php
Expand Up @@ -79,7 +79,7 @@ public function tearDown() {
*/
public function testNumberHelperProxyMethodCalls() {
$methods = array(
'precision', 'toReadableSize', 'format',
'precision', 'toReadableSize',
);
$CakeNumber = $this->getMock(__NAMESPACE__ . '\NumberMock', $methods);
$Number = new NumberHelperTestObject($this->View, array('engine' => __NAMESPACE__ . '\NumberMock'));
Expand All @@ -102,6 +102,12 @@ public function testNumberHelperProxyMethodCalls() {
$CakeNumber->expects($this->at(0))->method('currency');
$Number->currency('who', 'what', array('when'));

$CakeNumber = $this->getMock(__NAMESPACE__ . '\NumberMock', array('format'));
$Number = new NumberHelperTestObject($this->View, array('engine' => __NAMESPACE__ . '\NumberMock'));
$Number->attach($CakeNumber);
$CakeNumber->expects($this->at(0))->method('format');
$Number->format('who', array('when'));

$CakeNumber = $this->getMock(__NAMESPACE__ . '\NumberMock', array('addFormat'));
$Number = new NumberHelperTestObject($this->View, array('engine' => __NAMESPACE__ . '\NumberMock'));
$Number->attach($CakeNumber);
Expand Down

0 comments on commit 75063e3

Please sign in to comment.