Completed
Push — master ( d0a237...a1b9c8 )
by Freek
9s
created

Component::tag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\BladeX;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Spatie\BladeX\Exceptions\CouldNotRegisterComponent;
7
8
class Component
9
{
10
    /** @var string */
11
    public $view;
12
13
    /** @var string */
14
    public $tag;
15
16
    /** @var string */
17
    public $viewModel;
18
19
    public static function make(string $view, string $tag = '')
20
    {
21
        return new static($view, $tag);
22
    }
23
24
    public function __construct(string $view, string $tag = '')
25
    {
26
        if ($tag === '') {
27
            $tag = $this->determineDefaultTag($view);
28
        }
29
30
        $this->view = $view;
31
32
        $this->tag = $tag;
33
    }
34
35
    public function tag(string $tag)
36
    {
37
        $this->tag = $tag;
38
39
        return $this;
40
    }
41
42
    public function viewModel(string $viewModel)
43
    {
44
        if (! class_exists($viewModel)) {
45
            throw CouldNotRegisterComponent::viewModelNotFound($this->tag, $viewModel);
46
        }
47
48
        if (! is_a($viewModel, Arrayable::class, true)) {
49
            throw CouldNotRegisterComponent::viewModelNotArrayable($this->tag, $viewModel);
50
        }
51
52
        $this->viewModel = $viewModel;
53
54
        return $this;
55
    }
56
57
    protected function determineDefaultTag(string $view): string
58
    {
59
        $baseComponentName = explode('.', $view);
60
61
        $tag = kebab_case(end($baseComponentName));
62
63
        return $tag;
64
    }
65
}
66