Passed
Push — master ( fa79ba...4fcbee )
by Aimeos
08:39 queued 05:58
created

T3Router::transform()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 8
nop 6
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020-2022
6
 * @package MW
7
 * @subpackage View
8
 */
9
10
11
namespace Aimeos\Base\View\Helper\Url;
12
13
use TYPO3\CMS\Core\Routing\RouterInterface;
14
15
16
/**
17
 * View helper class for building URLs using the page router.
18
 *
19
 * @package MW
20
 * @subpackage View
21
 */
22
class T3Router
23
	extends \Aimeos\Base\View\Helper\Url\Base
24
	implements \Aimeos\Base\View\Helper\Url\Iface
25
{
26
	private $router;
27
	private $fixed;
28
29
30
	/**
31
	 * Initializes the URL view helper.
32
	 *
33
	 * @param \Aimeos\Base\View\Iface $view View instance with registered view helpers
34
	 * @param \TYPO3\CMS\Core\Routing\RouterInterface $router TYPO3 page router
35
	 * @param array $fixed Fixed parameters that should be added to each URL
36
	 */
37
	public function __construct( \Aimeos\Base\View\Iface $view, \TYPO3\CMS\Core\Routing\RouterInterface $router, array $fixed )
38
	{
39
		parent::__construct( $view );
40
41
		$this->router = clone $router;
42
		$this->fixed = $fixed;
43
	}
44
45
46
	/**
47
	 * Returns the URL assembled from the given arguments.
48
	 *
49
	 * @param string|null $target Route or page which should be the target of the link (if any)
50
	 * @param string|null $controller Name of the controller which should be part of the link (if any)
51
	 * @param string|null $action Name of the action which should be part of the link (if any)
52
	 * @param array $params Associative list of parameters that should be part of the URL
53
	 * @param array $trailing Trailing URL parts that are not relevant to identify the resource (for pretty URLs)
54
	 * @param array $config Additional configuration parameter per URL
55
	 * @return string Complete URL that can be used in the template
56
	 */
57
	public function transform( string $target = null, string $controller = null, string $action = null,
58
		array $params = [], array $trailing = [], array $config = [] ) : string
59
	{
60
		$params['controller'] = $controller !== null ? ucfirst( $controller ) : null;
61
		$params['action'] = $action;
62
63
		if( $params['locale'] ?? null ) {
64
			$params['L'] = $params['locale'];
65
		}
66
67
		$abs = !empty( $config['absoluteUri'] ) ? RouterInterface::ABSOLUTE_URL : RouterInterface::ABSOLUTE_PATH;
68
69
		return (string) $this->router->generateUri( $target, ['ai' => $params] + $this->fixed, join( '/', $trailing ), $abs );
70
	}
71
}
72