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