1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IlluminateAgnostic\Str\Support\Traits; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use ReflectionMethod; |
8
|
|
|
use BadMethodCallException; |
9
|
|
|
|
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
|
4 |
|
public static function macro($name, $macro) |
28
|
|
|
{ |
29
|
4 |
|
static::$macros[$name] = $macro; |
30
|
4 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Mix another object into the class. |
34
|
|
|
* |
35
|
|
|
* @param object $mixin |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
1 |
|
public static function mixin($mixin) |
39
|
|
|
{ |
40
|
1 |
|
$methods = (new ReflectionClass($mixin))->getMethods( |
41
|
1 |
|
ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED |
42
|
|
|
); |
43
|
|
|
|
44
|
1 |
|
foreach ($methods as $method) { |
45
|
1 |
|
$method->setAccessible(true); |
46
|
|
|
|
47
|
1 |
|
static::macro($method->name, $method->invoke($mixin)); |
48
|
|
|
} |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Checks if macro is registered. |
53
|
|
|
* |
54
|
|
|
* @param string $name |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
4 |
|
public static function hasMacro($name) |
58
|
|
|
{ |
59
|
4 |
|
return isset(static::$macros[$name]); |
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
|
2 |
|
public static function __callStatic($method, $parameters) |
72
|
|
|
{ |
73
|
2 |
|
if (! static::hasMacro($method)) { |
74
|
|
|
throw new BadMethodCallException(sprintf( |
75
|
|
|
'Method %s::%s does not exist.', static::class, $method |
76
|
|
|
)); |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
if (static::$macros[$method] instanceof Closure) { |
80
|
2 |
|
return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return call_user_func_array(static::$macros[$method], $parameters); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Dynamically handle calls to the class. |
88
|
|
|
* |
89
|
|
|
* @param string $method |
90
|
|
|
* @param array $parameters |
91
|
|
|
* @return mixed |
92
|
|
|
* |
93
|
|
|
* @throws \BadMethodCallException |
94
|
|
|
*/ |
95
|
3 |
|
public function __call($method, $parameters) |
96
|
|
|
{ |
97
|
3 |
|
if (! static::hasMacro($method)) { |
98
|
|
|
throw new BadMethodCallException(sprintf( |
99
|
|
|
'Method %s::%s does not exist.', static::class, $method |
100
|
|
|
)); |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
$macro = static::$macros[$method]; |
104
|
|
|
|
105
|
3 |
|
if ($macro instanceof Closure) { |
106
|
3 |
|
return call_user_func_array($macro->bindTo($this, static::class), $parameters); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return call_user_func_array($macro, $parameters); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|