Skip to content

Commit

Permalink
Merge cad74ef into 17ada55
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Eckerstorfer committed Mar 21, 2016
2 parents 17ada55 + cad74ef commit 5785a8b
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/Converter/NumberFormatConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* This file is part of plumphp/plum.
*
* (c) Florian Eckerstorfer <florian@eckerstorfer.co>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plum\Plum\Converter;

/**
* NumberFormatConverter.
*
* @author Florian Eckerstorfer
* @copyright 2014-2016 Florian Eckerstorfer
*/
class NumberFormatConverter implements ConverterInterface
{
/**
* @var int
*/
protected $decimals;

/**
* @var string
*/
protected $decimalPoint;

/**
* @var string
*/
protected $thousandsSeparator;

/**
* NumberFormatConverter constructor.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandsSeparator
*
* @codeCoverageIgnore
*/
public function __construct($decimals = 0, $decimalPoint = '.', $thousandsSeparator = ',')
{
$this->decimals = $decimals;
$this->decimalPoint = $decimalPoint;
$this->thousandsSeparator = $thousandsSeparator;
}

/**
* @param mixed $item
*
* @return string
*/
public function convert($item)
{
return number_format($item, $this->decimals, $this->decimalPoint, $this->thousandsSeparator);
}
}
45 changes: 45 additions & 0 deletions tests/Converter/NumberFormatConverterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* This file is part of plumphp/plum.
*
* (c) Florian Eckerstorfer <florian@eckerstorfer.co>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plum\Plum\Converter;

use PHPUnit_Framework_TestCase;

/**
* NumberFormatConverterTest.
*
* @author Florian Eckerstorfer
* @copyright 2014-2016 Florian Eckerstorfer
* @group unit
*/
class NumberFormatConverterTest extends PHPUnit_Framework_TestCase
{
/**
* @test
* @covers Plum\Plum\Converter\NumberFormatConverter::convert()
*/
public function convertFormatsNumberWithDefaultParmeters()
{
$converter = new NumberFormatConverter();

$this->assertEquals('42', $converter->convert(42.42));
}

/**
* @test
* @covers Plum\Plum\Converter\NumberFormatConverter::convert()
*/
public function convertFormatsNumber()
{
$converter = new NumberFormatConverter(2, ',', '.');

$this->assertEquals('10.042,11', $converter->convert(10042.112));
}
}

0 comments on commit 5785a8b

Please sign in to comment.