|
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
|
16 |
|
public static function component($name, $view, array $signature) |
|
37
|
|
|
{ |
|
38
|
16 |
|
static::$components[$name] = compact('view', 'signature'); |
|
39
|
16 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Check if a component is registered. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $name |
|
45
|
|
|
* |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
24 |
|
public static function hasComponent($name) |
|
49
|
|
|
{ |
|
50
|
24 |
|
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
|
8 |
|
protected function renderComponent($name, array $arguments) |
|
66
|
|
|
{ |
|
67
|
8 |
|
$component = static::$components[$name]; |
|
68
|
8 |
|
$data = $this->getComponentData($component['signature'], $arguments); |
|
69
|
|
|
|
|
70
|
8 |
|
return new HtmlString( |
|
71
|
8 |
|
view($component['view'], $data)->render() |
|
72
|
6 |
|
); |
|
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
|
8 |
|
protected function getComponentData(array $signature, array $arguments) |
|
84
|
|
|
{ |
|
85
|
8 |
|
$data = []; |
|
86
|
8 |
|
$i = 0; |
|
87
|
|
|
|
|
88
|
8 |
|
foreach ($signature as $variable => $default) { |
|
89
|
8 |
|
if (is_numeric($variable)) { |
|
90
|
8 |
|
$variable = $default; |
|
91
|
8 |
|
$default = null; |
|
92
|
6 |
|
} |
|
93
|
|
|
|
|
94
|
8 |
|
$data[$variable] = array_get($arguments, $i, $default); |
|
95
|
8 |
|
$i++; |
|
96
|
6 |
|
} |
|
97
|
|
|
|
|
98
|
8 |
|
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
|
16 |
|
public function __call($method, $parameters) |
|
112
|
|
|
{ |
|
113
|
16 |
|
if (static::hasComponent($method)) { |
|
114
|
8 |
|
return $this->renderComponent($method, $parameters); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
8 |
|
throw new BadMethodCallException("Method {$method} does not exist."); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|