composer package updates
[openemr.git] / vendor / illuminate / support / Traits / Macroable.php
bloba7703b145cbbdee0aded6d19fbf9ad016e42a198
1 <?php
3 namespace Illuminate\Support\Traits;
5 use Closure;
6 use ReflectionClass;
7 use ReflectionMethod;
8 use BadMethodCallException;
10 trait Macroable
12 /**
13 * The registered string macros.
15 * @var array
17 protected static $macros = [];
19 /**
20 * Register a custom macro.
22 * @param string $name
23 * @param object|callable $macro
25 * @return void
27 public static function macro($name, $macro)
29 static::$macros[$name] = $macro;
32 /**
33 * Mix another object into the class.
35 * @param object $mixin
36 * @return void
38 public static function mixin($mixin)
40 $methods = (new ReflectionClass($mixin))->getMethods(
41 ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
44 foreach ($methods as $method) {
45 $method->setAccessible(true);
47 static::macro($method->name, $method->invoke($mixin));
51 /**
52 * Checks if macro is registered.
54 * @param string $name
55 * @return bool
57 public static function hasMacro($name)
59 return isset(static::$macros[$name]);
62 /**
63 * Dynamically handle calls to the class.
65 * @param string $method
66 * @param array $parameters
67 * @return mixed
69 * @throws \BadMethodCallException
71 public static function __callStatic($method, $parameters)
73 if (! static::hasMacro($method)) {
74 throw new BadMethodCallException("Method {$method} does not exist.");
77 if (static::$macros[$method] instanceof Closure) {
78 return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters);
81 return call_user_func_array(static::$macros[$method], $parameters);
84 /**
85 * Dynamically handle calls to the class.
87 * @param string $method
88 * @param array $parameters
89 * @return mixed
91 * @throws \BadMethodCallException
93 public function __call($method, $parameters)
95 if (! static::hasMacro($method)) {
96 throw new BadMethodCallException("Method {$method} does not exist.");
99 $macro = static::$macros[$method];
101 if ($macro instanceof Closure) {
102 return call_user_func_array($macro->bindTo($this, static::class), $parameters);
105 return call_user_func_array($macro, $parameters);