Completed
Pull Request — master (#5)
by ARCANEDEV
05:18
created

Componentable::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4286
cc 2
eloc 4
nc 2
nop 2
crap 6
1
<?php namespace Arcanedev\LaravelHtml\Traits;
2
3
use Illuminate\Support\HtmlString;
4
use BadMethodCallException;
5
6
/**
7
 * Class     Componentable
8
 *
9
 * @package  Arcanedev\LaravelHtml\Traits
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
trait Componentable
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * The registered components.
20
     *
21
     * @var array
22
     */
23
    protected static $components = [];
24
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Main Functions
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * Register a custom component.
31
     *
32
     * @param       $name
33
     * @param       $view
34
     * @param array $signature
35
     *
36
     * @return void
37
     */
38 8
    public static function component($name, $view, array $signature)
39
    {
40 8
        static::$components[$name] = compact('view', 'signature');
41 8
    }
42
43
    /**
44
     * Check if a component is registered.
45
     *
46
     * @param $name
47
     *
48
     * @return bool
49
     */
50 8
    public static function hasComponent($name)
51
    {
52 8
        return isset(static::$components[$name]);
53
    }
54
55
    /* ------------------------------------------------------------------------------------------------
56
     |  Other Functions
57
     | ------------------------------------------------------------------------------------------------
58
     */
59
    /**
60
     * Render a custom component.
61
     *
62
     * @param  string  $name
63
     * @param  array   $arguments
64
     *
65
     * @return \Illuminate\Contracts\View\View
66
     */
67
    protected function renderComponent($name, array $arguments)
68
    {
69
        $component = static::$components[$name];
70
        $data      = $this->getComponentData($component['signature'], $arguments);
71
72
        return new HtmlString(
73
            view($component['view'], $data)->render()
74
        );
75
    }
76
77
    /**
78
     * Prepare the component data, while respecting provided defaults.
79
     *
80
     * @param  array  $signature
81
     * @param  array  $arguments
82
     *
83
     * @return array
84
     */
85
    protected function getComponentData(array $signature, array $arguments)
86
    {
87
        $data = [];
88
        $i    = 0;
89
90
        foreach ($signature as $variable => $default) {
91
            if (is_numeric($variable)) {
92
                $variable = $default;
93
                $default  = null;
94
            }
95
96
            $data[$variable] = array_get($arguments, $i) ?: $default;
97
            $i++;
98
        }
99
100
        return $data;
101
    }
102
103
    /**
104
     * Dynamically handle calls to the class.
105
     *
106
     * @param  string  $method
107
     * @param  array   $parameters
108
     *
109
     * @return \Illuminate\Contracts\View\View|mixed
110
     *
111
     * @throws \BadMethodCallException
112
     */
113
    public function __call($method, $parameters)
114
    {
115
        if (static::hasComponent($method)) {
116
            return $this->renderComponent($method, $parameters);
117
        }
118
119
        throw new BadMethodCallException("Method {$method} does not exist.");
120
    }
121
}
122