This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: integrate
NumberFormatter
(#669)
- Loading branch information
1 parent
9c3d401
commit 6fb2b82
Showing
18 changed files
with
790 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.