Completed
Push — 2016.04 ( 690368...e61816 )
by Aimeos
04:37
created

Typo3::getValues()   F

Complexity

Conditions 9
Paths 256

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 9
eloc 27
nc 256
nop 1
dl 0
loc 47
rs 3.3333
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2014-2015
7
 * @package MW
8
 * @subpackage View
9
 */
10
11
12
namespace Aimeos\MW\View\Helper\Url;
13
14
15
/**
16
 * View helper class for building URLs.
17
 *
18
 * @package MW
19
 * @subpackage View
20
 */
21
class Typo3
22
	extends \Aimeos\MW\View\Helper\Url\Base
23
	implements \Aimeos\MW\View\Helper\Url\Iface
24
{
25
	private $uriBuilder;
26
	private $prefix;
27
	private $fixed;
28
29
30
	/**
31
	 * Initializes the URL view helper.
32
	 *
33
	 * @param \Aimeos\MW\View\Iface $view View instance with registered view helpers
34
	 * @param \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder $uriBuilder TYPO3 URI builder
35
	 * @param array $fixed Fixed parameters that should be added to each URL
36
	 */
37
	public function __construct( \Aimeos\MW\View\Iface $view, \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder $uriBuilder, array $fixed )
38
	{
39
		parent::__construct( $view );
40
41
		$this->prefix = $uriBuilder->getArgumentPrefix();
42
		$this->uriBuilder = $uriBuilder;
43
		$this->fixed = $fixed;
44
	}
45
46
47
	/**
48
	 * Returns the URL assembled from the given arguments.
49
	 *
50
	 * @param string|null $target Route or page which should be the target of the link (if any)
51
	 * @param string|null $controller Name of the controller which should be part of the link (if any)
52
	 * @param string|null $action Name of the action which should be part of the link (if any)
53
	 * @param array $params Associative list of parameters that should be part of the URL
54
	 * @param array $trailing Trailing URL parts that are not relevant to identify the resource (for pretty URLs)
55
	 * @param array $config Additional configuration parameter per URL
56
	 * @return string Complete URL that can be used in the template
57
	 */
58
	public function transform( $target = null, $controller = null, $action = null, array $params = array(), array $trailing = array(), array $config = array() )
59
	{
60
		$arguments = $this->fixed;
61
		$arguments['controller'] = $controller;
62
		$arguments['action'] = $action;
63
64
		$values = $this->getValues( $config );
65
66
		if( $this->prefix != '' )
67
		{
68
			if( $values['namespace'] === true ) {
69
				$params = array( $this->prefix => $arguments + $params );
70
			} else {
71
				$params = $params + array( $this->prefix => $arguments );
72
			}
73
		}
74
75
		if( isset( $config['eID'] ) ) {
76
			$params['eID'] = $config['eID'];
77
		}
78
79
		$params = $this->sanitize( $params );
80
81
		$this->uriBuilder
82
			->reset()
83
			->setTargetPageUid( $target )
84
			->setSection( join( '/', $trailing ) )
85
			->setCreateAbsoluteUri( $values['absoluteUri'] )
86
			->setTargetPageType( $values['type'] )
87
			->setUseCacheHash( $values['chash'] )
88
			->setNoCache( $values['nocache'] )
89
			->setFormat( $values['format'] )
90
			->setArguments( $params );
91
92
		if( isset( $config['BE'] ) && $config['BE'] == true ) {
93
			return $this->uriBuilder->buildBackendUri();
94
		}
95
96
		return $this->uriBuilder->buildFrontendUri();
97
	}
98
99
100
	/**
101
	 * Returns the sanitized configuration values.
102
	 *
103
	 * @param array $config Associative list of key/value pairs
104
	 * @return array Associative list of sanitized key/value pairs
105
	 */
106
	protected function getValues( array $config )
107
	{
108
		$values = array(
109
			'plugin' => null,
110
			'extension' => null,
111
			'absoluteUri' => false,
112
			'namespace' => true,
113
			'nocache' => false,
114
			'chash' => true,
115
			'format' => '',
116
			'type' => 0,
117
		);
118
119
		if( isset( $config['plugin'] ) ) {
120
			$values['plugin'] = (string) $config['plugin'];
121
		}
122
123
		if( isset( $config['extension'] ) ) {
124
			$values['extension'] = (string) $config['extension'];
125
		}
126
127
		if( isset( $config['absoluteUri'] ) ) {
128
			$values['absoluteUri'] = (bool) $config['absoluteUri'];
129
		}
130
131
		if( isset( $config['namespace'] ) ) {
132
			$values['namespace'] = (bool) $config['namespace'];
133
		}
134
135
		if( isset( $config['nocache'] ) ) {
136
			$values['nocache'] = (bool) $config['nocache'];
137
		}
138
139
		if( isset( $config['chash'] ) ) {
140
			$values['chash'] = (bool) $config['chash'];
141
		}
142
143
		if( isset( $config['type'] ) ) {
144
			$values['type'] = (int) $config['type'];
145
		}
146
147
		if( isset( $config['format'] ) ) {
148
			$values['format'] = (string) $config['format'];
149
		}
150
151
		return $values;
152
	}
153
}
154