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

Componentable::component()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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