Test Failed
Pull Request — main (#21)
by
unknown
07:08 queued 03:25
created

ComponentTrait::renderComponent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Hafijul233\Form\Traits;
4
5
use BadMethodCallException;
6
use Illuminate\Contracts\View\View;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\HtmlString;
9
10
/**
11
 * Trait ComponentTrait
12
 */
13
trait ComponentTrait
14
{
15
    /**
16
     * The registered components.
17
     *
18
     * @var array
19
     */
20
    protected static $components = [];
21
22
    /**
23
     * Register a custom component.
24
     *
25
     * @param    $name
26
     * @param    $view
27
     * @param  array  $signature
28
     * @return void
29
     */
30
    public static function component($name, $view, array $signature)
31
    {
32
        static::$components[$name] = compact('view', 'signature');
33
    }
34
35
    /**
36
     * Dynamically handle calls to the class.
37
     *
38
     * @param  string  $method
39
     * @param  array  $parameters
40
     * @return View|HtmlString|mixed
41
     *
42
     * @throws BadMethodCallException
43
     */
44
    public function __call($method, $parameters)
45
    {
46
        if (static::hasComponent($method)) {
47
            return $this->renderComponent($method, $parameters);
48
        }
49
50
        throw new BadMethodCallException("Method {$method} does not exist.");
51
    }
52
53
    /**
54
     * Check if a component is registered.
55
     *
56
     * @param $name
57
     * @return bool
58
     */
59
    public static function hasComponent($name): bool
60
    {
61
        return isset(static::$components[$name]);
62
    }
63
64
    /**
65
     * Render a custom component.
66
     *
67
     * @param    $name
68
     * @param  array  $arguments
69
     * @return View|HtmlString
70
     */
71
    protected function renderComponent($name, array $arguments)
72
    {
73
        $component = static::$components[$name];
74
        $data = $this->getComponentData($component['signature'], $arguments);
75
76
        return new HtmlString(
77
            $this->view->make($component['view'], $data)->render()
78
        );
79
    }
80
81
    /**
82
     * Prepare the component data, while respecting provided defaults.
83
     *
84
     * @param  array  $signature
85
     * @param  array  $arguments
86
     * @return array
87
     */
88
    protected function getComponentData(array $signature, array $arguments)
89
    {
90
        $data = [];
91
92
        $i = 0;
93
        foreach ($signature as $variable => $default) {
94
            // If the "variable" value is actually a numeric key, we can assume that
95
            // no default had been specified for the component argument and we'll
96
            // just use null instead, so that we can treat them all the same.
97
            if (is_numeric($variable)) {
98
                $variable = $default;
99
                $default = null;
100
            }
101
102
            $data[$variable] = Arr::get($arguments, $i, $default);
103
104
            $i++;
105
        }
106
107
        return $data;
108
    }
109
}
110