Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Float and Int formatters #32

Closed
wants to merge 8 commits into from
Closed
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
49 changes: 49 additions & 0 deletions src/Collection/FloatFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace MichaelRubel\Formatters\Collection;

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.
*/
public function __construct(
public int|float|string|null $value = null,
public ?string $decimal_separator = null,
) {
}

/**
* Get the valid decimal separator or null.
*
* @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
{
switch ($decimal_separator) {
case '.':
return '.';
case ',':
return ',';
default:
return null;
}
}

/**
* Format the value as a float using the specified decimal separator.
*
* @return float The formatted float value.
*/
public function format(): float
{
return Num::float($this->value, $this->getDecimalSeparator($this->decimal_separator));
}
}
49 changes: 49 additions & 0 deletions src/Collection/IntFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace MichaelRubel\Formatters\Collection;

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.
*/
public function __construct(
public int|float|string|null $value = null,
public ?string $decimal_separator = null,
) {
}

/**
* Get the valid decimal separator or null.
*
* @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
{
switch ($decimal_separator) {
case '.':
return '.';
case ',':
return ',';
default:
return null;
}
}

/**
* Format the value as an integer using the specified decimal separator.
*
* @return int The formatted integer value.
*/
public function format(): int
{
return Num::int($this->value, $this->getDecimalSeparator($this->decimal_separator));
}
}
64 changes: 64 additions & 0 deletions tests/FloatFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace MichaelRubel\Formatters\Tests;

use MichaelRubel\Formatters\Collection\FloatFormatter;

class FloatFormatterTest extends TestCase
{
/** @test */
public function testCanFormatUsingFirstParameter()
{
$result = format('float', '10,000.00');

$this->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);
}
}
64 changes: 64 additions & 0 deletions tests/IntFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace MichaelRubel\Formatters\Tests;

use MichaelRubel\Formatters\Collection\IntFormatter;

class IntFormatterTest extends TestCase
{
/** @test */
public function testCanFormatUsingFirstParameter()
{
$result = format('int', '10,000.00');

$this->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);
}
}
Loading