28650eba2e3bba175783d99538e0ff56187c1884
[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 use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18 /**
19 * Icon object, holds all information for one icon, identified by the "identifier" property.
20 * Is available to render itself as string.
21 */
22 class Icon {
23
24 /**
25 * @var string the small size
26 */
27 const SIZE_SMALL = 'small'; // 16
28
29 /**
30 * @var string the default size
31 */
32 const SIZE_DEFAULT = 'default'; // 32
33
34 /**
35 * @var string the large size
36 */
37 const SIZE_LARGE = 'large'; // 48
38
39 /**
40 * @internal
41 * @var string the overlay size, which depends on icon size
42 */
43 const SIZE_OVERLAY = 'overlay';
44
45 /**
46 * The identifier which the PHP code that calls the IconFactory hands over
47 * @var string
48 */
49 protected $identifier;
50
51 /**
52 * The identifier for a possible overlay icon
53 * @var Icon
54 */
55 protected $overlayIcon = NULL;
56
57 /**
58 * Contains the size string ("large", "small" or "default")
59 * @var string
60 */
61 protected $size = '';
62
63 /**
64 * @var Dimension
65 */
66 protected $dimension;
67
68 /**
69 * @var string
70 */
71 protected $markup;
72
73 /**
74 * @internal this method is used for internal processing, to get the prepared and final markup use render()
75 * @return string
76 */
77 public function getMarkup() {
78 return $this->markup;
79 }
80
81 /**
82 * @param string $markup
83 */
84 public function setMarkup($markup) {
85 $this->markup = $markup;
86 }
87
88 /**
89 * @return string
90 */
91 public function getIdentifier() {
92 return $this->identifier;
93 }
94
95 /**
96 * @param string $identifier
97 */
98 public function setIdentifier($identifier) {
99 $this->identifier = $identifier;
100 }
101
102 /**
103 * @return Icon
104 */
105 public function getOverlayIcon() {
106 return $this->overlayIcon;
107 }
108
109 /**
110 * @param Icon $overlayIcon
111 */
112 public function setOverlayIcon($overlayIcon) {
113 $this->overlayIcon = $overlayIcon;
114 }
115
116 /**
117 * @return string
118 */
119 public function getSize() {
120 return $this->size;
121 }
122
123 /**
124 * Sets the size and creates the new dimension
125 * @param string $size
126 */
127 public function setSize($size) {
128 $this->size = $size;
129 $this->dimension = GeneralUtility::makeInstance(Dimension::class, $size);
130 }
131
132 /**
133 * @return Dimension
134 */
135 public function getDimension() {
136 return $this->dimension;
137 }
138
139 /**
140 * Render the icon as HTML code
141 *
142 * @return string
143 */
144 public function render() {
145 return $this->__toString();
146 }
147
148 /**
149 * Render the icon as HTML code
150 *
151 * @return string
152 */
153 public function __toString() {
154 $overlayIconMarkup = '';
155 if ($this->overlayIcon !== NULL) {
156 $overlayIconMarkup = '<span class="icon-overlay icon-' . htmlspecialchars($this->overlayIcon->getIdentifier()) . '">' . $this->overlayIcon->getMarkup() . '</span>';
157 }
158 return str_replace('{overlayMarkup}', $overlayIconMarkup, $this->wrappedIcon());
159 }
160
161 /**
162 * Wrap icon markup in unified HTML code
163 *
164 * @return string
165 */
166 protected function wrappedIcon() {
167 $markup = array();
168 $markup[] = '<span class="icon icon-size-' . $this->size . ' icon-' . htmlspecialchars($this->getIdentifier()) . '">';
169 $markup[] = ' <span class="icon-markup">';
170 $markup[] = $this->getMarkup();
171 $markup[] = ' </span>';
172 $markup[] = ' {overlayMarkup}';
173 $markup[] = '</span>';
174
175 return implode(LF, $markup);
176 }
177 }