‪TYPO3CMS  11.5
LinkResult.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 
21 
26 class ‪LinkResult implements ‪LinkResultInterface, \JsonSerializable, \ArrayAccess
27 {
29  protected string ‪$url;
30  protected string ‪$target = '';
31  protected array ‪$additionalAttributes = [];
32  protected ?string ‪$linkText = null;
33  protected array ‪$linkConfiguration = [];
34 
35  public function ‪__construct(string ‪$type, string ‪$url)
36  {
37  $this->type = ‪$type;
38  $this->url = ‪$url;
39  }
40 
41  public function ‪getUrl(): string
42  {
43  return ‪$this->url;
44  }
45 
46  public function ‪getType(): string
47  {
48  return ‪$this->type;
49  }
50 
51  public function ‪getTarget(): string
52  {
53  return ‪$this->target;
54  }
55 
56  public function ‪withTarget(string ‪$target): self
57  {
58  $newObject = clone $this;
59  $newObject->target = ‪$target;
60  return $newObject;
61  }
62 
66  public function ‪getLinkConfiguration(): array
67  {
69  }
70 
71  public function ‪withLinkConfiguration(array $configuration): self
72  {
73  $newObject = clone $this;
74  $newObject->linkConfiguration = $configuration;
75  return $newObject;
76  }
77 
78  public function ‪withLinkText(string ‪$linkText): self
79  {
80  $newObject = clone $this;
81  $newObject->linkText = ‪$linkText;
82  return $newObject;
83  }
84 
85  public function ‪getLinkText(): ?string
86  {
87  return ‪$this->linkText;
88  }
89 
90  public function ‪withAttributes(array ‪$additionalAttributes, bool $resetExistingAttributes = false): self
91  {
92  $newObject = clone $this;
93  if ($resetExistingAttributes) {
94  $newObject->additionalAttributes = [];
95  $newObject->url = '';
96  $newObject->target = '';
97  }
98  foreach (‪$additionalAttributes as $attributeName => $attributeValue) {
99  switch ($attributeName) {
100  case 'href':
101  $newObject->url = $attributeValue;
102  break;
103  case 'target':
104  $newObject->target = $attributeValue;
105  break;
106  }
107  if ($attributeValue !== null) {
108  $newObject->additionalAttributes[$attributeName] = $attributeValue;
109  } else {
110  unset($newObject->additionalAttributes[$attributeName]);
111  }
112  }
113  return $newObject;
114  }
115 
116  public function ‪withAttribute(string $attributeName, ?string $attributeValue): self
117  {
118  $newObject = clone $this;
119  switch ($attributeName) {
120  case 'href':
121  $newObject->url = $attributeValue ?? '';
122  break;
123  case 'target':
124  $newObject->target = $attributeValue ?? '';
125  break;
126  default:
127  if ($attributeValue !== null) {
128  $newObject->additionalAttributes[$attributeName] = $attributeValue;
129  } else {
130  unset($newObject->additionalAttributes[$attributeName]);
131  }
132  }
133  return $newObject;
134  }
135 
136  public function ‪jsonSerialize(): array
137  {
138  $additionalAttrs = ‪$this->additionalAttributes;
139  foreach ($additionalAttrs as $attribute => $value) {
140  if (in_array($attribute, ['href', 'target', 'class', 'title'], true)) {
141  unset($additionalAttrs[$attribute]);
142  }
143  }
144 
145  return [
146  'href' => $this->url ?: null,
147  'target' => $this->target ?: null,
148  'class' => $this->additionalAttributes['class'] ?? null,
149  'title' => $this->additionalAttributes['title'] ?? null,
150  'linkText' => $this->linkText ?: null,
151  'additionalAttributes' => $additionalAttrs,
152  ];
153  }
154 
155  public function ‪hasAttribute(string $attributeName): bool
156  {
157  switch ($attributeName) {
158  case 'href':
159  return $this->url !== '';
160  case 'target':
161  return $this->target !== '';
162  default:
163  return isset($this->additionalAttributes[$attributeName]);
164  }
165  }
166  public function ‪getAttribute(string $attributeName): ?string
167  {
168  switch ($attributeName) {
169  case 'href':
170  return ‪$this->url;
171  case 'target':
172  return ‪$this->target;
173  default:
174  return $this->additionalAttributes[$attributeName] ?? null;
175  }
176  }
177  public function ‪getAttributes(): array
178  {
179  $attributes = [];
180  if ($this->url) {
181  $attributes['href'] = ‪$this->url;
182  }
183  if ($this->target) {
184  $attributes['target'] = ‪$this->target;
185  }
186  return array_merge($attributes, $this->additionalAttributes);
187  }
188 
189  public function ‪__toString(): string
190  {
191  try {
192  return json_encode($this->‪jsonSerialize(), JSON_THROW_ON_ERROR);
193  } catch (\JsonException $e) {
194  return '';
195  }
196  }
197 
202  #[\ReturnTypeWillChange]
203  public function ‪offsetExists($offset)
204  {
205  switch ($offset) {
206  case 0:
207  case '0':
208  return $this->url !== '';
209  case 1:
210  case '1':
211  return $this->linkText !== null;
212  case 2:
213  case '2':
214  return $this->target !== '';
215  default:
216  return false;
217  }
218  }
219 
220  // @todo Will this also removed in TYPO3 v12.0, like offsetExists ?
221  #[\ReturnTypeWillChange]
222  public function ‪offsetGet($offset)
223  {
224  switch ($offset) {
225  case 0:
226  case '0':
227  return $this->‪getUrl();
228  case 1:
229  case '1':
230  return $this->‪getLinkText();
231  case 2:
232  case '2':
233  return $this->‪getTarget();
234  default:
235  return null;
236  }
237  }
238 
239  // @todo Will this also removed in TYPO3 v12.0, like offsetExists ?
240  #[\ReturnTypeWillChange]
241  public function ‪offsetSet($offset, $value)
242  {
243  switch ($offset) {
244  case 0:
245  case '0':
246  $this->url = (string)$value;
247  break;
248  case 1:
249  case '1':
250  $this->linkText = (string)$value;
251  break;
252  case 2:
253  case '2':
254  $this->target = (string)$value;
255  break;
256  default:
257  // do nothing
258  }
259  }
260 
261  // @todo Will this also removed in TYPO3 v12.0, like offsetExists ?
262  #[\ReturnTypeWillChange]
263  public function ‪offsetUnset($offset)
264  {
265  switch ($offset) {
266  case 0:
267  case '0':
268  $this->url = '';
269  break;
270  case 1:
271  case '1':
272  $this->linkText = null;
273  break;
274  case 2:
275  case '2':
276  $this->target = '';
277  break;
278  default:
279  // do nothing
280  }
281  }
282 }