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