Completed
Push — master ( 058435...d3162a )
by Patrick
02:34
created

Macroable::getMacro()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Builders\Traits;
4
5
use InvalidArgumentException;
6
7
trait Macroable
8
{
9
    /**
10
     * @var callable[]
11
     */
12
    protected static $macros = [];
13
14
    /**
15
     * @param string        $method
16
     * @param callable|null $callback
17
     */
18
    public static function macro($method, callable $callback = null)
19
    {
20
        if (!is_callable($callback)) {
21
            throw new InvalidArgumentException('Fluent builder should be extended with a closure argument, none given');
22
        }
23
24
        self::$macros[$method] = $callback;
25
    }
26
27
    /**
28
     * @param string $method
29
     *
30
     * @return bool
31
     */
32
    public function hasMacro($method)
33
    {
34
        return isset(self::$macros[$method]);
35
    }
36
37
    /**
38
     * @param string $method
39
     *
40
     * @return callable
41
     */
42
    public function getMacro($method)
43
    {
44
        return self::$macros[$method];
45
    }
46
47
    /**
48
     * @param string $method
49
     * @param array  $params
50
     *
51
     * @return mixed
52
     */
53
    public function callMacro($method, array $params = [])
54
    {
55
        // Add builder as first closure param, append the given params
56
        array_unshift($params, $this);
57
58
        return call_user_func_array($this->getMacro($method), $params);
59
    }
60
}
61