[FEATURE] Add TypoLinkCodec
[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 * @author Markus Klein <markus.klein@typo3.org>
21 */
22 class TypoLinkCodecService {
23
24 /**
25 * Delimiter for TypoLink string parts
26 *
27 * @var string
28 */
29 static protected $partDelimiter = ' ';
30
31 /**
32 * Symbol for TypoLink parts not specified
33 *
34 * @var string
35 */
36 static protected $emptyValueSymbol = '-';
37
38 /**
39 * Encode TypoLink parts to a single string
40 *
41 * @param array $typoLinkParts Array with keys url and optionally any of target, class, title, additionalParams
42 * @return string Returns a correctly encoded TypoLink string
43 */
44 public function encode(array $typoLinkParts) {
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 = isset($typoLinkParts[$key]) ? $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
72 /**
73 * Decodes a TypoLink string into its parts
74 *
75 * @param string $typoLink The properly encoded TypoLink string
76 * @return array Associative array of TypoLink parts with the keys url, target, class, title, additionalParams
77 */
78 public function decode($typoLink) {
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
98 }