‪TYPO3CMS  ‪main
Icon.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 ‪Icon
27 {
32  public const ‪SIZE_DEFAULT = 'default'; // 1em
33 
38  public const ‪SIZE_SMALL = 'small'; // 16px
39 
44  public const ‪SIZE_MEDIUM = 'medium'; // 32px
45 
50  public const ‪SIZE_LARGE = 'large'; // 48px
51 
56  public const ‪SIZE_MEGA = 'mega'; // 64px
57 
62  public const ‪SIZE_OVERLAY = 'overlay';
63 
67  protected string ‪$identifier;
68 
72  protected ?string ‪$title = null;
73 
77  protected ?‪Icon ‪$overlayIcon = null;
78 
82  protected IconSize ‪$size;
83 
87  protected bool ‪$spinning = false;
88 
94  protected ‪$state;
95 
99  protected ‪$dimension;
100 
104  protected ‪$markup;
105 
109  protected ‪$alternativeMarkups = [];
110 
114  public function ‪getMarkup(?string $alternativeMarkupIdentifier = null): string
115  {
116  if ($alternativeMarkupIdentifier !== null && isset($this->alternativeMarkups[$alternativeMarkupIdentifier])) {
117  return $this->alternativeMarkups[$alternativeMarkupIdentifier];
118  }
119  return ‪$this->markup;
120  }
121 
125  public function ‪setMarkup(string ‪$markup): self
126  {
127  $this->markup = ‪$markup;
128  return $this;
129  }
130 
131  public function ‪getAlternativeMarkup(string $markupIdentifier): string
132  {
133  return $this->alternativeMarkups[$markupIdentifier] ?: '';
134  }
135 
139  public function ‪setAlternativeMarkup(string $markupIdentifier, string ‪$markup): self
140  {
141  $this->alternativeMarkups[$markupIdentifier] = ‪$markup;
142  return $this;
143  }
144 
145  public function ‪getIdentifier(): string
146  {
147  return ‪$this->identifier;
148  }
149 
153  public function ‪setIdentifier(string ‪$identifier): self
154  {
155  $this->identifier = ‪$identifier;
156  return $this;
157  }
158 
159  public function ‪getTitle(): ?string
160  {
161  return ‪$this->title;
162  }
163 
167  public function ‪setTitle(?string ‪$title): self
168  {
169  $this->title = ‪$title;
170  return $this;
171  }
172 
173  public function ‪getOverlayIcon(): ?‪Icon
174  {
175  return ‪$this->overlayIcon;
176  }
177 
181  public function ‪setOverlayIcon(?‪Icon ‪$overlayIcon): self
182  {
183  $this->overlayIcon = ‪$overlayIcon;
184  return $this;
185  }
186 
187  public function ‪getSize(): string
188  {
189  return $this->size->value;
190  }
191 
197  public function ‪setSize(string|IconSize ‪$size): self
198  {
199  if (is_string(‪$size)) {
200  ‪$size = IconSize::from(‪$size);
201  ‪$size->triggerDeprecation();
202  }
203  $this->size = ‪$size;
204  $this->dimension = GeneralUtility::makeInstance(Dimension::class, ‪$size);
205  return $this;
206  }
207 
208  public function ‪isSpinning(): bool
209  {
210  return ‪$this->spinning;
211  }
212 
216  public function ‪setSpinning(bool ‪$spinning): self
217  {
218  $this->spinning = ‪$spinning;
219  return $this;
220  }
221 
222  public function ‪getState(): ‪IconState
223  {
224  return ‪$this->state;
225  }
226 
230  public function ‪setState(‪IconState ‪$state): self
231  {
232  $this->state = ‪$state;
233  return $this;
234  }
235 
236  public function ‪getDimension(): ‪Dimension
237  {
238  return ‪$this->dimension;
239  }
240 
241  public function ‪render(?string $alternativeMarkupIdentifier = null): string
242  {
243  $overlayIconMarkup = '';
244  if ($this->overlayIcon !== null) {
245  $overlayIconMarkup = '<span class="icon-overlay icon-' . htmlspecialchars($this->overlayIcon->getIdentifier()) . '">' . $this->overlayIcon->getMarkup() . '</span>';
246  }
247  return str_replace('{overlayMarkup}', $overlayIconMarkup, $this->‪wrappedIcon($alternativeMarkupIdentifier));
248  }
249 
250  public function ‪__toString(): string
251  {
252  return $this->‪render();
253  }
254 
258  protected function ‪wrappedIcon(?string $alternativeMarkupIdentifier = null): string
259  {
260  $classes = [];
261  $classes[] = 't3js-icon';
262  $classes[] = 'icon';
263  $classes[] = 'icon-size-' . $this->‪getSize();
264  $classes[] = 'icon-state-' . htmlspecialchars($this->state instanceof ‪IconState ? $this->state->value : IconState::STATE_DEFAULT->value);
265  $classes[] = 'icon-' . $this->‪getIdentifier();
266  if ($this->‪isSpinning()) {
267  $classes[] = 'icon-spin';
268  }
269 
270  $attributes = [];
271  $attributes['title'] = $this->‪getTitle();
272  $attributes['class'] = implode(' ', $classes);
273  $attributes['data-identifier'] = $this->‪getIdentifier();
274  $attributes['aria-hidden'] = 'true';
275 
276  ‪$markup = [];
277  ‪$markup[] = '<span ' . GeneralUtility::implodeAttributes($attributes, true) . '>';
278  ‪$markup[] = ' <span class="icon-markup">';
279  ‪$markup[] = $this->‪getMarkup($alternativeMarkupIdentifier);
280  ‪$markup[] = ' </span>';
281  ‪$markup[] = ' {overlayMarkup}';
282  ‪$markup[] = '</span>';
283 
284  return implode(LF, ‪$markup);
285  }
286 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:38
‪TYPO3\CMS\Core\Imaging\Icon\$spinning
‪bool $spinning
Definition: Icon.php:87
‪TYPO3\CMS\Core\Imaging\Icon\$identifier
‪string $identifier
Definition: Icon.php:67
‪TYPO3\CMS\Core\Imaging\Icon\wrappedIcon
‪wrappedIcon(?string $alternativeMarkupIdentifier=null)
Definition: Icon.php:254
‪TYPO3\CMS\Core\Imaging
Definition: Dimension.php:16
‪TYPO3\CMS\Core\Imaging\Icon\getAlternativeMarkup
‪getAlternativeMarkup(string $markupIdentifier)
Definition: Icon.php:127
‪TYPO3\CMS\Core\Imaging\Icon\setAlternativeMarkup
‪$this setAlternativeMarkup(string $markupIdentifier, string $markup)
Definition: Icon.php:135
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_DEFAULT
‪const SIZE_DEFAULT
Definition: Icon.php:32
‪TYPO3\CMS\Core\Imaging\Icon\$state
‪IconState null $state
Definition: Icon.php:93
‪TYPO3\CMS\Core\Imaging\Icon\render
‪render(?string $alternativeMarkupIdentifier=null)
Definition: Icon.php:237
‪TYPO3\CMS\Core\Imaging\Icon\setIdentifier
‪$this setIdentifier(string $identifier)
Definition: Icon.php:149
‪TYPO3\CMS\Core\Imaging\Icon\getState
‪getState()
Definition: Icon.php:218
‪TYPO3\CMS\Core\Imaging\Icon\setSize
‪setSize(string|IconSize $size)
Definition: Icon.php:193
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:27
‪TYPO3\CMS\Core\Imaging\Icon\$title
‪string $title
Definition: Icon.php:72
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_OVERLAY
‪const SIZE_OVERLAY
Definition: Icon.php:62
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_MEDIUM
‪const SIZE_MEDIUM
Definition: Icon.php:44
‪TYPO3\CMS\Core\Imaging\Icon\$size
‪IconSize $size
Definition: Icon.php:82
‪TYPO3\CMS\Core\Imaging\Icon\setOverlayIcon
‪$this setOverlayIcon(?Icon $overlayIcon)
Definition: Icon.php:177
‪TYPO3\CMS\Core\Imaging\Icon\__toString
‪__toString()
Definition: Icon.php:246
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_MEGA
‪const SIZE_MEGA
Definition: Icon.php:56
‪TYPO3\CMS\Core\Imaging\Dimension
Definition: Dimension.php:22
‪TYPO3\CMS\Core\Imaging\Icon\getIdentifier
‪getIdentifier()
Definition: Icon.php:141
‪TYPO3\CMS\Core\Imaging\Icon\$overlayIcon
‪Icon $overlayIcon
Definition: Icon.php:77
‪TYPO3\CMS\Core\Imaging\Icon\isSpinning
‪isSpinning()
Definition: Icon.php:204
‪TYPO3\CMS\Core\Imaging\Icon\getOverlayIcon
‪getOverlayIcon()
Definition: Icon.php:169
‪TYPO3\CMS\Core\Imaging\Icon\setSpinning
‪$this setSpinning(bool $spinning)
Definition: Icon.php:212
‪TYPO3\CMS\Core\Imaging\Icon\getDimension
‪getDimension()
Definition: Icon.php:232
‪TYPO3\CMS\Core\Imaging\Icon\getMarkup
‪getMarkup(?string $alternativeMarkupIdentifier=null)
Definition: Icon.php:110
‪TYPO3\CMS\Core\Imaging\Icon\$markup
‪string $markup
Definition: Icon.php:101
‪TYPO3\CMS\Core\Imaging\Icon\setMarkup
‪$this setMarkup(string $markup)
Definition: Icon.php:121
‪TYPO3\CMS\Core\Imaging\IconState
‪IconState
Definition: IconState.php:24
‪TYPO3\CMS\Core\Imaging\Icon\setTitle
‪$this setTitle(?string $title)
Definition: Icon.php:163
‪TYPO3\CMS\Core\Imaging\Icon\$dimension
‪Dimension $dimension
Definition: Icon.php:97
‪TYPO3\CMS\Core\Imaging\Icon\getTitle
‪getTitle()
Definition: Icon.php:155
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_LARGE
‪const SIZE_LARGE
Definition: Icon.php:50
‪TYPO3\CMS\Core\Imaging\Icon\getSize
‪getSize()
Definition: Icon.php:183
‪TYPO3\CMS\Core\Imaging\Icon\setState
‪$this setState(IconState $state)
Definition: Icon.php:226
‪TYPO3\CMS\Core\Imaging\Icon\$alternativeMarkups
‪array $alternativeMarkups
Definition: Icon.php:105