Zend2   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A transform() 0 27 5
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2014-2018
6
 * @package MW
7
 * @subpackage View
8
 */
9
10
11
namespace Aimeos\MW\View\Helper\Url;
12
13
14
/**
15
 * View helper class for building URLs using Zend2 Router.
16
 *
17
 * @package MW
18
 * @subpackage View
19
 */
20
class Zend2
21
	extends \Aimeos\MW\View\Helper\Base
22
	implements \Aimeos\MW\View\Helper\Iface
23
{
24
	private $router;
25
	private $serverUrl;
26
27
28
	/**
29
	 * Initializes the URL view helper.
30
	 *
31
	 * @param \Aimeos\MW\View\Iface $view View instance with registered view helpers
32
	 * @param \Zend\Mvc\Router\RouteInterface $router Zend Router implementation
33
	 * @param string $serverUrl Url of the server including scheme, host and port
34
	 */
35
	public function __construct( $view, \Zend\Mvc\Router\RouteInterface $router, $serverUrl )
36
	{
37
		parent::__construct( $view );
38
39
		$this->router = $router;
40
		$this->serverUrl = $serverUrl;
41
	}
42
43
44
	/**
45
	 * Returns the URL assembled from the given arguments.
46
	 *
47
	 * @param string|null $target Route or page which should be the target of the link (if any)
48
	 * @param string|null $controller Name of the controller which should be part of the link (if any)
49
	 * @param string|null $action Name of the action which should be part of the link (if any)
50
	 * @param array $params Associative list of parameters that should be part of the URL
51
	 * @param array $trailing Trailing URL parts that are not relevant to identify the resource (for pretty URLs)
52
	 * @param array $config Additional configuration parameter per URL
53
	 * @return string Complete URL that can be used in the template
54
	 */
55
	public function transform( $target = null, $controller = null, $action = null, array $params = [], array $trailing = [], array $config = [] )
56
	{
57
		$paramList = array( 'module' => $target, 'controller' => $controller, 'action' => $action );
58
59
60
		foreach( $params as $key => $value )
61
		{
62
			// Slashes in URL parameters confuses the router
63
			$paramList[$key] = str_replace( '/', '', $value );
64
65
			// Arrays are not supported
66
			if( is_array( $value ) ) {
67
				$paramList[$key] = implode( ' ', $value );
68
			}
69
		}
70
71
		if( !empty( $trailing ) ) {
72
			$paramList['trailing'] = str_replace( '/', '-', join( '-', $trailing ) );
73
		}
74
75
		$url = $this->router->assemble( $paramList, [] );
76
77
		if( isset( $config['absoluteUri'] ) ) {
78
			$url = $this->serverUrl . $url;
79
		}
80
81
		return $url;
82
	}
83
}