Skip to content

Core Concepts

Currency models every financial operation around immutable value objects, exact arbitrary-precision decimal math, and locked historical rate records.

flowchart LR
  Feed["ECB / TCMB / Custom Provider"] --> Transport["WireHttpTransport (SSRF & DNS Pinning)"]
  Transport --> Sync["CurrencyRateSyncService (CurrencyWriteLock)"]
  Sync --> Spot["currency-currencies (8dp Spot Rates + Spread)"]
  Sync --> Hist["currency-rate-history (Locked Daily Snapshots)"]
  Spot --> Conv["CurrencyConverter + DecimalMath (ext-bcmath)"]
  Hist --> Conv
  Conv --> Money["Money (Integer Minor Units)"]
  Money --> Format["MoneyFormatter (ext-intl) + VisitorCurrencyResolver"]
  • Money (Totoglu\Currency\Value\Money): Stores integer $amountMinor, 3-letter uppercase ISO 4217 $currency, and $minorUnits (0..8). Money::fromMajor() accepts string|int and intentionally rejects float to prevent IEEE-754 drift.
  • RoundingMode (Totoglu\Currency\Domain\RoundingMode): Enum supporting HalfUp (half_up), HalfEven (half_even), HalfDown (half_down), Ceil (ceil), and Floor (floor).
  • DecimalMath (Totoglu\Currency\Support\DecimalMath): Arbitrary-precision ext-bcmath calculator bounded to 128 mantissa digits, [-100, +100] scientific exponents, and <= 256 scale shifts to prevent memory DoS.
  • CurrencyDefinition, ConversionResult, ExchangeRateBatch, SyncResult: Typed readonly DTOs returned by converter and sync operations.

Three Entries Blueprints (CurrencyBlueprints)

Section titled “Three Entries Blueprints (CurrencyBlueprints)”

Currency stores all configuration and FX history inside three Entries blueprints with zero custom SQL tables:

Blueprint Type Purpose
currency-settings Singleton Active provider (ecb, tcmb, manual, or custom), tcmbRateType, autoSyncEnabled, syncIntervalHours (1..168), defaultRoundingMode, allowVisitorOverride, visitorCookieName (pw_currency), visitorCookieDays (1..365), historyRetentionDays (0..3650), and lastSync* metadata.
currency-currencies Collection Managed ISO 4217 currencies with symbol, minor_units, 8-decimal exchange_rate, spread_percent, rounding_increment, rate_source (auto, manual, or provider key), is_base, and sort_order.
currency-rate-history Collection (locked: true) Immutable historical FX snapshots keyed by provider, base currency, quote currency, and YYYY-MM-DD effective date.

Base-currency transitions (setBaseCurrency()) and rate sync writes (syncRates(), recordBatch()) execute inside CurrencyWriteLock::run(), which acquires a re-entrant database-scoped lock (GET_LOCK on MySQL, pg_advisory_xact_lock on PostgreSQL, or transaction serialization on SQLite) while performing network fetches outside the lock.

ProcessCurrency registers two dedicated React data providers under prefix currency:

  • CurrenciesDataProvider (currency:currencies): Powers the Currencies table with ★ Base decoration, inline/quick-edit guards (protecting code and the active base row from deletion or unpublishing), and currency-admin trash isolation.
  • RateHistoryDataProvider (currency:history): Always-locked read-only viewport exposing historical snapshots with CSV/JSON export gated by currency-view.

Next steps