f954b3110863a9bf550f10f9be799a90c450c102
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Service / TypoLinkCodecService.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Service;
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
17 /**
18 * This class provides basic functionality to encode and decode typolink strings
19 */
20 class TypoLinkCodecService
21 {
22 /**
23 * Delimiter for TypoLink string parts
24 *
25 * @var string
26 */
27 protected static $partDelimiter = ' ';
28
29 /**
30 * Symbol for TypoLink parts not specified
31 *
32 * @var string
33 */
34 protected static $emptyValueSymbol = '-';
35
36 /**
37 * Encode TypoLink parts to a single string
38 *
39 * @param array $typoLinkParts Array with keys url and optionally any of target, class, title, additionalParams
40 * @return string Returns a correctly encoded TypoLink string
41 */
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 = isset($typoLinkParts[$key]) ? $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
71 /**
72 * Decodes a TypoLink string into its parts
73 *
74 * @param string $typoLink The properly encoded TypoLink string
75 * @return array Associative array of TypoLink parts with the keys url, target, class, title, additionalParams
76 */
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 }