|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Derafu: Enum - Yet Another List of Enumerations for PHP. |
| 7 | + * |
| 8 | + * Copyright (c) 2026 Esteban De La Fuente Rubio / Derafu <https://www.derafu.dev> |
| 9 | + * Licensed under the MIT License. |
| 10 | + * See LICENSE file for more details. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Derafu\Enum\Contract; |
| 14 | + |
| 15 | +/** |
| 16 | + * Contract for currency enums. |
| 17 | + * |
| 18 | + * Implement this interface in your own backed enum to make it compatible with |
| 19 | + * any component that formats, validates, or renders monetary amounts without |
| 20 | + * depending on the default Currency enum. |
| 21 | + * |
| 22 | + * @example |
| 23 | + * enum GameCurrency: string implements CurrencyInterface |
| 24 | + * { |
| 25 | + * case Gold = 'GOLD'; |
| 26 | + * case Silver = 'SILVER'; |
| 27 | + * |
| 28 | + * public function getCode(): string { return $this->value; } |
| 29 | + * // ... |
| 30 | + * } |
| 31 | + */ |
| 32 | +interface CurrencyInterface |
| 33 | +{ |
| 34 | + /** |
| 35 | + * Returns the currency code (e.g. "USD", "EUR", "CLP"). |
| 36 | + */ |
| 37 | + public function getCode(): string; |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns the human-readable name of the currency. |
| 41 | + * |
| 42 | + * @param string $language ISO 639 language code (e.g. 'en', 'es'). |
| 43 | + */ |
| 44 | + public function getName(string $language = 'en'): string; |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns the currency symbol (e.g. "$", "€", "₿"). |
| 48 | + */ |
| 49 | + public function getSymbol(): string; |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns the number of decimal places used by the currency. |
| 53 | + * |
| 54 | + * For example, USD uses 2 decimals, CLP uses 0. |
| 55 | + */ |
| 56 | + public function getDecimals(): int; |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns the decimal separator character used by the currency. |
| 60 | + * |
| 61 | + * For example, "." for USD, "," for EUR in some locales. |
| 62 | + */ |
| 63 | + public function getDecimalSeparator(): string; |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns the thousands separator character used by the currency. |
| 67 | + * |
| 68 | + * For example, "," for USD, "." for CLP. |
| 69 | + */ |
| 70 | + public function getThousandsSeparator(): string; |
| 71 | + |
| 72 | + /** |
| 73 | + * Validates that an amount respects the decimal precision of the currency. |
| 74 | + * |
| 75 | + * @param int|float $amount |
| 76 | + */ |
| 77 | + public function isValidAmount(int|float $amount): bool; |
| 78 | + |
| 79 | + /** |
| 80 | + * Rounds an amount to the decimal precision of the currency. |
| 81 | + * |
| 82 | + * @param int|float $amount |
| 83 | + * @return int|float Integer when the currency has 0 decimals, float otherwise. |
| 84 | + */ |
| 85 | + public function round(int|float $amount): int|float; |
| 86 | + |
| 87 | + /** |
| 88 | + * Formats an amount as a string using the currency's separators. |
| 89 | + * |
| 90 | + * Does not include the symbol. Use render() for the full representation. |
| 91 | + * |
| 92 | + * @param int|float $amount |
| 93 | + */ |
| 94 | + public function format(int|float $amount): string; |
| 95 | + |
| 96 | + /** |
| 97 | + * Renders a fully formatted amount including the currency symbol. |
| 98 | + * |
| 99 | + * Uses the template returned by getTemplate() to place symbol and amount. |
| 100 | + * |
| 101 | + * @param int|float $amount |
| 102 | + */ |
| 103 | + public function render(int|float $amount): string; |
| 104 | + |
| 105 | + /** |
| 106 | + * Returns the template used to render amounts. |
| 107 | + * |
| 108 | + * The template may contain the placeholders {{symbol}} and {{amount}}. |
| 109 | + * For example: "{{symbol}} {{amount}}" or "{{amount}} {{symbol}}". |
| 110 | + */ |
| 111 | + public function getTemplate(): string; |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns an array with all the currency metadata. |
| 115 | + * |
| 116 | + * @return array<string, mixed> |
| 117 | + */ |
| 118 | + public function toArray(): array; |
| 119 | +} |
0 commit comments