3 namespace WPML\Collect\Support\Traits;
6 use BadMethodCallException;
11 * The registered string macros.
15 protected static $macros = [];
18 * Register a custom macro.
21 * @param callable $macro
24 public static function macro($name, callable $macro)
26 static::$macros[$name] = $macro;
30 * Checks if macro is registered.
35 public static function hasMacro($name)
37 return isset(static::$macros[$name]);
41 * Dynamically handle calls to the class.
43 * @param string $method
44 * @param array $parameters
47 * @throws \BadMethodCallException
49 public static function __callStatic($method, $parameters)
51 if (! static::hasMacro($method)) {
52 throw new BadMethodCallException("Method {$method} does not exist.");
55 if (static::$macros[$method] instanceof Closure) {
56 return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters);
59 return call_user_func_array(static::$macros[$method], $parameters);
63 * Dynamically handle calls to the class.
65 * @param string $method
66 * @param array $parameters
69 * @throws \BadMethodCallException
71 public function __call($method, $parameters)
73 if (! static::hasMacro($method)) {
74 throw new BadMethodCallException("Method {$method} does not exist.");
77 if (static::$macros[$method] instanceof Closure) {
78 return call_user_func_array(static::$macros[$method]->bindTo($this, static::class), $parameters);
81 return call_user_func_array(static::$macros[$method], $parameters);