Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Ingewikkeld/feature/32-numberFormat
Feature/32 number format
- Loading branch information
Showing
4 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Particle. | ||
* | ||
* @link http://github.com/particle-php for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com) | ||
* @license https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License | ||
*/ | ||
namespace Particle\Filter\FilterRule; | ||
|
||
use Particle\Filter\FilterRule; | ||
|
||
/** | ||
* Class NumberFormat | ||
* | ||
* @package Particle\Filter\FilterRule | ||
*/ | ||
class NumberFormat extends FilterRule | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
protected $decimals; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $decimalPoint; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $thousandSeperator; | ||
|
||
/** | ||
* Set required params for replacement | ||
* | ||
* @param int $decimals | ||
* @param string $decimalPoint | ||
* @param string $thousandSeperator | ||
*/ | ||
public function __construct($decimals, $decimalPoint, $thousandSeperator) | ||
{ | ||
$this->decimals = intval($decimals); | ||
$this->decimalPoint = $decimalPoint; | ||
$this->thousandSeperator = $thousandSeperator; | ||
} | ||
|
||
/** | ||
* Format the numbers | ||
* | ||
* @param mixed $value | ||
* @return string | ||
*/ | ||
public function filter($value) | ||
{ | ||
if (empty($value)) { | ||
return $value; | ||
} | ||
return number_format(floatval($value), $this->decimals, $this->decimalPoint, $this->thousandSeperator); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
namespace Particle\Tests\Filter\FilterRule; | ||
|
||
use Particle\Filter\Filter; | ||
|
||
class NumberFormatTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Filter | ||
*/ | ||
protected $filter; | ||
|
||
/** | ||
* Prepare the filter | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->filter = new Filter(); | ||
} | ||
|
||
/** | ||
* @dataProvider getTwoDecimalsAndDotResult | ||
* @param string $value | ||
* @param string $filteredValue | ||
*/ | ||
public function testNumbersFilterRuleWithTwoDecimalsAndDotAsDecimalSeperator($value, $filteredValue) | ||
{ | ||
$this->filter->value('test')->numberFormat(2, '.', ''); | ||
|
||
$result = $this->filter->filter([ | ||
'test' => $value | ||
]); | ||
|
||
$this->assertEquals($filteredValue, $result['test']); | ||
} | ||
|
||
/** | ||
* @dataProvider getThreeDecimalsAndCommaResult | ||
* @param string $value | ||
* @param string $filteredValue | ||
*/ | ||
public function testNumbersFilterRuleWithThreeDecimalsAndCommaAsDecimalSeperator($value, $filteredValue) | ||
{ | ||
$this->filter->value('test')->numberFormat(3, ',', ''); | ||
|
||
$result = $this->filter->filter([ | ||
'test' => $value | ||
]); | ||
|
||
$this->assertEquals($filteredValue, $result['test']); | ||
} | ||
|
||
/** | ||
* @dataProvider getThousandSeperatorResult | ||
* @param string $value | ||
* @param string $filteredValue | ||
*/ | ||
public function testWithThousandSeperator($value, $filteredValue) | ||
{ | ||
$this->filter->value('test')->numberFormat(2, '.', ','); | ||
|
||
$result = $this->filter->filter([ | ||
'test' => $value | ||
]); | ||
|
||
$this->assertEquals($filteredValue, $result['test']); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getTwoDecimalsAndDotResult() | ||
{ | ||
return [ | ||
['', ''], | ||
['1234567890', '1234567890.00'], | ||
['4142.421', '4142.42'], | ||
['a1s2d3f4g5h6j7k8l9;0', '0.00'], | ||
]; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getThreeDecimalsAndCommaResult() | ||
{ | ||
return [ | ||
['', ''], | ||
['1234567890', '1234567890,000'], | ||
['4142.421', '4142,421'], | ||
['a1s2d3f4g5h6j7k8l9;0', '0,000'], | ||
]; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getThousandSeperatorResult() | ||
{ | ||
return [ | ||
['', ''], | ||
['1234567890', '1,234,567,890.00'], | ||
['4142.421', '4,142.42'], | ||
['a1s2d3f4g5h6j7k8l9;0', '0.00'], | ||
]; | ||
} | ||
} |