Passed
Push — try2 ( bdbb85 )
by Enjoys
09:03 queued 04:25
created

Element::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 10
cc 1
nc 1
nop 1
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 $allowSameNameElements = false;
43
44
    /**
45
     * @param string $name
46
     * @param string|null $label
47
     */
48 285
    public function __construct(string $name, string $label = null)
49
    {
50 285
        $this->setRequest();
51 285
        $this->setName($name);
52
53 285
        if (!is_null($label)) {
54 121
            $this->setLabel($label);
55
        }
56
    }
57
58
    /**
59
     * @psalm-suppress PossiblyNullReference
60
     */
61 88
    public function setForm(?Form $form): void
62
    {
63 88
        if ($form === null) {
64 2
            return;
65
        }
66 86
        $this->form = $form;
67 86
        $this->setDefault($this->getForm()->getDefaultsHandler()->getValue($this->getName()));
68
69 86
        if ($this instanceof Fillable) {
70 18
            foreach ($this->getElements() as $element) {
71 2
                $element->setDefault($this->getDefaultValue());
72
            }
73
        }
74
    }
75
76
    /**
77
     *
78
     * @return Form
79
     */
80 88
    public function getForm(): ?Form
81
    {
82 88
        return $this->form;
83
    }
84
85
86
    /**
87
     *
88
     * @return void
89
     */
90 89
    public function unsetForm(): void
91
    {
92 89
        $this->form = null;
93
    }
94
95
    /**
96
     * @return true|void
97
     */
98 89
    public function prepare()
99
    {
100 89
        $this->unsetForm();
101
    }
102
103
    /**
104
     *
105
     * @return string
106
     */
107 83
    public function getType(): string
108
    {
109 83
        return $this->type;
110
    }
111
112
    /**
113
     *
114
     * @param string $name
115
     * @return $this
116
     */
117 291
    protected function setName(string $name): ElementInterface
118
    {
119 291
        $this->name = $name;
120 291
        $this->setAttributes(
121 291
            AttributeFactory::createFromArray([
122 291
                'id' => $this->name,
123 291
                'name' => $this->name
124
            ])
125
        );
126
127 291
        return $this;
128
    }
129
130
    /**
131
     *
132
     * @return string
133
     */
134 186
    public function getName(): string
135
    {
136 186
        return $this->name;
137
    }
138
139
    /**
140
     *
141
     * @param string|null $title
142
     * @return $this
143
     */
144 122
    public function setLabel(?string $title = null): ElementInterface
145
    {
146 122
        $this->label = $title;
147 122
        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 86
    protected function setDefault(mixed $value = null): self
161
    {
162 86
        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 86
        if (is_string($value) || is_numeric($value)) {
170
            // $this->setValue($value);
171 10
            $this->setAttribute(
172 10
                AttributeFactory::create('value', $value)
173
            );
174
        }
175 86
        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 89
    public function isAllowSameNameElements(): bool
187
    {
188 89
        return $this->allowSameNameElements;
189
    }
190
}
191