1 | <?php |
||
10 | trait Macroable |
||
11 | { |
||
12 | /** |
||
13 | * The registered string macros. |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | protected static $macros = []; |
||
18 | |||
19 | /** |
||
20 | * Register a custom macro. |
||
21 | * |
||
22 | * @param string $name |
||
23 | * @param object|callable $macro |
||
24 | * |
||
25 | * @return void |
||
26 | */ |
||
27 | 6 | public static function macro($name, $macro) |
|
31 | |||
32 | /** |
||
33 | * Mix another object into the class. |
||
34 | * |
||
35 | * @param object $mixin |
||
36 | * @return void |
||
37 | * @throws \ReflectionException |
||
38 | */ |
||
39 | public static function mixin($mixin) |
||
40 | 1 | { |
|
41 | $methods = (new ReflectionClass($mixin))->getMethods( |
||
42 | 1 | ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED |
|
43 | 1 | ); |
|
44 | |||
45 | foreach ($methods as $method) { |
||
46 | 1 | $method->setAccessible(true); |
|
47 | 1 | ||
48 | static::macro($method->name, $method->invoke($mixin)); |
||
49 | 1 | } |
|
50 | } |
||
51 | 1 | ||
52 | /** |
||
53 | * Checks if macro is registered. |
||
54 | * |
||
55 | * @param string $name |
||
56 | * @return bool |
||
57 | */ |
||
58 | public static function hasMacro($name) |
||
62 | |||
63 | /** |
||
64 | * Dynamically handle calls to the class. |
||
65 | * |
||
66 | * @param string $method |
||
67 | * @param array $parameters |
||
68 | * @return mixed |
||
69 | * |
||
70 | * @throws \BadMethodCallException |
||
71 | */ |
||
72 | public static function __callStatic($method, $parameters) |
||
86 | |||
87 | /** |
||
88 | * Dynamically handle calls to the class. |
||
89 | * |
||
90 | * @param string $method |
||
91 | * @param array $parameters |
||
92 | * @return mixed |
||
93 | * |
||
94 | * @throws \BadMethodCallException |
||
95 | */ |
||
96 | public function __call($method, $parameters) |
||
112 | } |
||
113 |