DatePicker::setup()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 45
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 25
nc 8
nop 0
dl 0
loc 45
rs 8.5866
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\View\Components;
6
7
use Closure;
8
use Illuminate\Contracts\View\View;
9
use Illuminate\Support\Arr;
10
use Illuminate\View\Component;
11
12
class DatePicker extends Component
13
{
14
    public string $uuid;
15
16
    public function __construct(
17
        public ?string $id = null,
18
        public ?string $label = null,
19
        public ?string $icon = null,
20
        public ?string $iconRight = null,
21
        public ?string $hint = null,
22
        public ?string $hintClass = 'fieldset-label',
23
        public ?bool $inline = false,
24
        public ?array $config = [],
25
26
        // Slots
27
        public mixed $prepend = null,
28
        public mixed $append = null,
29
30
        // Validations
31
        public ?string $errorField = null,
32
        public ?string $errorClass = 'text-error',
33
        public ?bool $omitError = false,
34
        public ?bool $firstErrorOnly = false,
35
    ) {
36
        $this->uuid = md5(serialize($this)) . $id;
37
    }
38
39
    public function setup(): string
40
    {
41
        // Handle `wire:model.live` for `range` dates
42
        if (isset($this->config["mode"]) && $this->config["mode"] === "range" && $this->attributes->wire('model')->hasModifier('live')) {
43
            $this->attributes->setAttributes([
44
                'wire:model' => $this->modelName(),
45
                'live' => true
46
            ]);
47
        }
48
49
        $config = json_encode(array_merge([
50
            'dateFormat' => 'Y-m-d H:i',
51
            'enableTime' => true,
52
            'disableMobile' => true,
53
            'minuteIncrement' => 1,
54
            //'altInput' => true,
55
            //'altInputClass' => ' ',
56
            'allowInput' => true,
57
            'time_24hr' => true,
58
            'clickOpens' => ! $this->attributes->has('readonly') || $this->attributes->get('readonly') === false,
59
            'defaultDate' => '#model#',
60
            'plugins' => ['#plugins#'],
61
            'disable' => ['#disable#'],
62
        ], Arr::except($this->config, ["plugins"])));
63
64
        // Plugins
65
        $plugins = "";
66
67
        foreach (Arr::get($this->config, 'plugins', []) as $plugin) {
68
            $plugins .= "new " . key($plugin) . "( " . json_encode(current($plugin)) . " ),";
69
        }
70
71
        $config = str_replace('"#plugins#"', $plugins, $config);
72
73
        // Disables
74
        $disables = '';
75
76
        foreach (Arr::get($this->config, 'disable', []) as $disable) {
77
            $disables .= $disable . ',';
78
        }
79
80
        $config = str_replace('"#disable#"', $disables, $config);
81
82
        // Sets default date as current bound model
83
        return str_replace('"#model#"', '$wire.get("' . $this->modelName() . '")', $config);
84
    }
85
86
    public function modelName(): ?string
87
    {
88
        return $this->attributes->whereStartsWith('wire:model')->first();
89
    }
90
91
    public function errorFieldName(): ?string
92
    {
93
        return $this->errorField ?? $this->modelName();
94
    }
95
96
    public function isReadonly(): bool
97
    {
98
        return $this->attributes->has('readonly') && $this->attributes->get('readonly') === true;
99
    }
100
101
    public function isDisabled(): bool
102
    {
103
        return $this->attributes->has('disabled') && $this->attributes->get('disabled') === true;
104
    }
105
106
    /**
107
     * Get the view / contents that represent the component.
108
     */
109
    public function render(): View|Closure|string
110
    {
111
        return view('components.date-picker');
112
    }
113
}
114