b7a03a51dfc32a0a2495da853a2a32d6e6cf90e4
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / MetaTag / GenericMetaTagManager.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\MetaTag;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Core\SingletonInterface;
20
21 /**
22 * Handles typical meta tags (non-grouped). Use AbstractMetaTagManager
23 * to create you own MetaTags, this class is final by design
24 */
25 final class GenericMetaTagManager implements MetaTagManagerInterface, SingletonInterface
26 {
27 /**
28 * The separator to define subproperties like og:image:width
29 *
30 * @var string
31 */
32 protected $subPropertySeparator = ':';
33
34 /**
35 * Array of properties that are set by the manager
36 *
37 * @var array
38 */
39 protected $properties = [];
40
41 /**
42 * Add a property (including subProperties)
43 *
44 * @param string $property
45 * @param string $content
46 * @param array $subProperties
47 * @param bool $replace
48 * @param string $type
49 */
50 public function addProperty(string $property, string $content, array $subProperties = [], bool $replace = false, string $type = 'name')
51 {
52 $property = strtolower($property);
53 $type = strtolower($type) ?: 'name';
54
55 if ($replace) {
56 $this->removeProperty($property, $type);
57 }
58
59 $this->properties[$property][$type][] = [
60 'content' => $content,
61 'subProperties' => $subProperties
62 ];
63 }
64
65 /**
66 * Get the data of a specific property
67 *
68 * @param string $property
69 * @param string $type
70 * @return array
71 */
72 public function getProperty(string $property, string $type = 'name'): array
73 {
74 $property = strtolower($property);
75 $type = strtolower($type) ?: 'name';
76
77 if (!empty($this->properties[$property][$type])) {
78 return $this->properties[$property][$type];
79 }
80 return [];
81 }
82
83 /**
84 * Returns an array with all properties that can be handled by this manager
85 * @return array
86 */
87 public function getAllHandledProperties(): array
88 {
89 return [];
90 }
91
92 /**
93 * Render all registered properties of this manager
94 *
95 * @return string
96 */
97 public function renderAllProperties(): string
98 {
99 $metatags = [];
100 foreach (array_keys($this->properties) as $property) {
101 $metatags[] = $this->renderProperty($property);
102 }
103
104 return implode(PHP_EOL, $metatags);
105 }
106
107 /**
108 * Render a specific property including subproperties of that property
109 *
110 * @param string $property
111 * @return string
112 */
113 public function renderProperty(string $property): string
114 {
115 $property = strtolower($property);
116
117 $metaTags = [];
118 foreach ((array)$this->properties[$property] as $type => $propertyItems) {
119 foreach ($propertyItems as $propertyItem) {
120 $metaTags[] = '<meta ' .
121 htmlspecialchars($type) . '="' . htmlspecialchars($property) . '" ' .
122 'content="' . htmlspecialchars($propertyItem['content']) . '" />';
123
124 if (!count($propertyItem['subProperties'])) {
125 continue;
126 }
127 foreach ($propertyItem['subProperties'] as $subProperty => $value) {
128 $metaTags[] = '<meta ' .
129 htmlspecialchars($type) . '="' . htmlspecialchars($property . $this->subPropertySeparator . $subProperty) . '" ' .
130 'content="' . htmlspecialchars((string)$value) . '" />';
131 }
132 }
133 }
134
135 return implode(PHP_EOL, $metaTags);
136 }
137
138 /**
139 * Remove one property from the MetaTagManager
140 * If there are multiple occurrences of a property, they all will be removed
141 *
142 * @param string $property
143 * @param string $type
144 */
145 public function removeProperty(string $property, string $type = '')
146 {
147 if (!empty($type)) {
148 unset($this->properties[$property][$type]);
149 } else {
150 unset($this->properties[$property]);
151 }
152 }
153
154 /**
155 * Unset all properties
156 */
157 public function removeAllProperties()
158 {
159 $this->properties = [];
160 }
161
162 /**
163 * @param string $property
164 * @return bool
165 */
166 public function canHandleProperty(string $property): bool
167 {
168 return true;
169 }
170 }