Html::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace ThinkStudio\HtmlField;
4
5
use Laravel\Nova\Fields\Field;
6
use Laravel\Nova\Http\Requests\NovaRequest;
7
8
class Html extends Field
9
{
10
    public $component      = 'html-field';
11
    public $showOnIndex    = false;
12
    public $showOnCreation = false;
13
    public $showOnUpdate   = false;
14
    public $showOnPreview  = true;
15
16
    public bool $clickable = false;
17
18 4
    public function __construct($name, ?\Closure $callback = null)
19
    {
20 4
        parent::__construct($name, $callback ?? (fn () => null));
21 4
        $this->attribute = 'TemporaryNotComputedField';
22
    }
23
24 1
    public function clickable(bool $clickable = true): static
25
    {
26 1
        $this->clickable = $clickable;
27
28 1
        return $this;
29
    }
30
31
    /**
32
     * Resolve the field's value for display.
33
     *
34
     * @param mixed $resource
35
     * @param string|null $attribute
36
     * @return void
37
     */
38 1
    public function resolveForDisplay($resource, $attribute = null)
39
    {
40 1
        $this->attribute = 'ComputedField';
41 1
        parent::resolveForDisplay($resource, $attribute);
42 1
        $this->attribute = 'ComputedField';
43
    }
44
45
    /**
46
     * Override attribute name
47
     * @see ResolvesFields::removeNonUpdateFields
48
     * @inheritDoc
49
     */
50 2
    public function resolve($resource, $attribute = null)
51
    {
52 2
        $this->attribute = 'ComputedField';
53 2
        parent::resolve($resource, $attribute);
54 2
        $this->attribute = 'TemporaryNotComputedField';
55
    }
56
57 1
    public function fill(NovaRequest $request, $model)
58
    {
59
        // nothing
60 1
    }
61
62 1
    public function jsonSerialize(): array
63
    {
64
        // AD-hoc for using filed in actions.
65 1
        if (!$this->value && $this->attribute == 'TemporaryNotComputedField') {
66 1
            $this->resolve($this->resource);
67
        }
68
69 1
        return array_merge(parent::jsonSerialize(), [
70 1
            'asHtml'    => true,
71 1
            'value'     => $this->value,
72 1
            'clickable' => $this->clickable,
73 1
        ]);
74
    }
75
}
76