Element::isAllowSameNames()   A
last analyzed

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
    protected ?string $label = null;
25
26
    /**
27
     * Флаг для обозначения обязательности заполнения этого элемента или нет
28
     */
29
    protected bool $required = false;
30
31
    protected ?Form $form = null;
32
33
    protected bool $allowSameNames = false;
34
35
36 308
    public function __construct(string $name, ?string $label = null)
37
    {
38 308
        $this->setRequest();
39 308
        $this->setName($name);
40
41 308
        if (!is_null($label)) {
42 124
            $this->setLabel($label);
43
        }
44
    }
45
46
47 95
    public function setForm(?Form $form): static
48
    {
49 95
        if ($form === null) {
50 2
            return $this;
51
        }
52 93
        $this->form = $form;
53 93
        $this->setDefault($this->form->getDefaultsHandler()->getValue($this->getName()));
54
55 93
        if ($this instanceof Fillable) {
56 21
            foreach ($this->getElements() as $element) {
57 3
                $element->setDefault($this->getDefaultValue());
58
            }
59
        }
60
61 93
        return $this;
62
    }
63
64 95
    public function getForm(): ?Form
65
    {
66 95
        return $this->form;
67
    }
68
69 96
    public function unsetForm(): void
70
    {
71 96
        $this->form = null;
72
    }
73
74
75 96
    public function prepare(): bool
76
    {
77 96
        $this->unsetForm();
78 96
        return false;
79
    }
80
81 86
    public function getType(): string
82
    {
83 86
        return $this->type;
84
    }
85
86 314
    protected function setName(string $name): static
87
    {
88 314
        $this->name = trim($name);
89 314
        $this->setAttributes(
90 314
            AttributeFactory::createFromArray([
91 314
                'id' => $this->name,
92 314
                'name' => $this->name
93 314
            ])
94 314
        );
95
96 314
        return $this;
97
    }
98
99 205
    public function getName(): string
100
    {
101 205
        return $this->name;
102
    }
103
104 125
    public function setLabel(?string $title = null): static
105
    {
106 125
        $this->label = $title;
107 125
        return $this;
108
    }
109
110 47
    public function getLabel(): ?string
111
    {
112 47
        return $this->label;
113
    }
114
115 93
    protected function setDefault(mixed $value = null): static
116
    {
117 93
        if (is_array($value)) {
118
            /** @var array<Closure|scalar|null> $value */
119 1
            $this->setAttribute(
120 1
                AttributeFactory::create('value', $value[0])
121 1
            );
122
        }
123
124 93
        if (is_string($value) || is_numeric($value)) {
125
            // $this->setValue($value);
126 10
            $this->setAttribute(
127 10
                AttributeFactory::create('value', $value)
128 10
            );
129
        }
130 93
        return $this;
131
    }
132
133 16
    public function baseHtml(): string
134
    {
135 16
        return "<input type=\"{$this->getType()}\"{$this->getAttributesString()}>";
136
    }
137
138 96
    public function isAllowSameNames(): bool
139
    {
140 96
        return $this->allowSameNames;
141
    }
142
}
143