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