‪TYPO3CMS  ‪main
GenericMetaTagManager.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 
25 {
31  protected ‪$subPropertySeparator = ':';
32 
38  protected ‪$properties = [];
39 
43  public function ‪addProperty(string $property, string $content, array $subProperties = [], bool $replace = false, string $type = 'name')
44  {
45  $property = strtolower($property);
46  $type = strtolower($type) ?: 'name';
47 
48  if ($replace) {
49  $this->‪removeProperty($property, $type);
50  }
51 
52  $this->properties[$property][$type][] = [
53  'content' => $content,
54  'subProperties' => $subProperties,
55  ];
56  }
57 
61  public function ‪getProperty(string $property, string $type = 'name'): array
62  {
63  $property = strtolower($property);
64  $type = strtolower($type) ?: 'name';
65 
66  if (!empty($this->properties[$property][$type])) {
67  return $this->properties[$property][$type];
68  }
69  return [];
70  }
71 
75  public function ‪getAllHandledProperties(): array
76  {
77  return [];
78  }
79 
83  public function ‪renderAllProperties(): string
84  {
85  $metatags = [];
86  foreach (array_keys($this->properties) as $property) {
87  $metatags[] = $this->‪renderProperty($property);
88  }
89 
90  return implode(PHP_EOL, $metatags);
91  }
92 
96  public function ‪renderProperty(string $property): string
97  {
98  $property = strtolower($property);
99 
100  $metaTags = [];
101  foreach ((array)$this->properties[$property] as $type => $propertyItems) {
102  foreach ($propertyItems as $propertyItem) {
103  $metaTags[] = '<meta ' .
104  htmlspecialchars($type) . '="' . htmlspecialchars($property) . '" ' .
105  'content="' . htmlspecialchars($propertyItem['content']) . '" />';
106 
107  if (!count($propertyItem['subProperties'])) {
108  continue;
109  }
110  foreach ($propertyItem['subProperties'] as $subProperty => $value) {
111  $metaTags[] = '<meta ' .
112  htmlspecialchars($type) . '="' . htmlspecialchars($property . $this->subPropertySeparator . $subProperty) . '" ' .
113  'content="' . htmlspecialchars((string)$value) . '" />';
114  }
115  }
116  }
117 
118  return implode(PHP_EOL, $metaTags);
119  }
120 
125  public function ‪removeProperty(string $property, string $type = '')
126  {
127  $property = strtolower($property);
128  $type = strtolower($type);
129 
130  if (!empty($type)) {
131  unset($this->properties[$property][$type]);
132  } else {
133  unset($this->properties[$property]);
134  }
135  }
136 
140  public function ‪removeAllProperties()
141  {
142  $this->properties = [];
143  }
144 
145  public function ‪canHandleProperty(string $property): bool
146  {
147  return true;
148  }
149 
150  public function ‪getState(): array
151  {
152  return [
153  'properties' => ‪$this->properties,
154  ];
155  }
156 
157  public function ‪updateState(array $state): void
158  {
159  $this->properties = $state['properties'] ?? [];
160  }
161 }
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager\updateState
‪updateState(array $state)
Definition: GenericMetaTagManager.php:155
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager\renderProperty
‪renderProperty(string $property)
Definition: GenericMetaTagManager.php:94
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager\$properties
‪array $properties
Definition: GenericMetaTagManager.php:36
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager\getState
‪getState()
Definition: GenericMetaTagManager.php:148
‪TYPO3\CMS\Core\MetaTag\MetaTagManagerInterface
Definition: MetaTagManagerInterface.php:21
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager\addProperty
‪addProperty(string $property, string $content, array $subProperties=[], bool $replace=false, string $type='name')
Definition: GenericMetaTagManager.php:41
‪TYPO3\CMS\Core\MetaTag
Definition: AbstractMetaTagManager.php:18
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager\getAllHandledProperties
‪getAllHandledProperties()
Definition: GenericMetaTagManager.php:73
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager\getProperty
‪getProperty(string $property, string $type='name')
Definition: GenericMetaTagManager.php:59
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager\$subPropertySeparator
‪string $subPropertySeparator
Definition: GenericMetaTagManager.php:30
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager
Definition: GenericMetaTagManager.php:25
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager\removeAllProperties
‪removeAllProperties()
Definition: GenericMetaTagManager.php:138
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager\removeProperty
‪removeProperty(string $property, string $type='')
Definition: GenericMetaTagManager.php:123
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager\canHandleProperty
‪canHandleProperty(string $property)
Definition: GenericMetaTagManager.php:143
‪TYPO3\CMS\Core\MetaTag\GenericMetaTagManager\renderAllProperties
‪renderAllProperties()
Definition: GenericMetaTagManager.php:81