Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}