Skip to content
This repository has been archived by the owner on Oct 1, 2021. It is now read-only.

Commit

Permalink
chore: integrate NumberFormatter (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian authored Sep 29, 2021
1 parent 9c3d401 commit 6fb2b82
Show file tree
Hide file tree
Showing 18 changed files with 790 additions and 167 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"spatie/laravel-flash": "^1.7",
"ruafozy/mersenne-twister": "^1.3",
"spatie/laravel-package-tools": "^1.9",
"spatie/regex": "^2.0",
"konceiver/better-number-formatter": "^2.2"
"spatie/regex": "^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
Expand Down
311 changes: 156 additions & 155 deletions composer.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Components/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Closure;
use Illuminate\Support\Arr;
use Illuminate\View\Component;
use Konceiver\BetterNumberFormatter\BetterNumberFormatter;
use ARKEcosystem\UserInterface\NumberFormatter\NumberFormatter;

final class Currency extends Component
{
Expand All @@ -25,7 +25,7 @@ public function render(): Closure
$decimals = (int) Arr::get($data, 'attributes.decimals');
}

return BetterNumberFormatter::new()->formatWithCurrencyCustom(trim((string) $data['slot']), $this->currency, $decimals);
return NumberFormatter::new()->formatWithCurrencyCustom(trim((string) $data['slot']), $this->currency, $decimals);
};
}
}
4 changes: 2 additions & 2 deletions src/Components/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use Closure;
use Illuminate\View\Component;
use Konceiver\BetterNumberFormatter\BetterNumberFormatter;
use ARKEcosystem\UserInterface\NumberFormatter\NumberFormatter;

final class Number extends Component
{
public function render(): Closure
{
return function (array $data): string {
return BetterNumberFormatter::new()->formatWithDecimal((float) trim((string) $data['slot']));
return NumberFormatter::new()->formatWithDecimal((float) trim((string) $data['slot']));
};
}
}
4 changes: 2 additions & 2 deletions src/Components/Percentage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use Closure;
use Illuminate\View\Component;
use Konceiver\BetterNumberFormatter\BetterNumberFormatter;
use ARKEcosystem\UserInterface\NumberFormatter\NumberFormatter;

final class Percentage extends Component
{
public function render(): Closure
{
return function (array $data): string {
return BetterNumberFormatter::new()->formatWithPercent((float) trim((string) $data['slot']), 2);
return NumberFormatter::new()->formatWithPercent((float) trim((string) $data['slot']), 2);
};
}
}
4 changes: 2 additions & 2 deletions src/Components/ShortCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Closure;
use Illuminate\View\Component;
use Konceiver\BetterNumberFormatter\BetterNumberFormatter;
use ARKEcosystem\UserInterface\NumberFormatter\NumberFormatter;

final class ShortCurrency extends Component
{
Expand All @@ -18,7 +18,7 @@ public function __construct(string $currency)
public function render(): Closure
{
return function (array $data): string {
return BetterNumberFormatter::new()->formatWithCurrencyShort((float) trim((string) $data['slot']), $this->currency);
return NumberFormatter::new()->formatWithCurrencyShort((float) trim((string) $data['slot']), $this->currency);
};
}
}
4 changes: 2 additions & 2 deletions src/Components/ShortPercentage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use Closure;
use Illuminate\View\Component;
use Konceiver\BetterNumberFormatter\BetterNumberFormatter;
use ARKEcosystem\UserInterface\NumberFormatter\NumberFormatter;

final class ShortPercentage extends Component
{
public function render(): Closure
{
return function (array $data): string {
return BetterNumberFormatter::new()->formatWithPercent((float) trim((string) $data['slot']), 0);
return NumberFormatter::new()->formatWithPercent((float) trim((string) $data['slot']), 0);
};
}
}
109 changes: 109 additions & 0 deletions src/NumberFormatter/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace ARKEcosystem\UserInterface\NumberFormatter\Concerns;

use NumberFormatter;

trait HasAttributes
{
private array $attributes = [];

public function withParseIntOnly(int | float $value): self
{
return $this->setAttribute(NumberFormatter::PARSE_INT_ONLY, $value);
}

public function withGroupingUsed(int | float $value): self
{
return $this->setAttribute(NumberFormatter::GROUPING_USED, $value);
}

public function withDecimalAlwaysShown(int | float $value): self
{
return $this->setAttribute(NumberFormatter::DECIMAL_ALWAYS_SHOWN, $value);
}

public function withMaxIntegerDigits(int $value): self
{
return $this->setAttribute(NumberFormatter::MAX_INTEGER_DIGITS, $value);
}

public function withMinIntegerDigits(int $value): self
{
return $this->setAttribute(NumberFormatter::MIN_INTEGER_DIGITS, $value);
}

public function withIntegerDigits(int $value): self
{
return $this->setAttribute(NumberFormatter::INTEGER_DIGITS, $value);
}

public function withMaxFractionDigits(int $value): self
{
return $this->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $value);
}

public function withMinFractionDigits(int $value): self
{
return $this->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $value);
}

public function withFractionDigits(int $value): self
{
return $this->setAttribute(NumberFormatter::FRACTION_DIGITS, $value);
}

public function withMultiplier(int $value): self
{
return $this->setAttribute(NumberFormatter::MULTIPLIER, $value);
}

public function withGroupingSize(int $value): self
{
return $this->setAttribute(NumberFormatter::GROUPING_SIZE, $value);
}

// @TODO
public function withRoundingIncrement(int $value): self
{
return $this->setAttribute(NumberFormatter::ROUNDING_INCREMENT, $value);
}

public function withFormatWidth(int $value): self
{
return $this->setAttribute(NumberFormatter::FORMAT_WIDTH, $value);
}

public function withSecondaryGroupingSize(int $value): self
{
return $this->setAttribute(NumberFormatter::SECONDARY_GROUPING_SIZE, $value);
}

public function withSignificantDigitsUsed(int $value): self
{
return $this->setAttribute(NumberFormatter::SIGNIFICANT_DIGITS_USED, $value);
}

public function withMinSignificantDigits(int $value): self
{
return $this->setAttribute(NumberFormatter::MIN_SIGNIFICANT_DIGITS, $value);
}

public function withMaxSignificantDigits(int $value): self
{
return $this->setAttribute(NumberFormatter::MAX_SIGNIFICANT_DIGITS, $value);
}

public function withLenientParse(int $value): self
{
return $this->setAttribute(NumberFormatter::LENIENT_PARSE, $value);
}

private function setAttribute(int $attribute, int $value): self
{
$this->formatter->setAttribute($attribute, $value);
$this->attributes[$attribute] = $value;

return $this;
}
}
35 changes: 35 additions & 0 deletions src/NumberFormatter/Concerns/HasCustomFormatters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace ARKEcosystem\UserInterface\NumberFormatter\Concerns;

use Illuminate\Support\Str;
use ARKEcosystem\UserInterface\NumberFormatter\ResolveScientificNotation;

trait HasCustomFormatters
{
public function formatWithCurrencyShort(int | float | string $value, string $currency): string
{
$i = 0;
$units = ['', 'K', 'M', 'B', 'T'];

for ($i = 0; $value >= 1000; $i++) {
$value /= 1000;
}

return round((float) $value, 1).$units[$i].' '.strtoupper($currency);
}

public function formatWithCurrencyCustom(int | float | string $value, string $currency, ?int $decimals = null): string
{
$result = $this->formatWithDecimal((float) $value);

if (Str::contains((string) $value, ',')) {
$result = $value;
} elseif (Str::contains((string) $value, '.')) {
$result = number_format((float) ResolveScientificNotation::execute((float) $value), $decimals ?? 8);
$result = rtrim(rtrim($result, '0'), '.');
}

return rtrim($result.' '.strtoupper($currency));
}
}
91 changes: 91 additions & 0 deletions src/NumberFormatter/Concerns/HasFormatters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace ARKEcosystem\UserInterface\NumberFormatter\Concerns;

use NumberFormatter;
use RuntimeException;

trait HasFormatters
{
public function formatWithCurrency(int | float $value): string
{
return $this->withStyle(NumberFormatter::CURRENCY)->format($value);
}

public function formatWithCurrencyAccounting(int | float $value): string
{
/* @phpstan-ignore-next-line */
return $this->withStyle(NumberFormatter::CURRENCY_ACCOUNTING)->format($value);
}

public function formatWithDecimal(int | float $value): string
{
return $this->withStyle(NumberFormatter::DECIMAL)->format($value);
}

public function formatWithDefaultStyle(int | float $value): string
{
return $this->withStyle(NumberFormatter::DEFAULT_STYLE)->format($value);
}

public function formatWithDuration(int | float $value): string
{
return $this->withStyle(NumberFormatter::DURATION)->format($value);
}

public function formatWithIgnore(int | float $value): string
{
return $this->withStyle(NumberFormatter::IGNORE)->format($value);
}

public function formatWithOrdinal(int | float $value): string
{
return $this->withStyle(NumberFormatter::ORDINAL)->format($value);
}

public function formatWithPercent(int | float $value, ?int $decimals = null): string
{
if (! is_null($decimals)) {
return sprintf('%0.'.$decimals.'f', $value, $decimals).'%';
}

return $this->withStyle(NumberFormatter::PERCENT)->format($value);
}

public function formatWithScientific(int | float $value): string
{
return $this->withStyle(NumberFormatter::SCIENTIFIC)->format($value);
}

public function formatWithSpellout(int | float $value): string
{
return $this->withStyle(NumberFormatter::SPELLOUT)->format($value);
}

public function format(int | float $value): string
{
$value = $this->formatter->format($value);

if ($value === false) {
throw new RuntimeException('Failed to format the given value.');
}

return $value;
}

public function formatCurrency(int | float $value, ?string $currency = null): string
{
$value = $this->withStyle(NumberFormatter::CURRENCY)
->formatter
->formatCurrency(
$value,
$currency ?? $this->getTextAttribute(NumberFormatter::CURRENCY_CODE)
);

if ($value === false) {
throw new RuntimeException('Failed to format the given value.');
}

return $value;
}
}
28 changes: 28 additions & 0 deletions src/NumberFormatter/Concerns/HasPadding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace ARKEcosystem\UserInterface\NumberFormatter\Concerns;

use NumberFormatter;

trait HasPadding
{
public function withPaddingAfterPrefix(): self
{
return $this->setAttribute(NumberFormatter::PADDING_POSITION, NumberFormatter::PAD_AFTER_PREFIX);
}

public function withPaddingAfterSuffix(): self
{
return $this->setAttribute(NumberFormatter::PADDING_POSITION, NumberFormatter::PAD_AFTER_SUFFIX);
}

public function withPaddingBeforePrefix(): self
{
return $this->setAttribute(NumberFormatter::PADDING_POSITION, NumberFormatter::PAD_BEFORE_PREFIX);
}

public function withPaddingBeforeSuffix(): self
{
return $this->setAttribute(NumberFormatter::PADDING_POSITION, NumberFormatter::PAD_BEFORE_SUFFIX);
}
}
Loading

0 comments on commit 6fb2b82

Please sign in to comment.