[TASK] Make TYPO3 Core PSR-2 standard compliant
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Link / ExternalViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Link;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 /**
15 * A view helper for creating links to external targets.
16 *
17 * = Examples =
18 *
19 * <code>
20 * <f:link.external uri="http://www.typo3.org" target="_blank">external link</f:link.external>
21 * </code>
22 * <output>
23 * <a href="http://www.typo3.org" target="_blank">external link</a>
24 * </output>
25 *
26 * <code title="custom default scheme">
27 * <f:link.external uri="typo3.org" defaultScheme="ftp">external ftp link</f:link.external>
28 * </code>
29 * <output>
30 * <a href="ftp://typo3.org">external ftp link</a>
31 * </output>
32 *
33 * @api
34 */
35 class ExternalViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
36 {
37 /**
38 * @var string
39 */
40 protected $tagName = 'a';
41
42 /**
43 * Initialize arguments
44 *
45 * @return void
46 * @api
47 */
48 public function initializeArguments()
49 {
50 $this->registerUniversalTagAttributes();
51 $this->registerTagAttribute('name', 'string', 'Specifies the name of an anchor');
52 $this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document');
53 $this->registerTagAttribute('rev', 'string', 'Specifies the relationship between the linked document and the current document');
54 $this->registerTagAttribute('target', 'string', 'Specifies where to open the linked document');
55 }
56
57 /**
58 * @param string $uri the URI that will be put in the href attribute of the rendered link tag
59 * @param string $defaultScheme scheme the href attribute will be prefixed with if specified $uri does not contain a scheme already
60 * @return string Rendered link
61 * @api
62 */
63 public function render($uri, $defaultScheme = 'http')
64 {
65 $scheme = parse_url($uri, PHP_URL_SCHEME);
66 if ($scheme === null && $defaultScheme !== '') {
67 $uri = $defaultScheme . '://' . $uri;
68 }
69 $this->tag->addAttribute('href', $uri);
70 $this->tag->setContent($this->renderChildren());
71 $this->tag->forceClosingTag(true);
72
73 return $this->tag->render();
74 }
75 }