|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IlluminateAgnostic\Collection\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
|
8 |
|
public static function macro($name, $macro) |
|
28
|
|
|
{ |
|
29
|
8 |
|
static::$macros[$name] = $macro; |
|
30
|
8 |
|
} |
|
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
|
10 |
|
public static function hasMacro($name) |
|
58
|
|
|
{ |
|
59
|
10 |
|
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
|
4 |
|
public static function __callStatic($method, $parameters) |
|
72
|
|
|
{ |
|
73
|
4 |
|
if (! static::hasMacro($method)) { |
|
74
|
1 |
|
throw new BadMethodCallException(sprintf( |
|
75
|
1 |
|
'Method %s::%s does not exist.', static::class, $method |
|
76
|
|
|
)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
3 |
|
if (static::$macros[$method] instanceof Closure) { |
|
80
|
3 |
|
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
|
7 |
|
public function __call($method, $parameters) |
|
96
|
|
|
{ |
|
97
|
7 |
|
if (! static::hasMacro($method)) { |
|
98
|
1 |
|
throw new BadMethodCallException(sprintf( |
|
99
|
1 |
|
'Method %s::%s does not exist.', static::class, $method |
|
100
|
|
|
)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
6 |
|
$macro = static::$macros[$method]; |
|
104
|
|
|
|
|
105
|
6 |
|
if ($macro instanceof Closure) { |
|
106
|
6 |
|
return call_user_func_array($macro->bindTo($this, static::class), $parameters); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return call_user_func_array($macro, $parameters); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|