Skip to content

Commit

Permalink
Add money factory (moneyphp#469)
Browse files Browse the repository at this point in the history
* Add money factory with static method phpdoc

* Move the static factory to MoneyFactory

* Update changelog

* Add money factory generator scrip to composer script

* Ignore the MoneyFactory from CS checks

* Add tests for the generated money factory

* Sort money factory method phpdoc alphabetically

* Use phpunit comparator to compare values
  • Loading branch information
sagikazarmark authored Feb 19, 2018
1 parent 2ba17a1 commit f7867b9
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 36 deletions.
1 change: 1 addition & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ $finder = PhpCsFixer\Finder::create()
->exclude('spec')
->exclude('resources')
->in(__DIR__)
->notPath('src/MoneyFactory.php')
;

return PhpCsFixer\Config::create()
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added

- [Exchanger](https://github.com/florianv/exchanger) exchange
- Generated static factory to help IDEs understand code like `Money::EUR(500)`

### Changed

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@
"vendor/bin/phpspec run -c phpspec.ci.yml",
"vendor/bin/phpunit -v --coverage-text --coverage-clover=build/unit_coverage.xml"
],
"update-currencies": "cp vendor/moneyphp/iso-currencies/resources/current.php resources/currency.php"
"update-currencies": [
"cp vendor/moneyphp/iso-currencies/resources/current.php resources/currency.php",
"php resources/generate-money-factory.php"
]
},
"extra": {
"branch-alias": {
Expand Down
61 changes: 61 additions & 0 deletions resources/generate-money-factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

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

use Money\Currencies;

$buffer = <<<PHP
<?php
namespace Money;
/**
* This is a generated file. Do not edit it manually!
*
PHPDOC
*/
trait MoneyFactory
{
/**
* Convenience factory method for a Money object.
*
* <code>
* \$fiveDollar = Money::USD(500);
* </code>
*
* @param string \$method
* @param array \$arguments
*
* @return Money
*
* @throws \InvalidArgumentException If amount is not integer(ish)
*/
public static function __callStatic(\$method, \$arguments)
{
return new Money(\$arguments[0], new Currency(\$method));
}
}
PHP;

$methodBuffer = '';

$currencies = new Currencies\AggregateCurrencies([
new Currencies\ISOCurrencies(),
new Currencies\BitcoinCurrencies(),
]);

$currencies = iterator_to_array($currencies);

usort($currencies, function (\Money\Currency $a, \Money\Currency $b) {
return strcmp($a->getCode(), $b->getCode());
});

/** @var \Money\Currency[] $currencies */
foreach ($currencies as $currency) {
$methodBuffer .= sprintf(" * @method static Money %s(string \$amount)\n", $currency->getCode());
}

$buffer = str_replace('PHPDOC', rtrim($methodBuffer), $buffer);

file_put_contents(__DIR__.'/../src/MoneyFactory.php', $buffer);
21 changes: 2 additions & 19 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
final class Money implements \JsonSerializable
{
use MoneyFactory;

const ROUND_HALF_UP = PHP_ROUND_HALF_UP;

const ROUND_HALF_DOWN = PHP_ROUND_HALF_DOWN;
Expand Down Expand Up @@ -76,25 +78,6 @@ public function __construct($amount, Currency $currency)
$this->currency = $currency;
}

/**
* Convenience factory method for a Money object.
*
* <code>
* $fiveDollar = Money::USD(500);
* </code>
*
* @param string $method
* @param array $arguments
*
* @return Money
*
* @throws \InvalidArgumentException If amount is not integer
*/
public static function __callStatic($method, $arguments)
{
return new self($arguments[0], new Currency($method));
}

/**
* Returns a new Money instance based on the current one using the Currency.
*
Expand Down
207 changes: 207 additions & 0 deletions src/MoneyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?php

namespace Money;

/**
* This is a generated file. Do not edit it manually!
*
* @method static Money AED(string $amount)
* @method static Money ALL(string $amount)
* @method static Money AMD(string $amount)
* @method static Money ANG(string $amount)
* @method static Money AOA(string $amount)
* @method static Money ARS(string $amount)
* @method static Money AUD(string $amount)
* @method static Money AWG(string $amount)
* @method static Money AZN(string $amount)
* @method static Money BAM(string $amount)
* @method static Money BBD(string $amount)
* @method static Money BDT(string $amount)
* @method static Money BGN(string $amount)
* @method static Money BHD(string $amount)
* @method static Money BIF(string $amount)
* @method static Money BMD(string $amount)
* @method static Money BND(string $amount)
* @method static Money BOB(string $amount)
* @method static Money BOV(string $amount)
* @method static Money BRL(string $amount)
* @method static Money BSD(string $amount)
* @method static Money BTN(string $amount)
* @method static Money BWP(string $amount)
* @method static Money BYN(string $amount)
* @method static Money BZD(string $amount)
* @method static Money CAD(string $amount)
* @method static Money CDF(string $amount)
* @method static Money CHE(string $amount)
* @method static Money CHF(string $amount)
* @method static Money CHW(string $amount)
* @method static Money CLF(string $amount)
* @method static Money CLP(string $amount)
* @method static Money CNY(string $amount)
* @method static Money COP(string $amount)
* @method static Money COU(string $amount)
* @method static Money CRC(string $amount)
* @method static Money CUC(string $amount)
* @method static Money CUP(string $amount)
* @method static Money CVE(string $amount)
* @method static Money CZK(string $amount)
* @method static Money DJF(string $amount)
* @method static Money DKK(string $amount)
* @method static Money DOP(string $amount)
* @method static Money DZD(string $amount)
* @method static Money EGP(string $amount)
* @method static Money ERN(string $amount)
* @method static Money ETB(string $amount)
* @method static Money EUR(string $amount)
* @method static Money FJD(string $amount)
* @method static Money FKP(string $amount)
* @method static Money GBP(string $amount)
* @method static Money GEL(string $amount)
* @method static Money GHS(string $amount)
* @method static Money GIP(string $amount)
* @method static Money GMD(string $amount)
* @method static Money GNF(string $amount)
* @method static Money GTQ(string $amount)
* @method static Money GYD(string $amount)
* @method static Money HKD(string $amount)
* @method static Money HNL(string $amount)
* @method static Money HRK(string $amount)
* @method static Money HTG(string $amount)
* @method static Money HUF(string $amount)
* @method static Money IDR(string $amount)
* @method static Money ILS(string $amount)
* @method static Money INR(string $amount)
* @method static Money IQD(string $amount)
* @method static Money IRR(string $amount)
* @method static Money ISK(string $amount)
* @method static Money JMD(string $amount)
* @method static Money JOD(string $amount)
* @method static Money JPY(string $amount)
* @method static Money KES(string $amount)
* @method static Money KGS(string $amount)
* @method static Money KHR(string $amount)
* @method static Money KMF(string $amount)
* @method static Money KPW(string $amount)
* @method static Money KRW(string $amount)
* @method static Money KWD(string $amount)
* @method static Money KYD(string $amount)
* @method static Money KZT(string $amount)
* @method static Money LAK(string $amount)
* @method static Money LBP(string $amount)
* @method static Money LKR(string $amount)
* @method static Money LRD(string $amount)
* @method static Money LSL(string $amount)
* @method static Money LYD(string $amount)
* @method static Money MAD(string $amount)
* @method static Money MDL(string $amount)
* @method static Money MGA(string $amount)
* @method static Money MKD(string $amount)
* @method static Money MMK(string $amount)
* @method static Money MNT(string $amount)
* @method static Money MOP(string $amount)
* @method static Money MRO(string $amount)
* @method static Money MUR(string $amount)
* @method static Money MVR(string $amount)
* @method static Money MWK(string $amount)
* @method static Money MXN(string $amount)
* @method static Money MXV(string $amount)
* @method static Money MYR(string $amount)
* @method static Money MZN(string $amount)
* @method static Money NAD(string $amount)
* @method static Money NGN(string $amount)
* @method static Money NIO(string $amount)
* @method static Money NOK(string $amount)
* @method static Money NPR(string $amount)
* @method static Money NZD(string $amount)
* @method static Money OMR(string $amount)
* @method static Money PAB(string $amount)
* @method static Money PEN(string $amount)
* @method static Money PGK(string $amount)
* @method static Money PHP(string $amount)
* @method static Money PKR(string $amount)
* @method static Money PLN(string $amount)
* @method static Money PYG(string $amount)
* @method static Money QAR(string $amount)
* @method static Money RON(string $amount)
* @method static Money RSD(string $amount)
* @method static Money RUB(string $amount)
* @method static Money RWF(string $amount)
* @method static Money SAR(string $amount)
* @method static Money SBD(string $amount)
* @method static Money SCR(string $amount)
* @method static Money SDG(string $amount)
* @method static Money SEK(string $amount)
* @method static Money SGD(string $amount)
* @method static Money SHP(string $amount)
* @method static Money SLL(string $amount)
* @method static Money SOS(string $amount)
* @method static Money SRD(string $amount)
* @method static Money SSP(string $amount)
* @method static Money STD(string $amount)
* @method static Money SVC(string $amount)
* @method static Money SYP(string $amount)
* @method static Money SZL(string $amount)
* @method static Money THB(string $amount)
* @method static Money TJS(string $amount)
* @method static Money TMT(string $amount)
* @method static Money TND(string $amount)
* @method static Money TOP(string $amount)
* @method static Money TRY(string $amount)
* @method static Money TTD(string $amount)
* @method static Money TWD(string $amount)
* @method static Money TZS(string $amount)
* @method static Money UAH(string $amount)
* @method static Money UGX(string $amount)
* @method static Money USD(string $amount)
* @method static Money USN(string $amount)
* @method static Money UYI(string $amount)
* @method static Money UYU(string $amount)
* @method static Money UZS(string $amount)
* @method static Money VEF(string $amount)
* @method static Money VND(string $amount)
* @method static Money VUV(string $amount)
* @method static Money WST(string $amount)
* @method static Money XAF(string $amount)
* @method static Money XAG(string $amount)
* @method static Money XAU(string $amount)
* @method static Money XBA(string $amount)
* @method static Money XBB(string $amount)
* @method static Money XBC(string $amount)
* @method static Money XBD(string $amount)
* @method static Money XBT(string $amount)
* @method static Money XCD(string $amount)
* @method static Money XDR(string $amount)
* @method static Money XOF(string $amount)
* @method static Money XPD(string $amount)
* @method static Money XPF(string $amount)
* @method static Money XPT(string $amount)
* @method static Money XSU(string $amount)
* @method static Money XTS(string $amount)
* @method static Money XUA(string $amount)
* @method static Money XXX(string $amount)
* @method static Money YER(string $amount)
* @method static Money ZAR(string $amount)
* @method static Money ZMW(string $amount)
* @method static Money ZWL(string $amount)
*/
trait MoneyFactory
{
/**
* Convenience factory method for a Money object.
*
* <code>
* $fiveDollar = Money::USD(500);
* </code>
*
* @param string $method
* @param array $arguments
*
* @return Money
*
* @throws \InvalidArgumentException If amount is not integer(ish)
*/
public static function __callStatic($method, $arguments)
{
return new Money($arguments[0], new Currency($method));
}
}
42 changes: 42 additions & 0 deletions tests/MoneyFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Tests\Money;

use Money\Currencies\AggregateCurrencies;
use Money\Currencies\BitcoinCurrencies;
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Money;
use PHPUnit\Framework\TestCase;

final class MoneyFactoryTest extends TestCase
{
/**
* @dataProvider currencyExamples
* @test
*/
public function it_creates_money_using_factories(Currency $currency)
{
$code = $currency->getCode();
$money = Money::{$code}(20);

$this->assertInstanceOf(Money::class, $money);
$this->assertEquals(new Money(20, $currency), $money);
}

public function currencyExamples()
{
$currencies = new AggregateCurrencies([
new ISOCurrencies(),
new BitcoinCurrencies(),
]);

$examples = [];

foreach ($currencies as $currency) {
$examples[] = [$currency];
}

return $examples;
}
}
14 changes: 0 additions & 14 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@ final class MoneyTest extends TestCase

const OTHER_CURRENCY = 'USD';

/**
* @test
*/
public function it_creates_money_using_factories()
{
$money = Money::XYZ(20);

$this->assertInstanceOf(Money::class, $money);

$this->assertEquals(new Money(20, new Currency('XYZ')), $money);
$this->assertEquals('20', $money->getAmount());
$this->assertEquals('XYZ', $money->getCurrency()->getCode());
}

/**
* @dataProvider equalityExamples
* @test
Expand Down
Loading

0 comments on commit f7867b9

Please sign in to comment.