diff --git a/composer.json b/composer.json index 5f15a4a..112d01e 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "require": { "php": "^8.0", "ext-intl": "*", + "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" diff --git a/src/Collection/FloatFormatter.php b/src/Collection/FloatFormatter.php new file mode 100644 index 0000000..312a652 --- /dev/null +++ b/src/Collection/FloatFormatter.php @@ -0,0 +1,49 @@ +value, $this->getDecimalSeparator($this->decimal_separator)); + } +} diff --git a/src/Collection/IntFormatter.php b/src/Collection/IntFormatter.php new file mode 100644 index 0000000..f1ea343 --- /dev/null +++ b/src/Collection/IntFormatter.php @@ -0,0 +1,49 @@ +value, $this->getDecimalSeparator($this->decimal_separator)); + } +} 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); + } +} 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); + } +}