Passed
Push — try2 ( ef2915...d7091e )
by Enjoys
02:11
created

Element::isAllowSameNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms;
6
7
use Closure;
8
use Enjoys\Forms\Interfaces\ElementInterface;
9
use Enjoys\Forms\Interfaces\Fillable;
10
use Enjoys\Forms\Traits\Attributes;
11
use Enjoys\Forms\Traits\Request;
12
13
abstract class Element implements ElementInterface
14
{
15
    use Attributes;
16
    use Request;
17
18
    /**
19
     * @psalm-suppress PropertyNotSetInConstructor
20
     */
21
    protected string $name;
22
    protected string $type = '';
23
24
    /**
25
     *
26
     * @var string|null
27
     */
28
    protected ?string $label = null;
29
30
    /**
31
     * Флаг для обозначения обязательности заполнения этого элемента или нет
32
     * @var bool
33
     */
34
    protected bool $required = false;
35
36
    /**
37
     *
38
     * @var Form|null
39
     */
40
    protected ?Form $form = null;
41
42
    protected bool $allowSameNames = false;
43
44
    /**
45
     * @param string $name
46
     * @param string|null $label
47
     */
48 286
    public function __construct(string $name, string $label = null)
49
    {
50 286
        $this->setRequest();
51 286
        $this->setName($name);
52
53 286
        if (!is_null($label)) {
54 122
            $this->setLabel($label);
55
        }
56
    }
57
58
    /**
59
     * @psalm-suppress PossiblyNullReference
60
     */
61 89
    public function setForm(?Form $form): void
62
    {
63 89
        if ($form === null) {
64 2
            return;
65
        }
66 87
        $this->form = $form;
67 87
        $this->setDefault($this->getForm()->getDefaultsHandler()->getValue($this->getName()));
68
69 87
        if ($this instanceof Fillable) {
70 19
            foreach ($this->getElements() as $element) {
71 2
                $element->setDefault($this->getDefaultValue());
72
            }
73
        }
74
    }
75
76
    /**
77
     *
78
     * @return Form
79
     */
80 89
    public function getForm(): ?Form
81
    {
82 89
        return $this->form;
83
    }
84
85
86
    /**
87
     *
88
     * @return void
89
     */
90 90
    public function unsetForm(): void
91
    {
92 90
        $this->form = null;
93
    }
94
95
    /**
96
     * @return true|void
97
     */
98 90
    public function prepare()
99
    {
100 90
        $this->unsetForm();
101
    }
102
103
    /**
104
     *
105
     * @return string
106
     */
107 84
    public function getType(): string
108
    {
109 84
        return $this->type;
110
    }
111
112
    /**
113
     *
114
     * @param string $name
115
     * @return $this
116
     */
117 292
    protected function setName(string $name): ElementInterface
118
    {
119 292
        $this->name = $name;
120 292
        $this->setAttributes(
121 292
            AttributeFactory::createFromArray([
122 292
                'id' => $this->name,
123 292
                'name' => $this->name
124
            ])
125
        );
126
127 292
        return $this;
128
    }
129
130
    /**
131
     *
132
     * @return string
133
     */
134 187
    public function getName(): string
135
    {
136 187
        return $this->name;
137
    }
138
139
    /**
140
     *
141
     * @param string|null $title
142
     * @return $this
143
     */
144 123
    public function setLabel(?string $title = null): ElementInterface
145
    {
146 123
        $this->label = $title;
147 123
        return $this;
148
    }
149
150
    /**
151
     *
152
     * @return string|null
153
     */
154 45
    public function getLabel(): ?string
155
    {
156 45
        return $this->label;
157
    }
158
159
160 87
    protected function setDefault(mixed $value = null): self
161
    {
162 87
        if (is_array($value)) {
163
            /** @var array<Closure|scalar|null>  $value */
164 1
            $this->setAttribute(
165 1
                AttributeFactory::create('value', $value[0])
166
            );
167
        }
168
169 87
        if (is_string($value) || is_numeric($value)) {
170
            // $this->setValue($value);
171 10
            $this->setAttribute(
172 10
                AttributeFactory::create('value', $value)
173
            );
174
        }
175 87
        return $this;
176
    }
177
178 16
    public function baseHtml(): string
179
    {
180 16
        return "<input type=\"{$this->getType()}\"{$this->getAttributesString()}>";
181
    }
182
183
    /**
184
     * @return bool
185
     */
186 90
    public function isAllowSameNames(): bool
187
    {
188 90
        return $this->allowSameNames;
189
    }
190
191
192
}
193