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!
20 * Handles typical meta tags (non-grouped). Use AbstractMetaTagManager
21 * to create you own MetaTags, this class is final by design
23 final class GenericMetaTagManager
implements MetaTagManagerInterface
26 * The separator to define subproperties like og:image:width
30 protected $subPropertySeparator = ':';
33 * Array of properties that are set by the manager
37 protected $properties = [];
40 * Add a property (including subProperties)
42 * @param string $property
43 * @param string $content
44 * @param array $subProperties
45 * @param bool $replace
48 public function addProperty(string $property, string $content, array $subProperties = [], bool $replace = false, string $type = 'name')
50 $property = strtolower($property);
51 $type = strtolower($type) ?
: 'name';
54 $this->removeProperty($property, $type);
57 $this->properties
[$property][$type][] = [
58 'content' => $content,
59 'subProperties' => $subProperties
64 * Get the data of a specific property
66 * @param string $property
70 public function getProperty(string $property, string $type = 'name'): array
72 $property = strtolower($property);
73 $type = strtolower($type) ?
: 'name';
75 if (!empty($this->properties
[$property][$type])) {
76 return $this->properties
[$property][$type];
82 * Returns an array with all properties that can be handled by this manager
85 public function getAllHandledProperties(): array
91 * Render all registered properties of this manager
95 public function renderAllProperties(): string
98 foreach (array_keys($this->properties
) as $property) {
99 $metatags[] = $this->renderProperty($property);
102 return implode(PHP_EOL
, $metatags);
106 * Render a specific property including subproperties of that property
108 * @param string $property
111 public function renderProperty(string $property): string
113 $property = strtolower($property);
116 foreach ((array)$this->properties
[$property] as $type => $propertyItems) {
117 foreach ($propertyItems as $propertyItem) {
118 $metaTags[] = '<meta ' .
119 htmlspecialchars($type) . '="' . htmlspecialchars($property) . '" ' .
120 'content="' . htmlspecialchars($propertyItem['content']) . '" />';
122 if (!count($propertyItem['subProperties'])) {
125 foreach ($propertyItem['subProperties'] as $subProperty => $value) {
126 $metaTags[] = '<meta ' .
127 htmlspecialchars($type) . '="' . htmlspecialchars($property . $this->subPropertySeparator
. $subProperty) . '" ' .
128 'content="' . htmlspecialchars((string)$value) . '" />';
133 return implode(PHP_EOL
, $metaTags);
137 * Remove one property from the MetaTagManager
138 * If there are multiple occurrences of a property, they all will be removed
140 * @param string $property
141 * @param string $type
143 public function removeProperty(string $property, string $type = '')
146 unset($this->properties
[$property][$type]);
148 unset($this->properties
[$property]);
153 * Unset all properties
155 public function removeAllProperties()
157 $this->properties
= [];
161 * @param string $property
164 public function canHandleProperty(string $property): bool