]> _ Git - physioassist-wordpress.git/blob
27480c1d2efee8035299c09786c8d2f28a445b69
[physioassist-wordpress.git] /
1 <?php
2
3 namespace WPML\Collect\Support\Traits;
4
5 use Closure;
6 use BadMethodCallException;
7
8 trait Macroable
9 {
10     /**
11      * The registered string macros.
12      *
13      * @var array
14      */
15     protected static $macros = [];
16
17     /**
18      * Register a custom macro.
19      *
20      * @param  string    $name
21      * @param  callable  $macro
22      * @return void
23      */
24     public static function macro($name, callable $macro)
25     {
26         static::$macros[$name] = $macro;
27     }
28
29     /**
30      * Checks if macro is registered.
31      *
32      * @param  string  $name
33      * @return bool
34      */
35     public static function hasMacro($name)
36     {
37         return isset(static::$macros[$name]);
38     }
39
40     /**
41      * Dynamically handle calls to the class.
42      *
43      * @param  string  $method
44      * @param  array   $parameters
45      * @return mixed
46      *
47      * @throws \BadMethodCallException
48      */
49     public static function __callStatic($method, $parameters)
50     {
51         if (! static::hasMacro($method)) {
52             throw new BadMethodCallException("Method {$method} does not exist.");
53         }
54
55         if (static::$macros[$method] instanceof Closure) {
56             return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters);
57         }
58
59         return call_user_func_array(static::$macros[$method], $parameters);
60     }
61
62     /**
63      * Dynamically handle calls to the class.
64      *
65      * @param  string  $method
66      * @param  array   $parameters
67      * @return mixed
68      *
69      * @throws \BadMethodCallException
70      */
71     public function __call($method, $parameters)
72     {
73         if (! static::hasMacro($method)) {
74             throw new BadMethodCallException("Method {$method} does not exist.");
75         }
76
77         if (static::$macros[$method] instanceof Closure) {
78             return call_user_func_array(static::$macros[$method]->bindTo($this, static::class), $parameters);
79         }
80
81         return call_user_func_array(static::$macros[$method], $parameters);
82     }
83 }