[TASK] Simplify error assertion in two functional tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Imaging / Icon.php
1 <?php
2
3 /*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16 namespace TYPO3\CMS\Core\Imaging;
17
18 use TYPO3\CMS\Core\Type\Icon\IconState;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Icon object, holds all information for one icon, identified by the "identifier" property.
23 * Is available to render itself as string.
24 */
25 class Icon
26 {
27 /**
28 * @var string the small size
29 */
30 const SIZE_SMALL = 'small'; // 16
31
32 /**
33 * @var string the default size
34 */
35 const SIZE_DEFAULT = 'default'; // 32
36
37 /**
38 * @var string the large size
39 */
40 const SIZE_LARGE = 'large'; // 48
41
42 /**
43 * @internal
44 * @var string the overlay size, which depends on icon size
45 */
46 const SIZE_OVERLAY = 'overlay';
47
48 /**
49 * The identifier which the PHP code that calls the IconFactory hands over
50 *
51 * @var string
52 */
53 protected $identifier;
54
55 /**
56 * The identifier for a possible overlay icon
57 *
58 * @var Icon|null
59 */
60 protected $overlayIcon;
61
62 /**
63 * Contains the size string ("large", "small" or "default")
64 *
65 * @var string
66 */
67 protected $size = '';
68
69 /**
70 * Flag to indicate if the icon has a spinning animation
71 *
72 * @var bool
73 */
74 protected $spinning = false;
75
76 /**
77 * Contains the state information
78 *
79 * @var IconState
80 */
81 protected $state;
82
83 /**
84 * @var Dimension
85 */
86 protected $dimension;
87
88 /**
89 * @var string
90 */
91 protected $markup;
92
93 /**
94 * @var array
95 */
96 protected $alternativeMarkups = [];
97
98 /**
99 * @internal this method is used for internal processing, to get the prepared and final markup use render()
100 *
101 * @param string $alternativeMarkupIdentifier
102 *
103 * @return string
104 */
105 public function getMarkup($alternativeMarkupIdentifier = null)
106 {
107 if ($alternativeMarkupIdentifier !== null && isset($this->alternativeMarkups[$alternativeMarkupIdentifier])) {
108 return $this->alternativeMarkups[$alternativeMarkupIdentifier];
109 }
110 return $this->markup;
111 }
112
113 /**
114 * @param string $markup
115 */
116 public function setMarkup($markup)
117 {
118 $this->markup = $markup;
119 }
120
121 /**
122 * @param string $markupIdentifier
123 *
124 * @return string
125 */
126 public function getAlternativeMarkup($markupIdentifier)
127 {
128 return $this->alternativeMarkups[$markupIdentifier];
129 }
130
131 /**
132 * @param string $markupIdentifier
133 * @param string $markup
134 */
135 public function setAlternativeMarkup($markupIdentifier, $markup)
136 {
137 $this->alternativeMarkups[$markupIdentifier] = $markup;
138 }
139
140 /**
141 * @return string
142 */
143 public function getIdentifier()
144 {
145 return $this->identifier;
146 }
147
148 /**
149 * @param string $identifier
150 */
151 public function setIdentifier($identifier)
152 {
153 $this->identifier = $identifier;
154 }
155
156 /**
157 * @return Icon|null
158 */
159 public function getOverlayIcon()
160 {
161 return $this->overlayIcon;
162 }
163
164 /**
165 * @param Icon $overlayIcon
166 */
167 public function setOverlayIcon($overlayIcon)
168 {
169 $this->overlayIcon = $overlayIcon;
170 }
171
172 /**
173 * @return string
174 */
175 public function getSize()
176 {
177 return $this->size;
178 }
179
180 /**
181 * Sets the size and creates the new dimension
182 *
183 * @param string $size
184 */
185 public function setSize($size)
186 {
187 $this->size = $size;
188 $this->dimension = GeneralUtility::makeInstance(Dimension::class, $size);
189 }
190
191 /**
192 * @return bool
193 */
194 public function isSpinning()
195 {
196 return $this->spinning;
197 }
198
199 /**
200 * @param bool $spinning
201 */
202 public function setSpinning($spinning)
203 {
204 $this->spinning = $spinning;
205 }
206
207 /**
208 * @return IconState
209 */
210 public function getState()
211 {
212 return $this->state;
213 }
214
215 /**
216 * Sets the state of the icon
217 *
218 * @param IconState $state
219 */
220 public function setState(IconState $state)
221 {
222 $this->state = $state;
223 }
224
225 /**
226 * @return Dimension
227 */
228 public function getDimension()
229 {
230 return $this->dimension;
231 }
232
233 /**
234 * Render the icon as HTML code
235 *
236 * @param string $alternativeMarkupIdentifier
237 *
238 * @return string
239 */
240 public function render($alternativeMarkupIdentifier = null)
241 {
242 $overlayIconMarkup = '';
243 if ($this->overlayIcon !== null) {
244 $overlayIconMarkup = '<span class="icon-overlay icon-' . htmlspecialchars($this->overlayIcon->getIdentifier()) . '">' . $this->overlayIcon->getMarkup() . '</span>';
245 }
246 return str_replace('{overlayMarkup}', $overlayIconMarkup, $this->wrappedIcon($alternativeMarkupIdentifier));
247 }
248
249 /**
250 * Render the icon as HTML code
251 *
252 * @return string
253 */
254 public function __toString()
255 {
256 return $this->render();
257 }
258
259 /**
260 * Wrap icon markup in unified HTML code
261 *
262 * @param string $alternativeMarkupIdentifier
263 *
264 * @return string
265 */
266 protected function wrappedIcon($alternativeMarkupIdentifier = null)
267 {
268 $classes = [];
269 $classes[] = 't3js-icon';
270 $classes[] = 'icon';
271 $classes[] = 'icon-size-' . $this->size;
272 $classes[] = 'icon-state-' . htmlspecialchars((string)$this->state);
273 $classes[] = 'icon-' . $this->getIdentifier();
274 if ($this->isSpinning()) {
275 $classes[] = 'icon-spin';
276 }
277
278 $markup = [];
279 $markup[] = '<span class="' . htmlspecialchars(implode(' ', $classes)) . '" data-identifier="' . htmlspecialchars($this->getIdentifier()) . '">';
280 $markup[] = ' <span class="icon-markup">';
281 $markup[] = $this->getMarkup($alternativeMarkupIdentifier);
282 $markup[] = ' </span>';
283 $markup[] = ' {overlayMarkup}';
284 $markup[] = '</span>';
285
286 return implode(LF, $markup);
287 }
288 }