From 129138743d29844c0ed5d1aa14d429059b791084 Mon Sep 17 00:00:00 2001 From: Florian Eckerstorfer Date: Mon, 21 Mar 2016 21:50:23 +0100 Subject: [PATCH 1/4] Add NumberFormatConverter --- src/Converter/NumberFormatConverter.php | 62 +++++++++++++++++++ tests/Converter/NumberFormatConverterTest.php | 46 ++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 src/Converter/NumberFormatConverter.php create mode 100644 tests/Converter/NumberFormatConverterTest.php diff --git a/src/Converter/NumberFormatConverter.php b/src/Converter/NumberFormatConverter.php new file mode 100644 index 0000000..75de3c0 --- /dev/null +++ b/src/Converter/NumberFormatConverter.php @@ -0,0 +1,62 @@ + + * + * 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 mixed + */ + 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..2098879 --- /dev/null +++ b/tests/Converter/NumberFormatConverterTest.php @@ -0,0 +1,46 @@ + + * + * 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)); + } +} From d475b12bfdc60ca0ffb17dc15c3632d8087e3d0b Mon Sep 17 00:00:00 2001 From: Florian Eckerstorfer Date: Mon, 21 Mar 2016 21:52:30 +0100 Subject: [PATCH 2/4] Fix code style --- tests/Converter/NumberFormatConverterTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Converter/NumberFormatConverterTest.php b/tests/Converter/NumberFormatConverterTest.php index 2098879..a3604c5 100644 --- a/tests/Converter/NumberFormatConverterTest.php +++ b/tests/Converter/NumberFormatConverterTest.php @@ -8,7 +8,6 @@ * 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; @@ -32,7 +31,7 @@ public function convertFormatsNumberWithDefaultParmeters() $this->assertEquals('42', $converter->convert(42.42)); } - + /** * @test * @covers Plum\Plum\Converter\NumberFormatConverter::convert() From e42c281ee5a77eb6e1e5786ea72fd32ea440e37a Mon Sep 17 00:00:00 2001 From: Florian Eckerstorfer Date: Mon, 21 Mar 2016 21:53:27 +0100 Subject: [PATCH 3/4] Fix code style --- src/Converter/NumberFormatConverter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Converter/NumberFormatConverter.php b/src/Converter/NumberFormatConverter.php index 75de3c0..44a73ec 100644 --- a/src/Converter/NumberFormatConverter.php +++ b/src/Converter/NumberFormatConverter.php @@ -8,7 +8,6 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace Plum\Plum\Converter; /** From cad74ef8fb03b2d59c9adfc846dcdfddac21b013 Mon Sep 17 00:00:00 2001 From: Florian Eckerstorfer Date: Mon, 21 Mar 2016 22:15:42 +0100 Subject: [PATCH 4/4] Improve PHPDoc --- src/Converter/NumberFormatConverter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Converter/NumberFormatConverter.php b/src/Converter/NumberFormatConverter.php index 44a73ec..1f27350 100644 --- a/src/Converter/NumberFormatConverter.php +++ b/src/Converter/NumberFormatConverter.php @@ -52,7 +52,7 @@ public function __construct($decimals = 0, $decimalPoint = '.', $thousandsSepara /** * @param mixed $item * - * @return mixed + * @return string */ public function convert($item) {