Skip to content

Commit

Permalink
Html::price && Format::price now use Currency enum
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk committed Sep 16, 2022
1 parent 7538248 commit ecd344e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/Enums/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public function label(): string
}


public function format(): string
{
return '%value% %unit%';
}


public function color(): Color
{
return Color::Secondary;
Expand Down
11 changes: 8 additions & 3 deletions src/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@

namespace JuniWalk\Utils;

use JuniWalk\Utils\Enums\Currency;

final class Format
{
/**
* @param float $value
* @param string $unit
* @param Currency $unit
* @param int $decimals
* @return string
*/
public static function price(float $value, string $unit = '', int $decimals = 2): string
public static function price(float $value, Currency $unit, int $decimals = 2): string
{
$value = number_format($value, $decimals, ',', ' ');
return sprintf('%s %s', $value, $unit);
return strtr($unit->format(), [
'%value%' => $value,
'%unit%' => $unit->label(),
]);
}


Expand Down
7 changes: 4 additions & 3 deletions src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace JuniWalk\Utils;

use JuniWalk\Utils\Enums\Color;
use JuniWalk\Utils\Enums\Currency;
use JuniWalk\Utils\Enums\LabeledEnum;
use Nette\Localization\Translator;
use Nette\Utils\Html as NetteHtml;
Expand Down Expand Up @@ -55,14 +56,14 @@ public static function badge(

/**
* @param float $value
* @param string $unit
* @param Currency $unit
* @param int $decimals
* @return static
*/
public static function price(float $value, string $unit, int $decimals = 2): self
public static function price(float $value, Currency $unit, int $decimals = 2): self
{
$value = Format::price($value, $unit, $decimals);
return self::badge($value)->addClass('badge-pill');
return self::badge($value, $unit->color())->addClass('badge-pill');
}


Expand Down

0 comments on commit ecd344e

Please sign in to comment.