Skip to content

Commit

Permalink
Merge pull request #35 from Ingewikkeld/feature/32-numberFormat
Browse files Browse the repository at this point in the history
Feature/32 number format
  • Loading branch information
Rick van der Staaij authored and Rick van der Staaij committed Dec 3, 2015
2 parents dc448b0 + 6d48b37 commit c787ee6
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/filter-rules.md
Expand Up @@ -12,6 +12,7 @@ filters, take a look at the callback filter-rule, or check out "Extending the Fi
* [int](#int)
* [letters](#letters)
* [lower](#lower)
* [numberFormat](numberformat)
* [numbers](#numbers)
* [prepend](#prepend)
* [regexReplace](#regexreplace)
Expand Down Expand Up @@ -127,6 +128,25 @@ $result = $f->filter(['name' => 'JOHN']);
// array(1) { ["name"]=> string(4) "john"
```

## NumberFormat

Formats the numbers according to the configuration

```php
$f = new Filter;
$f->all()->numberFormat(2, '.', '');
$result = $f->filter([
'price' => 5,
'discount' => 2.92847,
]);
/**
* array(2) {
* ["price"]=> string(4) "5.00"
* ["discount"]=> string(4) "2.93"
* }
*/
```

## Numbers

Filters everything but numbers out of the value
Expand Down
13 changes: 13 additions & 0 deletions src/FilterResource.php
Expand Up @@ -128,6 +128,19 @@ public function lower()
return $this->addRule(new FilterRule\Lower);
}

/**
* Returns rule that formats numbers
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
* @return $this
*/
public function numberFormat($decimals, $decimalPoint, $thousandSeperator)
{
return $this->addRule(new FilterRule\NumberFormat($decimals, $decimalPoint, $thousandSeperator));
}

/**
* Returns rule that results all numbers of a value
*
Expand Down
62 changes: 62 additions & 0 deletions src/FilterRule/NumberFormat.php
@@ -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);
}
}
107 changes: 107 additions & 0 deletions tests/FilterRule/NumberFormatTest.php
@@ -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'],
];
}
}

0 comments on commit c787ee6

Please sign in to comment.