|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\BladeX; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
8
|
|
|
use Spatie\BladeX\Exceptions\CouldNotRegisterComponent; |
|
9
|
|
|
|
|
10
|
|
|
class Component |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var BladeX */ |
|
13
|
|
|
protected $bladeX; |
|
14
|
|
|
|
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
public $view; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
* @internal |
|
21
|
|
|
* @see Component::getTag() |
|
22
|
|
|
*/ |
|
23
|
|
|
public $tag; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
public $viewModel; |
|
27
|
|
|
|
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
protected $prefix; |
|
30
|
|
|
|
|
31
|
|
|
/** @var bool */ |
|
32
|
|
|
protected $withNamespace; |
|
33
|
|
|
|
|
34
|
|
|
public static function make(string $view, string $tag = '', string $prefix = '', bool $withNamespace = true) |
|
35
|
|
|
{ |
|
36
|
|
|
return new static($view, $tag, $prefix, $withNamespace); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(string $view, string $tag = '', string $prefix = '', bool $withNamespace = true) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->view = $view; |
|
42
|
|
|
|
|
43
|
|
|
$this->tag = $tag; |
|
44
|
|
|
|
|
45
|
|
|
$this->prefix = $prefix; |
|
46
|
|
|
|
|
47
|
|
|
$this->withNamespace = $withNamespace; |
|
48
|
|
|
|
|
49
|
|
|
$this->bladeX = app(BladeX::class); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function tag(string $tag) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->tag = $tag; |
|
55
|
|
|
|
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function prefix(string $prefix) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->prefix = $prefix; |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function withoutNamespace() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->withNamespace = false; |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function viewModel(string $viewModel) |
|
74
|
|
|
{ |
|
75
|
|
|
if (! class_exists($viewModel)) { |
|
76
|
|
|
throw CouldNotRegisterComponent::viewModelNotFound($this->tag, $viewModel); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (! is_a($viewModel, Arrayable::class, true)) { |
|
80
|
|
|
throw CouldNotRegisterComponent::viewModelNotArrayable($this->tag, $viewModel); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->viewModel = $viewModel; |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function getTag(): string |
|
89
|
|
|
{ |
|
90
|
|
|
$tag = empty($this->prefix) ? $this->bladeX->getPrefix() : Str::finish($this->prefix, '-'); |
|
91
|
|
|
|
|
92
|
|
|
$tag .= empty($this->tag) ? $this->determineDefaultTag() : $this->tag; |
|
93
|
|
|
|
|
94
|
|
|
return $tag; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function determineDefaultTag(): string |
|
98
|
|
|
{ |
|
99
|
|
|
$baseComponentName = explode('.', $this->view); |
|
100
|
|
|
|
|
101
|
|
|
$tag = Str::kebab(end($baseComponentName)); |
|
102
|
|
|
|
|
103
|
|
|
if (Str::contains($this->view, '::') && ! Str::contains($tag, '::')) { |
|
104
|
|
|
$namespace = Str::before($this->view, '::'); |
|
105
|
|
|
$tag = "{$namespace}::{$tag}"; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (! $this->withNamespace && Str::contains($tag, '::')) { |
|
109
|
|
|
$tag = Str::after($tag, '::'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $tag; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|