Skip to content

Commit

Permalink
Auto generated TellerFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
ruudk committed Nov 22, 2024
1 parent ddf6a86 commit c78bf1e
Show file tree
Hide file tree
Showing 4 changed files with 690 additions and 34 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
91 changes: 91 additions & 0 deletions resources/generate-teller-factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

require __DIR__.'/../vendor/autoload.php';

use Money\Currencies;
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Formatter\DecimalMoneyFormatter;
use Money\Money;
use Money\Parser\DecimalMoneyParser;
use Money\Teller;

(static function (): void {
$buffer = <<<'PHP'
<?php
declare(strict_types=1);
namespace Money;
use Money\Currencies\ISOCurrencies;
use Money\Formatter\DecimalMoneyFormatter;
use Money\Parser\DecimalMoneyParser;
/**
* This is a generated file. Do not edit it manually!
*
PHPDOC
* @psalm-immutable
*/
trait TellerFactory
{
/**
* Convenience factory method for a Teller object.
*
* <code>
* $teller = Teller::USD();
* </code>
*
* @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);
})();
34 changes: 1 addition & 33 deletions src/Teller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,12 @@

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.
*
* <code>
* $teller = Teller::USD();
* </code>
*
* @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,
Expand Down
Loading

0 comments on commit c78bf1e

Please sign in to comment.