allow vendord
All checks were successful
Build, Push and Deploy / build-and-push (push) Successful in 3m3s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 16s
Build, Push and Deploy / deploy-staging (push) Successful in 32s
Build, Push and Deploy / deploy-production (push) Has been skipped
Build, Push and Deploy / cleanup (push) Successful in 1s

This commit is contained in:
2026-03-30 14:54:57 +07:00
parent 66aed7c4e8
commit b5e3a778ce
21316 changed files with 2777892 additions and 13 deletions

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Money\Formatter;
use Money\Exception\FormatterException;
use Money\Money;
use Money\MoneyFormatter;
/**
* Formats a Money object using other Money formatters.
*/
final class AggregateMoneyFormatter implements MoneyFormatter
{
/**
* @var MoneyFormatter[] indexed by currency code
* @phpstan-var non-empty-array<non-empty-string, MoneyFormatter> indexed by currency code
*/
private array $formatters;
/**
* @param MoneyFormatter[] $formatters indexed by currency code
* @phpstan-param non-empty-array<non-empty-string, MoneyFormatter> $formatters indexed by currency code
*/
public function __construct(array $formatters)
{
$this->formatters = $formatters;
}
public function format(Money $money): string
{
$currencyCode = $money->getCurrency()->getCode();
if (isset($this->formatters[$currencyCode])) {
return $this->formatters[$currencyCode]->format($money);
}
if (isset($this->formatters['*'])) {
return $this->formatters['*']->format($money);
}
throw new FormatterException('No formatter found for currency ' . $currencyCode);
}
}

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Money\Formatter;
use Money\Currencies;
use Money\Currencies\BitcoinCurrencies;
use Money\Exception\FormatterException;
use Money\Money;
use Money\MoneyFormatter;
use Money\Number;
use function str_pad;
use function strlen;
use function strpos;
use function substr;
/**
* Formats Money to Bitcoin currency.
*/
final class BitcoinMoneyFormatter implements MoneyFormatter
{
public function __construct(private readonly int $fractionDigits, private readonly Currencies $currencies)
{
}
public function format(Money $money): string
{
if ($money->getCurrency()->getCode() !== BitcoinCurrencies::CODE) {
throw new FormatterException('Bitcoin Formatter can only format Bitcoin currency');
}
$valueBase = $money->getAmount();
$negative = false;
if ($valueBase[0] === '-') {
$negative = true;
$valueBase = substr($valueBase, 1);
}
$subunit = $this->currencies->subunitFor($money->getCurrency());
$valueBase = Number::roundMoneyValue($valueBase, $this->fractionDigits, $subunit);
$valueLength = strlen($valueBase);
if ($valueLength > $subunit) {
$formatted = substr($valueBase, 0, $valueLength - $subunit);
if ($subunit) {
$formatted .= '.';
$formatted .= substr($valueBase, $valueLength - $subunit);
}
} else {
$formatted = '0.' . str_pad('', $subunit - $valueLength, '0') . $valueBase;
}
if ($this->fractionDigits === 0) {
$formatted = substr($formatted, 0, (int) strpos($formatted, '.'));
} elseif ($this->fractionDigits > $subunit) {
$formatted .= str_pad('', $this->fractionDigits - $subunit, '0');
} elseif ($this->fractionDigits < $subunit) {
$lastDigit = (int) strpos($formatted, '.') + $this->fractionDigits + 1;
$formatted = substr($formatted, 0, $lastDigit);
}
$formatted = BitcoinCurrencies::SYMBOL . $formatted;
if ($negative) {
$formatted = '-' . $formatted;
}
return $formatted;
}
}

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Money\Formatter;
use Money\Currencies;
use Money\Money;
use Money\MoneyFormatter;
use function assert;
use function str_pad;
use function strlen;
use function substr;
/**
* Formats a Money object as a decimal string.
*/
final class DecimalMoneyFormatter implements MoneyFormatter
{
public function __construct(private readonly Currencies $currencies)
{
}
/**
* @phpstan-return numeric-string
*/
public function format(Money $money): string
{
$valueBase = $money->getAmount();
$negative = $valueBase[0] === '-';
if ($negative) {
$valueBase = substr($valueBase, 1);
}
$subunit = $this->currencies->subunitFor($money->getCurrency());
$valueLength = strlen($valueBase);
if ($valueLength > $subunit) {
$formatted = substr($valueBase, 0, $valueLength - $subunit);
$decimalDigits = substr($valueBase, $valueLength - $subunit);
if (strlen($decimalDigits) > 0) {
$formatted .= '.' . $decimalDigits;
}
} else {
$formatted = '0.' . str_pad('', $subunit - $valueLength, '0') . $valueBase;
}
if ($negative) {
$formatted = '-' . $formatted;
}
assert($formatted !== '');
return $formatted;
}
}

View File

@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace Money\Formatter;
use Money\Currencies;
use Money\Money;
use Money\MoneyFormatter;
use NumberFormatter;
use function assert;
use function is_string;
use function str_pad;
use function strlen;
use function substr;
/**
* Formats a Money object using intl extension.
*/
final class IntlLocalizedDecimalFormatter implements MoneyFormatter
{
public function __construct(private readonly NumberFormatter $formatter, private readonly Currencies $currencies)
{
}
public function format(Money $money): string
{
$valueBase = $money->getAmount();
$negative = $valueBase[0] === '-';
if ($negative) {
$valueBase = substr($valueBase, 1);
}
$subunit = $this->currencies->subunitFor($money->getCurrency());
$valueLength = strlen($valueBase);
if ($valueLength > $subunit) {
$formatted = substr($valueBase, 0, $valueLength - $subunit);
$decimalDigits = substr($valueBase, $valueLength - $subunit);
if (strlen($decimalDigits) > 0) {
$formatted .= '.' . $decimalDigits;
}
} else {
$formatted = '0.' . str_pad('', $subunit - $valueLength, '0') . $valueBase;
}
if ($negative) {
$formatted = '-' . $formatted;
}
$formatted = $this->formatter->format((float) $formatted);
assert(is_string($formatted) && $formatted !== '');
return $formatted;
}
}

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Money\Formatter;
use Money\Currencies;
use Money\Money;
use Money\MoneyFormatter;
use NumberFormatter;
use function assert;
use function str_pad;
use function strlen;
use function substr;
/**
* Formats a Money object using intl extension.
*/
final class IntlMoneyFormatter implements MoneyFormatter
{
public function __construct(private readonly NumberFormatter $formatter, private readonly Currencies $currencies)
{
}
public function format(Money $money): string
{
$valueBase = $money->getAmount();
$negative = $valueBase[0] === '-';
if ($negative) {
$valueBase = substr($valueBase, 1);
}
$subunit = $this->currencies->subunitFor($money->getCurrency());
$valueLength = strlen($valueBase);
if ($valueLength > $subunit) {
$formatted = substr($valueBase, 0, $valueLength - $subunit);
$decimalDigits = substr($valueBase, $valueLength - $subunit);
if (strlen($decimalDigits) > 0) {
$formatted .= '.' . $decimalDigits;
}
} else {
$formatted = '0.' . str_pad('', $subunit - $valueLength, '0') . $valueBase;
}
if ($negative) {
$formatted = '-' . $formatted;
}
$formatted = $this->formatter->formatCurrency((float) $formatted, $money->getCurrency()->getCode());
assert($formatted !== '');
return $formatted;
}
}