2 declare(strict_types
= 1);
4 namespace TYPO3\CMS\Core\MetaTag
;
7 * This file is part of the TYPO3 CMS project.
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.
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
16 * The TYPO3 project - inspiring people to share!
19 use TYPO3\CMS\Core\SingletonInterface
;
22 * Handles typical meta tags (non-grouped). Use AbstractMetaTagManager
23 * to create you own MetaTags, this class is final by design
25 final class GenericMetaTagManager
implements MetaTagManagerInterface
, SingletonInterface
28 * The separator to define subproperties like og:image:width
32 protected $subPropertySeparator = ':';
35 * Array of properties that are set by the manager
39 protected $properties = [];
42 * Add a property (including subProperties)
44 * @param string $property
45 * @param string $content
46 * @param array $subProperties
47 * @param bool $replace
50 public function addProperty(string $property, string $content, array $subProperties = [], bool $replace = false, string $type = 'name')
52 $property = strtolower($property);
53 $type = strtolower($type) ?
: 'name';
56 $this->removeProperty($property, $type);
59 $this->properties
[$property][$type][] = [
60 'content' => $content,
61 'subProperties' => $subProperties
66 * Get the data of a specific property
68 * @param string $property
72 public function getProperty(string $property, string $type = 'name'): array
74 $property = strtolower($property);
75 $type = strtolower($type) ?
: 'name';
77 if (!empty($this->properties
[$property][$type])) {
78 return $this->properties
[$property][$type];
84 * Returns an array with all properties that can be handled by this manager
87 public function getAllHandledProperties(): array
93 * Render all registered properties of this manager
97 public function renderAllProperties(): string
100 foreach (array_keys($this->properties
) as $property) {
101 $metatags[] = $this->renderProperty($property);
104 return implode(PHP_EOL
, $metatags);
108 * Render a specific property including subproperties of that property
110 * @param string $property
113 public function renderProperty(string $property): string
115 $property = strtolower($property);
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']) . '" />';
124 if (!count($propertyItem['subProperties'])) {
127 foreach ($propertyItem['subProperties'] as $subProperty => $value) {
128 $metaTags[] = '<meta ' .
129 htmlspecialchars($type) . '="' . htmlspecialchars($property . $this->subPropertySeparator
. $subProperty) . '" ' .
130 'content="' . htmlspecialchars((string)$value) . '" />';
135 return implode(PHP_EOL
, $metaTags);
139 * Remove one property from the MetaTagManager
140 * If there are multiple occurrences of a property, they all will be removed
142 * @param string $property
143 * @param string $type
145 public function removeProperty(string $property, string $type = '')
148 unset($this->properties
[$property][$type]);
150 unset($this->properties
[$property]);
155 * Unset all properties
157 public function removeAllProperties()
159 $this->properties
= [];
163 * @param string $property
166 public function canHandleProperty(string $property): bool