diff --git a/src/Converter/NumberFormatConverter.php b/src/Converter/NumberFormatConverter.php new file mode 100644 index 0000000..1f27350 --- /dev/null +++ b/src/Converter/NumberFormatConverter.php @@ -0,0 +1,61 @@ + + * + * 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); + } +} diff --git a/tests/Converter/NumberFormatConverterTest.php b/tests/Converter/NumberFormatConverterTest.php new file mode 100644 index 0000000..a3604c5 --- /dev/null +++ b/tests/Converter/NumberFormatConverterTest.php @@ -0,0 +1,45 @@ + + * + * 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)); + } +}