Skip to content

Commit b43e60f

Browse files
committed
#87 Allow static Macros
Money and Currency override the Macroable __callStatic method, effectively disabling static macros. Add a check in, if the statically called method exists as a macro, then call the macro instead.
1 parent 5f5efe5 commit b43e60f

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

src/Currency.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@
182182
*/
183183
class Currency implements Arrayable, Castable, Jsonable, JsonSerializable, Renderable
184184
{
185-
use Macroable;
185+
use Macroable {
186+
__callStatic as protected macroableCallStatic;
187+
}
186188

187189
protected string $currency;
188190

@@ -235,6 +237,10 @@ public function __construct(string $currency)
235237

236238
public static function __callStatic(string $method, array $arguments): Currency
237239
{
240+
if (static::hasMacro($method)) {
241+
return static::macroableCallStatic($method, $arguments);
242+
}
243+
238244
return new self($method);
239245
}
240246

src/Money.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@
186186
*/
187187
class Money implements Arrayable, Castable, Jsonable, JsonSerializable, Renderable
188188
{
189-
use Macroable;
189+
use Macroable {
190+
__callStatic as protected macroableCallStatic;
191+
}
190192

191193
const ROUND_HALF_UP = PHP_ROUND_HALF_UP;
192194

@@ -283,6 +285,10 @@ protected function convertAmount(int|float $amount, bool $convert = false): int|
283285

284286
public static function __callStatic(string $method, array $arguments): Money
285287
{
288+
if (static::hasMacro($method)) {
289+
return static::macroableCallStatic($method, $arguments);
290+
}
291+
286292
$convert = isset($arguments[1]) && is_bool($arguments[1]) && $arguments[1];
287293

288294
return new self($arguments[0], new Currency($method), $convert);

tests/CurrencyTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,11 @@ public function testResetCurrencies()
8888
$this->assertEmpty(Currency::getCurrencies());
8989
Currency::setCurrencies($currencies);
9090
}
91+
92+
public function testStaticMacro()
93+
{
94+
Currency::macro('testMacro', fn () => Currency::EUR());
95+
96+
$this->assertEquals(Currency::EUR(), Currency::testMacro());
97+
}
9198
}

tests/MoneyTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,11 @@ public function testMakingMutable()
411411
$this->assertTrue($money->isImmutable());
412412
$this->assertFalse($money->mutable()->isImmutable());
413413
}
414+
415+
public function testStaticMacro()
416+
{
417+
Money::macro('testMacro', fn () => Money::USD(1099));
418+
419+
$this->assertEquals(Money::USD(1099), Money::testMacro());
420+
}
414421
}

0 commit comments

Comments
 (0)