‪TYPO3CMS  ‪main
SelectItem.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 final class ‪SelectItem implements \ArrayAccess
21 {
22  private array ‪$container = [];
24  0 => 'label',
25  1 => 'value',
26  2 => 'icon',
27  3 => 'group',
28  4 => 'description',
29  ];
30 
31  public function ‪__construct(
32  private string $type,
33  private string $label,
34  private int|string|null $value,
35  private ?string $icon = null,
36  private ?string $group = null,
37  private string|array|null $description = null,
38  private bool $invertStateDisplay = false,
39  private ?string $iconIdentifierChecked = null,
40  private ?string $iconIdentifierUnchecked = null,
41  private ?string $labelChecked = null,
42  private ?string $labelUnchecked = null,
43  ) {}
44 
45  public static function ‪fromTcaItemArray(array $item, string $type = 'select'): ‪SelectItem
46  {
47  return new self(
48  type: $type,
49  label: $item['label'] ?? $item[0],
50  value: $item['value'] ?? $item[1] ?? null,
51  icon: $item['icon'] ?? $item[2] ?? null,
52  group: $item['group'] ?? $item[3] ?? null,
53  ‪description: $item['description'] ?? $item[4] ?? null,
54  ‪invertStateDisplay: (bool)($item['invertStateDisplay'] ?? false),
55  iconIdentifierChecked: $item['iconIdentifierChecked'] ?? null,
56  iconIdentifierUnchecked: $item['iconIdentifierUnchecked'] ?? null,
57  labelChecked: $item['labelChecked'] ?? null,
58  labelUnchecked: $item['labelUnchecked'] ?? null,
59  );
60  }
61 
62  public function ‪toArray(): array
63  {
64  if ($this->type === 'radio') {
65  return [
66  'label' => $this->label,
67  'value' => $this->value,
68  ];
69  }
70 
71  if ($this->type === 'check') {
72  return [
73  'label' => $this->label,
74  'invertStateDisplay' => $this->invertStateDisplay,
75  'iconIdentifierChecked' => $this->iconIdentifierChecked,
76  'iconIdentifierUnchecked' => $this->iconIdentifierUnchecked,
77  'labelChecked' => $this->labelChecked,
78  'labelUnchecked' => $this->labelUnchecked,
79  ];
80  }
81 
82  // Default type=select
83  return [
84  'label' => $this->label,
85  'value' => $this->value,
86  'icon' => $this->icon,
87  'group' => $this->group,
88  'description' => $this->description,
89  ];
90  }
91 
92  public function ‪getLabel(): string
93  {
94  return $this->label;
95  }
96 
97  public function ‪withLabel(string $label): ‪SelectItem
98  {
99  $clone = clone $this;
100  $clone->label = $label;
101  return $clone;
102  }
103 
104  public function ‪getValue(): int|string|null
105  {
106  return $this->value;
107  }
108 
109  public function ‪withValue(int|string|null $value): ‪SelectItem
110  {
111  $clone = clone $this;
112  $clone->value = $value;
113  return $clone;
114  }
115 
116  public function ‪getIcon(): ?string
117  {
118  return $this->icon;
119  }
120 
121  public function ‪hasIcon(): bool
122  {
123  return $this->icon !== null;
124  }
125 
126  public function ‪withIcon(?string $icon): ‪SelectItem
127  {
128  $clone = clone $this;
129  $clone->icon = $icon;
130  return $clone;
131  }
132 
133  public function ‪getGroup(): ?string
134  {
135  return $this->group;
136  }
137 
138  public function ‪hasGroup(): bool
139  {
140  return $this->group !== null;
141  }
142 
143  public function ‪withGroup(?string $group): ‪SelectItem
144  {
145  $clone = clone $this;
146  $clone->group = $group;
147  return $clone;
148  }
149 
150  public function ‪getDescription(): string|array|null
151  {
152  return $this->description;
153  }
154 
155  public function ‪hasDescription(): bool
156  {
157  return $this->‪description !== null;
158  }
159 
160  public function ‪withDescription(string|array|null $description): ‪SelectItem
161  {
162  $clone = clone $this;
163  $clone->description = $description;
164  return $clone;
165  }
166 
167  public function ‪invertStateDisplay(): bool
168  {
169  return $this->invertStateDisplay;
170  }
171 
172  public function ‪getIconIdentifierChecked(): ?string
173  {
174  return $this->iconIdentifierChecked;
175  }
176 
177  public function ‪hasIconIdentifierChecked(): bool
178  {
179  return $this->iconIdentifierChecked !== null;
180  }
181 
182  public function ‪getIconIdentifierUnchecked(): ?string
183  {
184  return $this->iconIdentifierUnchecked;
185  }
186 
187  public function ‪hasIconIdentifierUnchecked(): bool
188  {
189  return $this->iconIdentifierUnchecked !== null;
190  }
191 
192  public function ‪getLabelChecked(): ?string
193  {
194  return $this->labelChecked;
195  }
196 
197  public function ‪hasLabelChecked(): bool
198  {
199  return $this->labelChecked !== null;
200  }
201 
202  public function ‪getLabelUnchecked(): ?string
203  {
204  return $this->labelUnchecked;
205  }
206 
207  public function ‪hasLabelUnchecked(): bool
208  {
209  return $this->labelUnchecked !== null;
210  }
211 
212  public function ‪isDivider(): bool
213  {
214  return $this->value === '--div--';
215  }
216 
217  public function ‪offsetExists(mixed $offset): bool
218  {
219  if (array_key_exists($offset, self::LEGACY_INDEXED_KEYS_MAPPING_TABLE)) {
220  $offset = self::LEGACY_INDEXED_KEYS_MAPPING_TABLE[$offset];
221  }
222  if (property_exists($this, $offset)) {
223  return isset($this->‪toArray()[$offset]);
224  }
225  return isset($this->container[$offset]);
226  }
227 
228  public function ‪offsetGet(mixed $offset): mixed
229  {
230  if (array_key_exists($offset, self::LEGACY_INDEXED_KEYS_MAPPING_TABLE)) {
231  $offset = self::LEGACY_INDEXED_KEYS_MAPPING_TABLE[$offset];
232  }
233  if (property_exists($this, $offset)) {
234  return $this->‪toArray()[$offset];
235  }
236  return $this->container[$offset] ?? null;
237  }
238 
239  public function ‪offsetSet(mixed $offset, mixed $value): void
240  {
241  if (array_key_exists($offset, self::LEGACY_INDEXED_KEYS_MAPPING_TABLE)) {
242  $offset = self::LEGACY_INDEXED_KEYS_MAPPING_TABLE[$offset];
243  }
244  if (property_exists($this, $offset)) {
245  $this->{$offset} = $value;
246  } else {
247  $this->container[$offset] = $value;
248  }
249  }
250 
251  public function ‪offsetUnset(mixed $offset): void
252  {
253  if (array_key_exists($offset, self::LEGACY_INDEXED_KEYS_MAPPING_TABLE)) {
254  $offset = self::LEGACY_INDEXED_KEYS_MAPPING_TABLE[$offset];
255  }
256 
257  if (property_exists($this, $offset)) {
258  $this->{$offset} = null;
259  } else {
260  unset($this->container[$offset]);
261  }
262  }
263 }
‪TYPO3\CMS\Core\Schema\Struct
Definition: SelectItem.php:18
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\getGroup
‪getGroup()
Definition: SelectItem.php:133
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\__construct
‪__construct(private string $type, private string $label, private int|string|null $value, private ?string $icon=null, private ?string $group=null, private string|array|null $description=null, private bool $invertStateDisplay=false, private ?string $iconIdentifierChecked=null, private ?string $iconIdentifierUnchecked=null, private ?string $labelChecked=null, private ?string $labelUnchecked=null,)
Definition: SelectItem.php:31
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\offsetExists
‪offsetExists(mixed $offset)
Definition: SelectItem.php:217
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\toArray
‪toArray()
Definition: SelectItem.php:62
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\LEGACY_INDEXED_KEYS_MAPPING_TABLE
‪const LEGACY_INDEXED_KEYS_MAPPING_TABLE
Definition: SelectItem.php:23
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\isDivider
‪isDivider()
Definition: SelectItem.php:212
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\withLabel
‪withLabel(string $label)
Definition: SelectItem.php:97
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\getDescription
‪getDescription()
Definition: SelectItem.php:150
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\hasIconIdentifierChecked
‪hasIconIdentifierChecked()
Definition: SelectItem.php:177
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\getLabelUnchecked
‪getLabelUnchecked()
Definition: SelectItem.php:202
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\withValue
‪withValue(int|string|null $value)
Definition: SelectItem.php:109
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\hasDescription
‪hasDescription()
Definition: SelectItem.php:155
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\getIconIdentifierUnchecked
‪getIconIdentifierUnchecked()
Definition: SelectItem.php:182
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\getLabelChecked
‪getLabelChecked()
Definition: SelectItem.php:192
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\getIcon
‪getIcon()
Definition: SelectItem.php:116
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\$container
‪array $container
Definition: SelectItem.php:22
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\hasIcon
‪hasIcon()
Definition: SelectItem.php:121
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\withGroup
‪withGroup(?string $group)
Definition: SelectItem.php:143
‪TYPO3\CMS\Core\Schema\Struct\SelectItem
Definition: SelectItem.php:21
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\invertStateDisplay
‪invertStateDisplay()
Definition: SelectItem.php:167
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\fromTcaItemArray
‪static fromTcaItemArray(array $item, string $type='select')
Definition: SelectItem.php:45
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\getValue
‪getValue()
Definition: SelectItem.php:104
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\getIconIdentifierChecked
‪getIconIdentifierChecked()
Definition: SelectItem.php:172
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\offsetGet
‪offsetGet(mixed $offset)
Definition: SelectItem.php:228
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\offsetSet
‪offsetSet(mixed $offset, mixed $value)
Definition: SelectItem.php:239
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\getLabel
‪getLabel()
Definition: SelectItem.php:92
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\hasLabelChecked
‪hasLabelChecked()
Definition: SelectItem.php:197
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\hasIconIdentifierUnchecked
‪hasIconIdentifierUnchecked()
Definition: SelectItem.php:187
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\offsetUnset
‪offsetUnset(mixed $offset)
Definition: SelectItem.php:251
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\hasGroup
‪hasGroup()
Definition: SelectItem.php:138
‪TYPO3\CMS\Redirects\Message\description
‪identifier description
Definition: RedirectWasHitMessage.php:32
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\withDescription
‪withDescription(string|array|null $description)
Definition: SelectItem.php:160
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\hasLabelUnchecked
‪hasLabelUnchecked()
Definition: SelectItem.php:207
‪TYPO3\CMS\Core\Schema\Struct\SelectItem\withIcon
‪withIcon(?string $icon)
Definition: SelectItem.php:126