From 8198e6083af505c06537d1fd1f3b4d334fa49f29 Mon Sep 17 00:00:00 2001 From: 514sid <140138716+514sid@users.noreply.github.com> Date: Sun, 3 Sep 2023 10:41:28 +0400 Subject: [PATCH 1/8] add num helper --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 5f15a4a..fa87803 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "require": { "php": "^8.0", "ext-intl": "*", + "514sid/num": "^1.5", "illuminate/contracts": "^9.7|^10.0", "michael-rubel/laravel-enhanced-container": "^10.0|^11.0", "spatie/laravel-package-tools": "^1.12" From 5dc78f95a513c3651b60f33b640a8557827b79a5 Mon Sep 17 00:00:00 2001 From: 514sid <140138716+514sid@users.noreply.github.com> Date: Sun, 3 Sep 2023 10:41:45 +0400 Subject: [PATCH 2/8] add float formatter based on Num --- src/Collection/FloatFormatter.php | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/Collection/FloatFormatter.php diff --git a/src/Collection/FloatFormatter.php b/src/Collection/FloatFormatter.php new file mode 100644 index 0000000..2c64998 --- /dev/null +++ b/src/Collection/FloatFormatter.php @@ -0,0 +1,50 @@ +value, $this->getDecimalSeparator($this->decimal_separator)); + } +} From d85326d89874936105ef02241ab06bf2ab2def37 Mon Sep 17 00:00:00 2001 From: 514sid <140138716+514sid@users.noreply.github.com> Date: Sun, 3 Sep 2023 10:41:58 +0400 Subject: [PATCH 3/8] add float formatter tests --- tests/FloatFormatterTest.php | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/FloatFormatterTest.php diff --git a/tests/FloatFormatterTest.php b/tests/FloatFormatterTest.php new file mode 100644 index 0000000..f4ad846 --- /dev/null +++ b/tests/FloatFormatterTest.php @@ -0,0 +1,64 @@ +assertSame(10000.00, $result); + } + + /** @test */ + public function testFormatBehaviorWithNullOrEmpty() + { + $format = format('float'); + $this->assertSame(0.00, $format); + + $format = format('float', ''); + $this->assertSame(0.00, $format); + + $format = format('float', null); + $this->assertSame(0.00, $format); + } + + /** @test */ + public function testCanFormatUsingStringBinding() + { + $result = format('float', ['value' => '123,45', 'decimal_separator' => '.']); + + $this->assertSame(12345.0, $result); + } + + /** @test */ + public function testCanFormatUsingNamedArguments() + { + $result = format( + formatter: FloatFormatter::class, + value: 10000, + ); + + $this->assertSame(10000.00, $result); + } + + /** @test */ + public function testCanSetDecimalSeparatorUsingExtend() + { + $this->app->extend(FloatFormatter::class, function (FloatFormatter $formatter) { + $formatter->decimal_separator = ','; + + return $formatter; + }); + + $result = format(FloatFormatter::class, '100,55'); + + $this->assertEquals(100.55, $result); + } +} From 8a7202a6222310daa67d6b3bd0354e563de04118 Mon Sep 17 00:00:00 2001 From: 514sid <140138716+514sid@users.noreply.github.com> Date: Sun, 3 Sep 2023 10:42:19 +0400 Subject: [PATCH 4/8] add int formatter based on Num --- src/Collection/IntFormatter.php | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/Collection/IntFormatter.php diff --git a/src/Collection/IntFormatter.php b/src/Collection/IntFormatter.php new file mode 100644 index 0000000..7bfb9d7 --- /dev/null +++ b/src/Collection/IntFormatter.php @@ -0,0 +1,50 @@ +value, $this->getDecimalSeparator($this->decimal_separator)); + } +} From edd25c476dd97401162684a42052f86559bea0e2 Mon Sep 17 00:00:00 2001 From: 514sid <140138716+514sid@users.noreply.github.com> Date: Sun, 3 Sep 2023 10:42:27 +0400 Subject: [PATCH 5/8] add int formatter tests --- tests/IntFormatterTest.php | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/IntFormatterTest.php diff --git a/tests/IntFormatterTest.php b/tests/IntFormatterTest.php new file mode 100644 index 0000000..7f676b9 --- /dev/null +++ b/tests/IntFormatterTest.php @@ -0,0 +1,64 @@ +assertSame(10000, $result); + } + + /** @test */ + public function testFormatBehaviorWithNullOrEmpty() + { + $format = format('int'); + $this->assertSame(0, $format); + + $format = format('int', ''); + $this->assertSame(0, $format); + + $format = format('int', null); + $this->assertSame(0, $format); + } + + /** @test */ + public function testCanFormatUsingStringBinding() + { + $result = format('int', ['value' => '123,45', 'decimal_separator' => '.']); + + $this->assertSame(12345, $result); + } + + /** @test */ + public function testCanFormatUsingNamedArguments() + { + $result = format( + formatter: IntFormatter::class, + value: 10000.00, + ); + + $this->assertSame(10000, $result); + } + + /** @test */ + public function testCanSetDecimalSeparatorUsingExtend() + { + $this->app->extend(IntFormatter::class, function (IntFormatter $formatter) { + $formatter->decimal_separator = ','; + + return $formatter; + }); + + $result = format(IntFormatter::class, '100,55'); + + $this->assertEquals(100, $result); + } +} From ab703e71033fd2266bc928d4b6351064495550b4 Mon Sep 17 00:00:00 2001 From: 514sid <140138716+514sid@users.noreply.github.com> Date: Sun, 3 Sep 2023 11:35:10 +0400 Subject: [PATCH 6/8] add support for php 8.0 --- src/Collection/FloatFormatter.php | 11 +++++------ src/Collection/IntFormatter.php | 15 +++++++-------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Collection/FloatFormatter.php b/src/Collection/FloatFormatter.php index 2c64998..3a1e54e 100644 --- a/src/Collection/FloatFormatter.php +++ b/src/Collection/FloatFormatter.php @@ -6,7 +6,6 @@ use Num\Num; use MichaelRubel\Formatters\Formatter; -use Num\Enums\DecimalSeparator; class FloatFormatter implements Formatter { @@ -21,18 +20,18 @@ public function __construct( } /** - * Get the decimal separator as a DecimalSeparator enum value. + * Get the valid decimal separator or null. * * @param string|null $decimal_separator The decimal separator to convert. - * @return DecimalSeparator|null The DecimalSeparator enum value. + * @return string|null The valid decimal separator or null. */ - private function getDecimalSeparator(?string $decimal_separator): ?DecimalSeparator + private function getDecimalSeparator(?string $decimal_separator): ?string { switch ($decimal_separator) { case '.': - return DecimalSeparator::POINT; + return '.'; case ',': - return DecimalSeparator::COMMA; + return ','; default: return null; } diff --git a/src/Collection/IntFormatter.php b/src/Collection/IntFormatter.php index 7bfb9d7..a14e621 100644 --- a/src/Collection/IntFormatter.php +++ b/src/Collection/IntFormatter.php @@ -6,7 +6,6 @@ use Num\Num; use MichaelRubel\Formatters\Formatter; -use Num\Enums\DecimalSeparator; class IntFormatter implements Formatter { @@ -21,27 +20,27 @@ public function __construct( } /** - * Get the decimal separator as a DecimalSeparator enum value. + * Get the valid decimal separator or null. * * @param string|null $decimal_separator The decimal separator to convert. - * @return DecimalSeparator|null The DecimalSeparator enum value. + * @return string|null The valid decimal separator or null. */ - private function getDecimalSeparator(?string $decimal_separator): ?DecimalSeparator + private function getDecimalSeparator(?string $decimal_separator): ?string { switch ($decimal_separator) { case '.': - return DecimalSeparator::POINT; + return '.'; case ',': - return DecimalSeparator::COMMA; + return ','; default: return null; } } /** - * Format the value as a int using the specified decimal separator. + * Format the value as an integer using the specified decimal separator. * - * @return int The formatted int value. + * @return int The formatted integer value. */ public function format(): int { From 213f12a9bbc6a52c04bb2e2a9e2da1414b7f319d Mon Sep 17 00:00:00 2001 From: 514sid <140138716+514sid@users.noreply.github.com> Date: Sun, 3 Sep 2023 11:38:19 +0400 Subject: [PATCH 7/8] change lowest version of Num package --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index fa87803..112d01e 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "require": { "php": "^8.0", "ext-intl": "*", - "514sid/num": "^1.5", + "514sid/num": "^1.5.3", "illuminate/contracts": "^9.7|^10.0", "michael-rubel/laravel-enhanced-container": "^10.0|^11.0", "spatie/laravel-package-tools": "^1.12" From 9a9c386d2528672df000cb11fcecfa407d4b897a Mon Sep 17 00:00:00 2001 From: 514sid <140138716+514sid@users.noreply.github.com> Date: Sun, 3 Sep 2023 11:43:12 +0400 Subject: [PATCH 8/8] formatting --- src/Collection/FloatFormatter.php | 8 ++++---- src/Collection/IntFormatter.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Collection/FloatFormatter.php b/src/Collection/FloatFormatter.php index 3a1e54e..312a652 100644 --- a/src/Collection/FloatFormatter.php +++ b/src/Collection/FloatFormatter.php @@ -4,14 +4,14 @@ namespace MichaelRubel\Formatters\Collection; -use Num\Num; use MichaelRubel\Formatters\Formatter; +use Num\Num; class FloatFormatter implements Formatter { /** - * @param int|float|string|null $value The value to format. - * @param string|null $decimal_separator The decimal separator to use for formatting. + * @param int|float|string|null $value The value to format. + * @param string|null $decimal_separator The decimal separator to use for formatting. */ public function __construct( public int|float|string|null $value = null, @@ -22,7 +22,7 @@ public function __construct( /** * Get the valid decimal separator or null. * - * @param string|null $decimal_separator The decimal separator to convert. + * @param string|null $decimal_separator The decimal separator to convert. * @return string|null The valid decimal separator or null. */ private function getDecimalSeparator(?string $decimal_separator): ?string diff --git a/src/Collection/IntFormatter.php b/src/Collection/IntFormatter.php index a14e621..f1ea343 100644 --- a/src/Collection/IntFormatter.php +++ b/src/Collection/IntFormatter.php @@ -4,14 +4,14 @@ namespace MichaelRubel\Formatters\Collection; -use Num\Num; use MichaelRubel\Formatters\Formatter; +use Num\Num; class IntFormatter implements Formatter { /** - * @param int|float|string|null $value The value to format. - * @param string|null $decimal_separator The decimal separator to use for formatting. + * @param int|float|string|null $value The value to format. + * @param string|null $decimal_separator The decimal separator to use for formatting. */ public function __construct( public int|float|string|null $value = null, @@ -22,7 +22,7 @@ public function __construct( /** * Get the valid decimal separator or null. * - * @param string|null $decimal_separator The decimal separator to convert. + * @param string|null $decimal_separator The decimal separator to convert. * @return string|null The valid decimal separator or null. */ private function getDecimalSeparator(?string $decimal_separator): ?string