Macroable::hasMacro()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MiotApi\Util\Collection;
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
     *
25
     * @return void
26
     */
27
    public static function macro($name, $macro)
28
    {
29
        static::$macros[$name] = $macro;
30
    }
31
32
    /**
33
     * Mix another object into the class.
34
     *
35
     * @param object $mixin
36
     *
37
     * @throws \ReflectionException
38
     *
39
     * @return void
40
     */
41
    public static function mixin($mixin)
42
    {
43
        $methods = (new ReflectionClass($mixin))->getMethods(
44
            ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
45
        );
46
47
        foreach ($methods as $method) {
48
            $method->setAccessible(true);
49
50
            static::macro($method->name, $method->invoke($mixin));
51
        }
52
    }
53
54
    /**
55
     * Checks if macro is registered.
56
     *
57
     * @param string $name
58
     *
59
     * @return bool
60
     */
61
    public static function hasMacro($name)
62
    {
63
        return isset(static::$macros[$name]);
64
    }
65
66
    /**
67
     * Dynamically handle calls to the class.
68
     *
69
     * @param string $method
70
     * @param array  $parameters
71
     *
72
     * @throws \BadMethodCallException
73
     *
74
     * @return mixed
75
     */
76
    public static function __callStatic($method, $parameters)
77
    {
78
        if (!static::hasMacro($method)) {
79
            throw new BadMethodCallException("Method {$method} does not exist.");
80
        }
81
82
        if (static::$macros[$method] instanceof Closure) {
83
            return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters);
84
        }
85
86
        return call_user_func_array(static::$macros[$method], $parameters);
87
    }
88
89
    /**
90
     * Dynamically handle calls to the class.
91
     *
92
     * @param string $method
93
     * @param array  $parameters
94
     *
95
     * @throws \BadMethodCallException
96
     *
97
     * @return mixed
98
     */
99
    public function __call($method, $parameters)
100
    {
101
        if (!static::hasMacro($method)) {
102
            throw new BadMethodCallException("Method {$method} does not exist.");
103
        }
104
105
        $macro = static::$macros[$method];
106
107
        if ($macro instanceof Closure) {
108
            return call_user_func_array($macro->bindTo($this, static::class), $parameters);
109
        }
110
111
        return call_user_func_array($macro, $parameters);
112
    }
113
}
114