Skip to content

Implement MoneyFormatter with Intl support #11

@henriquemoody

Description

@henriquemoody

Summary

Create a MoneyFormatter that uses PHP's Intl extension (NumberFormatter) for currency formatting with a clean, minimal API.

Proposed Design

The formatter should leverage NumberFormatter for locale-aware currency formatting while keeping a simple interface:

use Respect\StringFormatter\MoneyFormatter;

// Simple case - uses default locale
$formatter = new MoneyFormatter('USD');
echo $formatter->format('1234.56');
// Output: "$1,234.56"

// With custom locale
$formatter = new MoneyFormatter('BRL', 'pt_BR');
echo $formatter->format('1234.56');
// Output: "R$ 1.234,56"

// With custom precision
$formatter = new MoneyFormatter('JPY', precision: 0);
echo $formatter->format('1234.56');
// Output: "¥1,235"

Constructor Signature

public function __construct(
    string $currency,
    string|null $locale = null,
    int|null $precision = null
)
  • $currency: ISO 4217 currency code (e.g., 'USD', 'EUR', 'BRL')
  • $locale: Optional locale for formatting (defaults to system locale)
  • $precision: Optional decimal precision (defaults to currency's default)

Key Features

  1. Intl integration: Use NumberFormatter::CURRENCY for all formatting
  2. Locale-aware: Respect locale-specific formatting (symbol position, separators)
  3. Precision control: Override currency defaults when needed
  4. Error handling: Validate currency codes and throw InvalidFormatterException
  5. Currency validation: Use NumberFormatter to verify valid currency codes

Questions/Decisions Needed

  • Should we support currency code-only output (no symbol)?
  • Should we support negative numbers with specific formatting?
  • Should we format as accounting style for negatives?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions