9c626cc28e8db72820b89835d775ad7129f2af5e
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / LinkHandling / PageLinkHandler.php
1 <?php
2 declare (strict_types = 1);
3 namespace TYPO3\CMS\Core\LinkHandling;
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
18 /**
19 * Resolves links to pages and the parameters given
20 */
21 class PageLinkHandler implements LinkHandlingInterface
22 {
23
24 /**
25 * The Base URN for this link handling to act on
26 * @var string
27 */
28 protected $baseUrn = 't3://page';
29
30 /**
31 * Returns all valid parameters for linking to a TYPO3 page as a string
32 *
33 * @param array $parameters
34 * @return string
35 */
36 public function asString(array $parameters): string
37 {
38 $urn = $this->baseUrn;
39 if (isset($parameters['pagealias']) && $parameters['pagealias'] !== 'current') {
40 $urn .= '?alias=' . $parameters['pagealias'];
41 } else {
42 $urn .= '?uid=' . $parameters['pageuid'];
43 }
44 $urn = rtrim($urn, ':');
45 if (!empty($parameters['pagetype'])) {
46 $urn .= '&type=' . $parameters['pagetype'];
47 }
48 if (!empty($parameters['parameters'])) {
49 $urn .= '&' . ltrim($parameters['parameters'], '?&');
50 }
51 if (!empty($parameters['fragment'])) {
52 $urn .= '#' . $parameters['fragment'];
53 }
54
55 return $urn;
56 }
57
58 /**
59 * Returns all relevant information built in the link to a page (see asString())
60 *
61 * @param array $data
62 * @return array
63 */
64 public function resolveHandlerData(array $data): array
65 {
66 $result = [];
67 if (isset($data['uid'])) {
68 $result['pageuid'] = $data['uid'];
69 unset($data['uid']);
70 }
71 if (isset($data['alias'])) {
72 $result['pagealias'] = $data['alias'];
73 unset($data['alias']);
74 }
75 if (isset($data['type'])) {
76 $result['pagetype'] = $data['type'];
77 unset($data['type']);
78 }
79 if (!empty($data)) {
80 $result['parameters'] = http_build_query($data);
81 }
82 if (empty($result)) {
83 $result['pageuid'] = 'current';
84 }
85
86 return $result;
87 }
88 }