generate-money-factory.php 1.3 KB

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