1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Faulancer\View\Helper; |
4
|
|
|
|
5
|
|
|
use Faulancer\Exception\RouteInvalidException; |
6
|
|
|
use Faulancer\Http\Request; |
7
|
|
|
use Faulancer\Service\Config; |
8
|
|
|
use Faulancer\Translate\Translator; |
9
|
|
|
use Faulancer\View\AbstractViewHelper; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Link |
13
|
|
|
* |
14
|
|
|
* @package Faulancer\View\Helper |
15
|
|
|
* @author Florian Knapp <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Link extends AbstractViewHelper |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Render a ready-to-use link within an 'a' tag |
22
|
|
|
* |
23
|
|
|
* @param string $routeName |
24
|
|
|
* @param array $elementAttributes |
25
|
|
|
* @param array $parameter |
26
|
|
|
* @return string |
27
|
|
|
* |
28
|
|
|
* @throws RouteInvalidException |
29
|
|
|
*/ |
30
|
|
|
public function __invoke($routeName, $elementAttributes = [], $parameter = []) |
31
|
|
|
{ |
32
|
|
|
$id = ''; |
33
|
|
|
$class = ''; |
34
|
|
|
$style = ''; |
35
|
|
|
|
36
|
|
|
$serviceLocator = $this->getServiceLocator(); |
37
|
|
|
|
38
|
|
|
/** @var Config $config */ |
39
|
|
|
$config = $serviceLocator->get(Config::class); |
40
|
|
|
|
41
|
|
|
/** @var Request $request */ |
42
|
|
|
$request = $serviceLocator->get(Request::class); |
43
|
|
|
|
44
|
|
|
$route = $config->get('routes:' . $routeName); |
45
|
|
|
|
46
|
|
|
if (empty($route)) { |
47
|
|
|
throw new RouteInvalidException('No valid route for "' . $routeName . '" found.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($request->getPath() === $route['path']) { |
51
|
|
|
$elementAttributes['class'][] = 'selected'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
if (!empty($elementAttributes['id'])) { |
|
|
|
|
55
|
|
|
$id = ' id="' . implode(' ', $elementAttributes['id']) . '" '; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
if (!empty($elementAttributes['class'])){ |
|
|
|
|
59
|
|
|
$class = 'class="' . implode(' ', $elementAttributes['class']) . '" '; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
if (!empty($elementAttributes['style'])) { |
|
|
|
|
63
|
|
|
$style = 'style="' . implode(' ', $elementAttributes['style']) . '" '; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$linkPattern = '<a ' . $id . $class . $style . 'href="%s" onfocus="blur()">%s</a>'; |
67
|
|
|
|
68
|
|
|
if (!empty($route['i18n_key']) && !empty($route['title'])) { |
69
|
|
|
$translator = new Translator(); |
70
|
|
|
$route['title'] = $translator->translate($route['i18n_key']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$path = $route['path']; |
74
|
|
|
|
75
|
|
|
if (strpos($path, '(') !== false) { |
76
|
|
|
$path = $this->view->route($routeName, $parameter); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return sprintf($linkPattern, $path, $route['title']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.