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