Issues (368)

src/Admin/Form/Controls/AbstractControl.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Arbory\Base\Admin\Form\Controls;
4
5
use Arbory\Base\Html\Elements\Element;
6
use Arbory\Base\Admin\Form\Fields\Concerns\HasRenderOptions;
7
8
abstract class AbstractControl implements InputControlInterface
9
{
10
    use HasRenderOptions;
11
12
    protected $value;
13
14
    protected $readOnly = false;
15
    protected $disabled = false;
16
17
    /**
18
     * @var string|null
19
     */
20
    protected $name;
21
22
    abstract public function element(): Element;
23
24
    /**
25
     * @return Element
26
     */
27
    abstract public function render(Element $control);
28
29
    /**
30
     * @param  Element  $element
31
     * @return Element
32
     */
33
    public function applyAttributesAndClasses(Element $element)
34
    {
35
        $element->addAttributes($this->getAttributes());
36
        $element->addClass(
37
            implode(' ', $this->getClasses())
38
        );
39
40
        if ($this->isReadOnly()) {
41
            $element->addAttributes(
42
                ['readonly' => '']
43
            );
44
        }
45
46
        if ($this->isDisabled()) {
47
            $element->addAttributes(
48
                ['disabled' => '']
49
            );
50
        }
51
52
        $element->attributes()->put('name', $this->getName());
0 ignored issues
show
'name' of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        $element->attributes()->put(/** @scrutinizer ignore-type */ 'name', $this->getName());
Loading history...
It seems like $this->getName() can also be of type string; however, parameter $value of Illuminate\Support\Collection::put() does only seem to accept Illuminate\Support\TValue, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        $element->attributes()->put('name', /** @scrutinizer ignore-type */ $this->getName());
Loading history...
53
54
        return $element;
55
    }
56
57
    /**
58
     * @param $value
59
     * @return InputControlInterface
60
     */
61
    public function setValue($value): InputControlInterface
62
    {
63
        $this->value = $value;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return mixed
70
     */
71
    public function getValue()
72
    {
73
        return $this->value;
74
    }
75
76
    public function setDisabled(bool $value): InputControlInterface
77
    {
78
        $this->disabled = $value;
79
80
        return $this;
81
    }
82
83
    public function isDisabled(): bool
84
    {
85
        return $this->disabled;
86
    }
87
88
    public function setReadOnly(bool $value): InputControlInterface
89
    {
90
        $this->readOnly = $value;
91
92
        return $this;
93
    }
94
95
    public function isReadOnly(): bool
96
    {
97
        return $this->readOnly;
98
    }
99
100
    /**
101
     * @return string|null
102
     */
103
    public function getName(): ?string
104
    {
105
        return $this->name;
106
    }
107
108
    /**
109
     * @param  string|null  $name
110
     * @return InputControlInterface
111
     */
112
    public function setName(?string $name): InputControlInterface
113
    {
114
        $this->name = $name;
115
116
        $this->addAttributes(['id' => $this->getInputId($name)]);
117
118
        return $this;
119
    }
120
121
    /**
122
     * Converts dot donation name to a input name.
123
     *
124
     * @param $namespacedName
125
     * @return string
126
     */
127
    public function getInputName($namespacedName)
128
    {
129
        return Element::formatName($namespacedName);
130
    }
131
132
    /**
133
     * Creates Input ID from input name.
134
     *
135
     * @param $inputName
136
     * @return string
137
     */
138
    public function getInputId($inputName)
139
    {
140
        return rtrim(strtr($inputName, ['[' => '_', ']' => '']), '_');
141
    }
142
143
    /**
144
     * @param $namespacedName
145
     * @return string
146
     */
147
    public function getInputIdFromNamespace($namespacedName)
148
    {
149
        return $this->getInputId(
150
            $this->getInputName($namespacedName)
151
        );
152
    }
153
}
154