-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
106 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
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); | ||
} | ||
} |
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,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)); | ||
} | ||
} |