Passed
Push — main ( f98caf...46400b )
by Torben
71:32 queued 29:51
created

Field::getRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Domain\Model\Registration;
13
14
use DERHANSEN\SfEventMgt\Domain\Model\Event;
15
use DERHANSEN\SfEventMgt\Utility\FieldType;
16
use DERHANSEN\SfEventMgt\Utility\FieldValueType;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
19
20
class Field extends AbstractEntity
21
{
22
    protected string $title = '';
23
    protected string $type = '';
24
    protected bool $required = false;
25
    protected string $placeholder = '';
26
    protected string $defaultValue = '';
27
    protected string $settings = '';
28
    protected ?Event $event = null;
29
    protected string $text = '';
30
    protected int $datepickermode = 0;
31
    protected string $feuserValue = '';
32
33
    public function getTitle(): string
34
    {
35
        return $this->title;
36
    }
37
38
    public function setTitle(string $title): void
39
    {
40
        $this->title = $title;
41
    }
42
43
    public function getType(): string
44
    {
45
        return $this->type;
46
    }
47
48
    public function setType(string $type): void
49
    {
50
        $this->type = $type;
51
    }
52
53
    public function getRequired(): bool
54
    {
55
        return $this->required;
56
    }
57
58
    public function setRequired(bool $required): void
59
    {
60
        $this->required = $required;
61
    }
62
63
    public function getPlaceholder(): string
64
    {
65
        return $this->placeholder;
66
    }
67
68
    public function setPlaceholder(string $placeholder): void
69
    {
70
        $this->placeholder = $placeholder;
71
    }
72
73
    public function getDefaultValue(): string
74
    {
75
        return $this->defaultValue;
76
    }
77
78
    public function setDefaultValue(string $defaultValue): void
79
    {
80
        $this->defaultValue = $defaultValue;
81
    }
82
83
    public function getSettings(): string
84
    {
85
        return $this->settings;
86
    }
87
88
    public function setSettings(string $settings): void
89
    {
90
        $this->settings = $settings;
91
    }
92
93
    /**
94
     * Explodes the given string and returns an array of options for check and radio fields
95
     *
96
     * @return array
97
     */
98
    public function getSettingsForOption(): array
99
    {
100
        $options = [];
101
        $string = str_replace('[\n]', PHP_EOL, $this->settings);
102
        $settingsField = GeneralUtility::trimExplode(PHP_EOL, $string, true);
103
        foreach ($settingsField as $line) {
104
            $settings = GeneralUtility::trimExplode('|', $line, false);
105
            $value = ($settings[1] ?? $settings[0]);
106
            $label = $settings[0];
107
            $options[] = [
108
                'label' => $label,
109
                'value' => $value,
110
                'selected' => $this->defaultValue === $value ? 1 : 0,
111
            ];
112
        }
113
114
        return $options;
115
    }
116
117
    public function getEvent(): ?Event
118
    {
119
        return $this->event;
120
    }
121
122
    public function setEvent(?Event $event): void
123
    {
124
        $this->event = $event;
125
    }
126
127
    /**
128
     * Returns the field valueType
129
     *
130
     * @return int
131
     */
132
    public function getValueType(): int
133
    {
134
        $valueTypes = [
135
            FieldType::INPUT => FieldValueType::TYPE_TEXT,
136
            FieldType::CHECK => FieldValueType::TYPE_ARRAY,
137
            FieldType::RADIO => FieldValueType::TYPE_TEXT,
138
            FieldType::TEXTAREA => FieldValueType::TYPE_TEXT,
139
            FieldType::TEXT => FieldValueType::TYPE_TEXT,
140
            FieldType::DIVIDER => FieldValueType::TYPE_TEXT,
141
            FieldType::SELECT => FieldValueType::TYPE_ARRAY,
142
        ];
143
        if (isset($valueTypes[$this->type])) {
144
            return $valueTypes[$this->type];
145
        }
146
147
        return FieldValueType::TYPE_TEXT;
148
    }
149
150
    public function getPartialName(): string
151
    {
152
        return ucfirst($this->type);
153
    }
154
155
    public function getText(): string
156
    {
157
        return $this->text;
158
    }
159
160
    public function setText(string $text): void
161
    {
162
        $this->text = $text;
163
    }
164
165
    public function getDatepickermode(): int
166
    {
167
        return $this->datepickermode;
168
    }
169
170
    public function setDatepickermode(int $datepickermode): void
171
    {
172
        $this->datepickermode = $datepickermode;
173
    }
174
175
    public function getDatepickermodeType(): string
176
    {
177
        switch ($this->datepickermode) {
178
            case 1:
179
                return 'datetime-local';
180
            case 2:
181
                return 'time';
182
            default:
183
                return 'date';
184
        }
185
    }
186
187
    public function getFeuserValue(): string
188
    {
189
        return $this->feuserValue;
190
    }
191
192
    public function setFeuserValue(string $feuserValue): void
193
    {
194
        $this->feuserValue = $feuserValue;
195
    }
196
}
197