PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.8.6
Kubio AI Page Builder v2.8.6
2.9.0 2.8.6 2.8.5 2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / vendor / pragmarx / ia-str / src / Support / Traits / Macroable.php
kubio / vendor / pragmarx / ia-str / src / Support / Traits Last commit date
EnumeratesValues.php 4 years ago Macroable.php 1 year ago
Macroable.php
121 lines
1 <?php
2
3 namespace IlluminateAgnostic\Str\Support\Traits;
4
5 use BadMethodCallException;
6 use Closure;
7 use ReflectionClass;
8 use ReflectionMethod;
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 * @return void
25 */
26 public static function macro($name, $macro)
27 {
28 static::$macros[$name] = $macro;
29 }
30
31 /**
32 * Mix another object into the class.
33 *
34 * @param object $mixin
35 * @param bool $replace
36 * @return void
37 *
38 * @throws \ReflectionException
39 */
40 public static function mixin($mixin, $replace = true)
41 {
42 $methods = (new ReflectionClass($mixin))->getMethods(
43 ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
44 );
45
46 foreach ($methods as $method) {
47 if ($replace || ! static::hasMacro($method->name)) {
48 $method->setAccessible(true);
49 static::macro($method->name, $method->invoke($mixin));
50 }
51 }
52 }
53
54 /**
55 * Checks if macro is registered.
56 *
57 * @param string $name
58 * @return bool
59 */
60 public static function hasMacro($name)
61 {
62 return isset(static::$macros[$name]);
63 }
64
65 /**
66 * Dynamically handle calls to the class.
67 *
68 * @param string $method
69 * @param array $parameters
70 * @return mixed
71 *
72 * @throws \BadMethodCallException
73 */
74 public static function __callStatic($method, $parameters)
75 {
76 if (! static::hasMacro($method)) {
77 throw new BadMethodCallException(sprintf(
78 'Method %s::%s does not exist.',
79 static::class,
80 $method
81 ));
82 }
83
84 $macro = static::$macros[$method];
85
86 if ($macro instanceof Closure) {
87 return call_user_func_array(Closure::bind($macro, null, static::class), $parameters);
88 }
89
90 return $macro(...$parameters);
91 }
92
93 /**
94 * Dynamically handle calls to the class.
95 *
96 * @param string $method
97 * @param array $parameters
98 * @return mixed
99 *
100 * @throws \BadMethodCallException
101 */
102 public function __call($method, $parameters)
103 {
104 if (! static::hasMacro($method)) {
105 throw new BadMethodCallException(sprintf(
106 'Method %s::%s does not exist.',
107 static::class,
108 $method
109 ));
110 }
111
112 $macro = static::$macros[$method];
113
114 if ($macro instanceof Closure) {
115 return call_user_func_array($macro->bindTo($this, static::class), $parameters);
116 }
117
118 return $macro(...$parameters);
119 }
120 }
121