generate-money-factory.php
62 lines
| 1 | <?php |
| 2 | |
| 3 | require __DIR__.'/../vendor/autoload.php'; |
| 4 | |
| 5 | use Money\Currencies; |
| 6 | |
| 7 | $buffer = <<<PHP |
| 8 | <?php |
| 9 | |
| 10 | namespace Money; |
| 11 | |
| 12 | /** |
| 13 | * This is a generated file. Do not edit it manually! |
| 14 | * |
| 15 | PHPDOC |
| 16 | */ |
| 17 | trait MoneyFactory |
| 18 | { |
| 19 | /** |
| 20 | * Convenience factory method for a Money object. |
| 21 | * |
| 22 | * <code> |
| 23 | * \$fiveDollar = Money::USD(500); |
| 24 | * </code> |
| 25 | * |
| 26 | * @param string \$method |
| 27 | * @param array \$arguments |
| 28 | * |
| 29 | * @return Money |
| 30 | * |
| 31 | * @throws \InvalidArgumentException If amount is not integer(ish) |
| 32 | */ |
| 33 | public static function __callStatic(\$method, \$arguments) |
| 34 | { |
| 35 | return new Money(\$arguments[0], new Currency(\$method)); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | PHP; |
| 40 | |
| 41 | $methodBuffer = ''; |
| 42 | |
| 43 | $currencies = new Currencies\AggregateCurrencies([ |
| 44 | new Currencies\ISOCurrencies(), |
| 45 | new Currencies\BitcoinCurrencies(), |
| 46 | ]); |
| 47 | |
| 48 | $currencies = iterator_to_array($currencies); |
| 49 | |
| 50 | usort($currencies, function (\Money\Currency $a, \Money\Currency $b) { |
| 51 | return strcmp($a->getCode(), $b->getCode()); |
| 52 | }); |
| 53 | |
| 54 | /** @var \Money\Currency[] $currencies */ |
| 55 | foreach ($currencies as $currency) { |
| 56 | $methodBuffer .= sprintf(" * @method static Money %s(string|int \$amount)\n", $currency->getCode()); |
| 57 | } |
| 58 | |
| 59 | $buffer = str_replace('PHPDOC', rtrim($methodBuffer), $buffer); |
| 60 | |
| 61 | file_put_contents(__DIR__.'/../src/MoneyFactory.php', $buffer); |
| 62 |