diff --git a/composer.json b/composer.json index 2984f3fd..903a9a05 100644 --- a/composer.json +++ b/composer.json @@ -99,7 +99,8 @@ "update-currencies": [ "cp vendor/moneyphp/iso-currencies/resources/current.php resources/currency.php", "cp vendor/moneyphp/crypto-currencies/resources/binance.php resources/binance.php", - "php resources/generate-money-factory.php" + "php resources/generate-money-factory.php", + "php resources/generate-teller-factory.php" ] } } diff --git a/resources/generate-teller-factory.php b/resources/generate-teller-factory.php new file mode 100644 index 00000000..77d2aede --- /dev/null +++ b/resources/generate-teller-factory.php @@ -0,0 +1,93 @@ + + * $teller = Teller::USD(); + * + * + * @param non-empty-string $method + * @param array{0?: int} $arguments + */ + public static function __callStatic(string $method, array $arguments): Teller + { + $currency = new Currency($method); + $currencies = new ISOCurrencies(); + $parser = new DecimalMoneyParser($currencies); + $formatter = new DecimalMoneyFormatter($currencies); + $roundingMode = empty($arguments) + ? Money::ROUND_HALF_UP + : (int) array_shift($arguments); + + return new Teller( + $currency, + $parser, + $formatter, + $roundingMode + ); + } +} + +PHP; + + $methodBuffer = ''; + + $iterator = new Currencies\AggregateCurrencies([ + new Currencies\ISOCurrencies(), + new Currencies\BitcoinCurrencies(), + new Currencies\CryptoCurrencies(), + ]); + + $currencies = array_unique([...$iterator]); + usort($currencies, static fn (Currency $a, Currency $b): int => strcmp($a->getCode(), $b->getCode())); + + /** @var Currency[] $currencies */ + foreach ($currencies as $currency) { + $code = $currency->getCode(); + if (is_numeric($code[0])) { + preg_match('/^([0-9]*)(.*?)$/', $code, $extracted); + + $formatter = new \NumberFormatter('en', \NumberFormatter::SPELLOUT); + $code = strtoupper(preg_replace('/\s+/', '', $formatter->format($extracted[1])) . $extracted[2]); + } + + $methodBuffer .= sprintf(" * @method static Teller %s(int \$roundingMode = Money::ROUND_HALF_UP)\n", $code); + } + + $buffer = str_replace('PHPDOC', rtrim($methodBuffer), $buffer); + + file_put_contents(__DIR__.'/../src/TellerFactory.php', $buffer); +})(); diff --git a/src/Teller.php b/src/Teller.php index 48e29413..48e35fe5 100644 --- a/src/Teller.php +++ b/src/Teller.php @@ -4,44 +4,11 @@ namespace Money; -use Money\Currencies\ISOCurrencies; -use Money\Formatter\DecimalMoneyFormatter; -use Money\Parser\DecimalMoneyParser; - -use function array_shift; use function is_float; final class Teller { - /** - * Convenience factory method for a Teller object. - * - * - * $teller = Teller::USD(); - * - * - * @param non-empty-string $method - * @param array{0?: int} $arguments - * - * @return Teller - */ - public static function __callStatic(string $method, array $arguments): self - { - $currency = new Currency($method); - $currencies = new ISOCurrencies(); - $parser = new DecimalMoneyParser($currencies); - $formatter = new DecimalMoneyFormatter($currencies); - $roundingMode = empty($arguments) - ? Money::ROUND_HALF_UP - : (int) array_shift($arguments); - - return new self( - $currency, - $parser, - $formatter, - $roundingMode - ); - } + use TellerFactory; public function __construct( private readonly Currency $currency, diff --git a/src/TellerFactory.php b/src/TellerFactory.php new file mode 100644 index 00000000..cb4e7292 --- /dev/null +++ b/src/TellerFactory.php @@ -0,0 +1,598 @@ + + * $teller = Teller::USD(); + * + * + * @param non-empty-string $method + * @param array{0?: int} $arguments + */ + public static function __callStatic(string $method, array $arguments): Teller + { + $currency = new Currency($method); + $currencies = new ISOCurrencies(); + $parser = new DecimalMoneyParser($currencies); + $formatter = new DecimalMoneyFormatter($currencies); + $roundingMode = empty($arguments) + ? Money::ROUND_HALF_UP + : (int) array_shift($arguments); + + return new Teller( + $currency, + $parser, + $formatter, + $roundingMode + ); + } +}