‪TYPO3CMS  10.4
AbstractMetaTagManager.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 {
29  protected ‪$defaultNameAttribute = 'name';
30 
38  protected ‪$defaultContentAttribute = 'content';
39 
45  protected ‪$defaultAllowMultipleOccurrences = false;
46 
52  protected ‪$subPropertySeparator = ':';
53 
83  protected ‪$handledProperties = [];
84 
90  protected ‪$properties = [];
91 
103  public function ‪addProperty(string $property, string $content, array $subProperties = [], bool $replace = false, string $type = '')
104  {
105  $property = strtolower($property);
106 
107  if (isset($this->handledProperties[$property])) {
108  $subPropertiesArray = [];
109  foreach ($subProperties as $subPropertyKey => $subPropertyValue) {
110  if (isset($this->handledProperties[$property]['allowedSubProperties'][$subPropertyKey])) {
111  $subPropertiesArray[$subPropertyKey] = is_array($subPropertyValue) ? $subPropertyValue : [$subPropertyValue];
112  }
113  }
114  if (!isset($this->properties[$property]) || empty($this->properties[$property])) {
115  $this->properties[$property][] = ['content' => $content, 'subProperties' => $subPropertiesArray];
116  } else {
117  if ($replace === true) {
118  $this->‪removeProperty($property, $type);
119  $this->properties[$property][] = ['content' => $content, 'subProperties' => $subPropertiesArray];
120  return;
121  }
122 
123  if (isset($this->handledProperties[$property]['allowMultipleOccurrences']) &&
124  (bool)$this->handledProperties[$property]['allowMultipleOccurrences']
125  ) {
126  $this->properties[$property][] = ['content' => $content, 'subProperties' => $subPropertiesArray];
127  }
128  }
129  } else {
130  // Check if there is an allowed subproperty that can handle the given property
131  foreach ($this->handledProperties as $handledProperty => $handledPropertyConfig) {
132  if (!isset($handledPropertyConfig['allowedSubProperties'])) {
133  continue;
134  }
135  foreach ((array)$handledPropertyConfig['allowedSubProperties'] as $allowedSubProperty => $allowedSubPropertyConfig) {
136  $propertyKey = is_array($allowedSubPropertyConfig) ? $allowedSubProperty : $allowedSubPropertyConfig;
137 
138  if ($property !== $handledProperty . $this->subPropertySeparator . $propertyKey ||
139  !isset($this->properties[$handledProperty])
140  ) {
141  continue;
142  }
143 
144  $propertyArrayKeys = array_keys($this->properties[$handledProperty]);
145  $lastIndex = end($propertyArrayKeys);
146 
147  if (!isset($this->properties[$handledProperty][$lastIndex]['subProperties'][$propertyKey])) {
148  $this->properties[$handledProperty][$lastIndex]['subProperties'][$propertyKey][] = $content;
149  } else {
150  if ($replace === true) {
151  unset($this->properties[$handledProperty][$lastIndex]['subProperties'][$propertyKey]);
152  $this->properties[$handledProperty][$lastIndex]['subProperties'][$propertyKey][] = $content;
153  return;
154  }
155 
156  if (is_array($allowedSubPropertyConfig) &&
157  isset($allowedSubPropertyConfig['allowMultipleOccurrences']) &&
158  (bool)$allowedSubPropertyConfig['allowMultipleOccurrences']
159  ) {
160  $this->properties[$handledProperty][$lastIndex]['subProperties'][$propertyKey][] = $content;
161  }
162  }
163 
164  return;
165  }
166  }
167 
168  throw new \UnexpectedValueException(
169  sprintf('This MetaTagManager can\'t handle property "%s"', $property),
170  1524209729
171  );
172  }
173  }
174 
180  public function ‪getAllHandledProperties(): array
181  {
183  }
184 
192  public function ‪getProperty(string $property, string $type = ''): array
193  {
194  $property = strtolower($property);
195 
196  if (isset($this->properties[$property])) {
197  return $this->properties[$property];
198  }
199 
200  return [];
201  }
202 
209  public function ‪renderProperty(string $property): string
210  {
211  $property = strtolower($property);
212  $metaTags = [];
213 
214  $nameAttribute = ‪$this->defaultNameAttribute;
215  if (isset($this->handledProperties[$property]['nameAttribute'])
216  && !empty((string)$this->handledProperties[$property]['nameAttribute'])) {
217  $nameAttribute = (string)$this->handledProperties[$property]['nameAttribute'];
218  }
219 
220  $contentAttribute = ‪$this->defaultContentAttribute;
221  if (isset($this->handledProperties[$property]['contentAttribute'])
222  && !empty((string)$this->handledProperties[$property]['contentAttribute'])) {
223  $contentAttribute = (string)$this->handledProperties[$property]['contentAttribute'];
224  }
225 
226  if ($nameAttribute && $contentAttribute) {
227  foreach ($this->‪getProperty($property) as $propertyItem) {
228  $metaTags[] = '<meta ' .
229  htmlspecialchars($nameAttribute) . '="' . htmlspecialchars($property) . '" ' .
230  htmlspecialchars($contentAttribute) . '="' . htmlspecialchars($propertyItem['content']) . '" />';
231 
232  if (!count($propertyItem['subProperties'])) {
233  continue;
234  }
235  foreach ($propertyItem['subProperties'] as $subProperty => $subPropertyItems) {
236  foreach ($subPropertyItems as $subPropertyItem) {
237  $metaTags[] = '<meta ' .
238  htmlspecialchars($nameAttribute) . '="' . htmlspecialchars($property . $this->subPropertySeparator . $subProperty) . '" ' .
239  htmlspecialchars($contentAttribute) . '="' . htmlspecialchars((string)$subPropertyItem) . '" />';
240  }
241  }
242  }
243  }
244 
245  return implode(PHP_EOL, $metaTags);
246  }
247 
253  public function ‪renderAllProperties(): string
254  {
255  $metatags = [];
256  foreach (array_keys($this->properties) as $property) {
257  $metatags[] = $this->‪renderProperty($property);
258  }
259 
260  return implode(PHP_EOL, $metatags);
261  }
262 
270  public function ‪removeProperty(string $property, string $type = '')
271  {
272  $property = strtolower($property);
273 
274  unset($this->properties[$property]);
275  }
276 
280  public function ‪removeAllProperties()
281  {
282  $this->properties = [];
283  }
284 
291  public function ‪canHandleProperty(string $property): bool
292  {
293  if (isset($this->handledProperties[$property])) {
294  return true;
295  }
296 
297  foreach ($this->handledProperties as $handledProperty => $handledPropertyConfig) {
298  foreach ((array)($handledPropertyConfig['allowedSubProperties'] ?? []) as $allowedSubProperty => $allowedSubPropertyConfig) {
299  $propertyKey = is_array($allowedSubPropertyConfig) ? $allowedSubProperty : $allowedSubPropertyConfig;
300  if ($property === $handledProperty . $this->subPropertySeparator . $propertyKey) {
301  return true;
302  }
303  }
304  }
305 
306  return false;
307  }
308 }
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\getProperty
‪array getProperty(string $property, string $type='')
Definition: AbstractMetaTagManager.php:186
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\removeProperty
‪removeProperty(string $property, string $type='')
Definition: AbstractMetaTagManager.php:264
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\renderProperty
‪string renderProperty(string $property)
Definition: AbstractMetaTagManager.php:203
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\getAllHandledProperties
‪array getAllHandledProperties()
Definition: AbstractMetaTagManager.php:174
‪TYPO3\CMS\Core\MetaTag\MetaTagManagerInterface
Definition: MetaTagManagerInterface.php:21
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\$subPropertySeparator
‪string $subPropertySeparator
Definition: AbstractMetaTagManager.php:48
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\$defaultContentAttribute
‪string $defaultContentAttribute
Definition: AbstractMetaTagManager.php:36
‪TYPO3\CMS\Core\MetaTag
Definition: AbstractMetaTagManager.php:18
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\canHandleProperty
‪bool canHandleProperty(string $property)
Definition: AbstractMetaTagManager.php:285
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager
Definition: AbstractMetaTagManager.php:21
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\$defaultAllowMultipleOccurrences
‪bool $defaultAllowMultipleOccurrences
Definition: AbstractMetaTagManager.php:42
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\$handledProperties
‪array $handledProperties
Definition: AbstractMetaTagManager.php:78
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\removeAllProperties
‪removeAllProperties()
Definition: AbstractMetaTagManager.php:274
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\$properties
‪array $properties
Definition: AbstractMetaTagManager.php:84
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\$defaultNameAttribute
‪string $defaultNameAttribute
Definition: AbstractMetaTagManager.php:28
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\renderAllProperties
‪string renderAllProperties()
Definition: AbstractMetaTagManager.php:247
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\addProperty
‪addProperty(string $property, string $content, array $subProperties=[], bool $replace=false, string $type='')
Definition: AbstractMetaTagManager.php:97