Macros allow for static class methods to be defined and called at runtime. To begin, add the Titon\Common\Macroable trait to a class.
class Foo {
use Titon\Common\Macroable;
}
Macros require the
__callStatic() magic method to work properly.
To add a macro, call the static macro() method with a unique name and a closure to be called.
Foo::macro('lower', ($value) ==> mb_strtolower($value));Once a macro has been added, you can simply call the macro by name as if it was a static method.
Foo::lower('BaR'); // barIf a macro is called that has not been defined, a Titon\Common\Exception\MissingMacroException is thrown.