‪TYPO3CMS  ‪main
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 
85  protected ‪$handledProperties = [];
86 
92  protected ‪$properties = [];
93 
105  public function ‪addProperty(string $property, string $content, array $subProperties = [], bool $replace = false, string $type = '')
106  {
107  $property = strtolower($property);
108 
109  if (isset($this->handledProperties[$property])) {
110  $subPropertiesArray = [];
111  foreach ($subProperties as $subPropertyKey => $subPropertyValue) {
112  if (isset($this->handledProperties[$property]['allowedSubProperties'][$subPropertyKey])) {
113  $subPropertiesArray[$subPropertyKey] = is_array($subPropertyValue) ? $subPropertyValue : [$subPropertyValue];
114  }
115  }
116  if (!isset($this->properties[$property]) || empty($this->properties[$property])) {
117  $this->properties[$property][] = ['content' => $content, 'subProperties' => $subPropertiesArray];
118  } else {
119  if ($replace === true) {
120  $this->‪removeProperty($property, $type);
121  $this->properties[$property][] = ['content' => $content, 'subProperties' => $subPropertiesArray];
122  return;
123  }
124 
125  if (isset($this->handledProperties[$property]['allowMultipleOccurrences']) &&
126  (bool)$this->handledProperties[$property]['allowMultipleOccurrences']
127  ) {
128  $this->properties[$property][] = ['content' => $content, 'subProperties' => $subPropertiesArray];
129  }
130  }
131  } else {
132  // Check if there is an allowed subproperty that can handle the given property
133  foreach ($this->handledProperties as $handledProperty => $handledPropertyConfig) {
134  if (!isset($handledPropertyConfig['allowedSubProperties'])) {
135  continue;
136  }
137  foreach ((array)($handledPropertyConfig['allowedSubProperties'] ?? []) as $allowedSubProperty => $allowedSubPropertyConfig) {
138  $propertyKey = is_array($allowedSubPropertyConfig) ? $allowedSubProperty : $allowedSubPropertyConfig;
139 
140  if ($property !== $handledProperty . $this->subPropertySeparator . $propertyKey ||
141  !isset($this->properties[$handledProperty])
142  ) {
143  continue;
144  }
145 
146  $propertyArrayKeys = array_keys($this->properties[$handledProperty]);
147  $lastIndex = end($propertyArrayKeys);
148 
149  if (!isset($this->properties[$handledProperty][$lastIndex]['subProperties'][$propertyKey])) {
150  $this->properties[$handledProperty][$lastIndex]['subProperties'][$propertyKey][] = $content;
151  } else {
152  if ($replace === true) {
153  unset($this->properties[$handledProperty][$lastIndex]['subProperties'][$propertyKey]);
154  $this->properties[$handledProperty][$lastIndex]['subProperties'][$propertyKey][] = $content;
155  return;
156  }
157 
158  if (is_array($allowedSubPropertyConfig) &&
159  isset($allowedSubPropertyConfig['allowMultipleOccurrences']) &&
160  (bool)$allowedSubPropertyConfig['allowMultipleOccurrences']
161  ) {
162  $this->properties[$handledProperty][$lastIndex]['subProperties'][$propertyKey][] = $content;
163  }
164  }
165 
166  return;
167  }
168  }
169 
170  throw new \UnexpectedValueException(
171  sprintf('This MetaTagManager can\'t handle property "%s"', $property),
172  1524209729
173  );
174  }
175  }
176 
180  public function ‪getAllHandledProperties(): array
181  {
183  }
184 
191  public function ‪getProperty(string $property, string $type = ''): array
192  {
193  $property = strtolower($property);
194 
195  if (isset($this->properties[$property])) {
196  return $this->properties[$property];
197  }
198 
199  return [];
200  }
201 
207  public function ‪renderProperty(string $property): string
208  {
209  $property = strtolower($property);
210  $metaTags = [];
211 
212  $nameAttribute = ‪$this->defaultNameAttribute;
213  if (isset($this->handledProperties[$property]['nameAttribute'])
214  && !empty((string)$this->handledProperties[$property]['nameAttribute'])) {
215  $nameAttribute = (string)$this->handledProperties[$property]['nameAttribute'];
216  }
217 
218  $contentAttribute = ‪$this->defaultContentAttribute;
219  if (isset($this->handledProperties[$property]['contentAttribute'])
220  && !empty((string)$this->handledProperties[$property]['contentAttribute'])) {
221  $contentAttribute = (string)$this->handledProperties[$property]['contentAttribute'];
222  }
223 
224  if ($nameAttribute && $contentAttribute) {
225  foreach ($this->‪getProperty($property) as $propertyItem) {
226  $metaTags[] = '<meta ' .
227  htmlspecialchars($nameAttribute) . '="' . htmlspecialchars($property) . '" ' .
228  htmlspecialchars($contentAttribute) . '="' . htmlspecialchars($propertyItem['content']) . '" />';
229 
230  if (!count($propertyItem['subProperties'])) {
231  continue;
232  }
233  foreach ($propertyItem['subProperties'] as $subProperty => $subPropertyItems) {
234  foreach ($subPropertyItems as $subPropertyItem) {
235  $metaTags[] = '<meta ' .
236  htmlspecialchars($nameAttribute) . '="' . htmlspecialchars($property . $this->subPropertySeparator . $subProperty) . '" ' .
237  htmlspecialchars($contentAttribute) . '="' . htmlspecialchars((string)$subPropertyItem) . '" />';
238  }
239  }
240  }
241  }
242 
243  return implode(PHP_EOL, $metaTags);
244  }
245 
249  public function ‪renderAllProperties(): string
250  {
251  $metatags = [];
252  foreach (array_keys($this->properties) as $property) {
253  $metatags[] = $this->‪renderProperty($property);
254  }
255 
256  return implode(PHP_EOL, $metatags);
257  }
258 
263  public function ‪removeProperty(string $property, string $type = '')
264  {
265  $property = strtolower($property);
266 
267  unset($this->properties[$property]);
268  }
269 
273  public function ‪removeAllProperties()
274  {
275  $this->properties = [];
276  }
277 
283  public function ‪canHandleProperty(string $property): bool
284  {
285  if (isset($this->handledProperties[$property])) {
286  return true;
287  }
288 
289  foreach ($this->handledProperties as $handledProperty => $handledPropertyConfig) {
290  foreach ((array)($handledPropertyConfig['allowedSubProperties'] ?? []) as $allowedSubProperty => $allowedSubPropertyConfig) {
291  $propertyKey = is_array($allowedSubPropertyConfig) ? $allowedSubProperty : $allowedSubPropertyConfig;
292  if ($property === $handledProperty . $this->subPropertySeparator . $propertyKey) {
293  return true;
294  }
295  }
296  }
297 
298  return false;
299  }
300 
301  public function ‪getState(): array
302  {
303  return [
304  'properties' => ‪$this->properties,
305  ];
306  }
307 
308  public function ‪updateState(array $state): void
309  {
310  $this->properties = $state['properties'] ?? [];
311  }
312 }
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\removeProperty
‪removeProperty(string $property, string $type='')
Definition: AbstractMetaTagManager.php:257
‪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\getProperty
‪getProperty(string $property, string $type='')
Definition: AbstractMetaTagManager.php:185
‪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\updateState
‪updateState(array $state)
Definition: AbstractMetaTagManager.php:302
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\renderAllProperties
‪renderAllProperties()
Definition: AbstractMetaTagManager.php:243
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\getState
‪getState()
Definition: AbstractMetaTagManager.php:295
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager
Definition: AbstractMetaTagManager.php:21
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\getAllHandledProperties
‪getAllHandledProperties()
Definition: AbstractMetaTagManager.php:174
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\$defaultAllowMultipleOccurrences
‪bool $defaultAllowMultipleOccurrences
Definition: AbstractMetaTagManager.php:42
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\$handledProperties
‪array $handledProperties
Definition: AbstractMetaTagManager.php:80
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\removeAllProperties
‪removeAllProperties()
Definition: AbstractMetaTagManager.php:267
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\$properties
‪array $properties
Definition: AbstractMetaTagManager.php:86
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\$defaultNameAttribute
‪string $defaultNameAttribute
Definition: AbstractMetaTagManager.php:28
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\renderProperty
‪renderProperty(string $property)
Definition: AbstractMetaTagManager.php:201
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\canHandleProperty
‪canHandleProperty(string $property)
Definition: AbstractMetaTagManager.php:277
‪TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager\addProperty
‪addProperty(string $property, string $content, array $subProperties=[], bool $replace=false, string $type='')
Definition: AbstractMetaTagManager.php:99