‪TYPO3CMS  10.4
TypoLinkCodecService.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 {
28  protected static ‪$partDelimiter = ' ';
29 
35  protected static ‪$emptyValueSymbol = '-';
36 
43  public function ‪encode(array $typoLinkParts)
44  {
45  if (empty($typoLinkParts) || !isset($typoLinkParts['url'])) {
46  return '';
47  }
48 
49  // Get empty structure
50  $reverseSortedParameters = array_reverse($this->‪decode(''), true);
51  $aValueWasSet = false;
52  foreach ($reverseSortedParameters as $key => &$value) {
53  $value = $typoLinkParts[$key] ?? '';
54  // escape special character \ and "
55  $value = str_replace(['\\', '"'], ['\\\\', '\\"'], $value);
56  // enclose with quotes if a string contains the delimiter
57  if (strpos($value, static::$partDelimiter) !== false) {
58  $value = '"' . $value . '"';
59  }
60  // fill with - if another values has already been set
61  if ($value === '' && $aValueWasSet) {
62  $value = static::$emptyValueSymbol;
63  }
64  if ($value !== '') {
65  $aValueWasSet = true;
66  }
67  }
68 
69  return trim(implode(static::$partDelimiter, array_reverse($reverseSortedParameters, true)));
70  }
71 
78  public function ‪decode($typoLink)
79  {
80  $typoLink = trim($typoLink);
81  if ($typoLink !== '') {
82  $parts = str_replace(['\\\\', '\\"'], ['\\', '"'], str_getcsv($typoLink, static::$partDelimiter));
83  } else {
84  $parts = '';
85  }
86 
87  // The order of the entries is crucial!!
88  $typoLinkParts = [
89  'url' => isset($parts[0]) ? trim($parts[0]) : '',
90  'target' => isset($parts[1]) && $parts[1] !== static::$emptyValueSymbol ? trim($parts[1]) : '',
91  'class' => isset($parts[2]) && $parts[2] !== static::$emptyValueSymbol ? trim($parts[2]) : '',
92  'title' => isset($parts[3]) && $parts[3] !== static::$emptyValueSymbol ? trim($parts[3]) : '',
93  'additionalParams' => isset($parts[4]) && $parts[4] !== static::$emptyValueSymbol ? trim($parts[4]) : ''
94  ];
95 
96  return $typoLinkParts;
97  }
98 }
‪TYPO3\CMS\Frontend\Service
Definition: TypoLinkCodecService.php:16