Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
MakeLink | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
mergeAttributes | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
__invoke | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Make link view helper |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2020. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package View_Helpers |
25 | * @author Chris Hallberg <crhallberg@gmail.com> |
26 | * @author Demian Katz <demian.katz@villanova.edu> |
27 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
28 | * @link https://vufind.org/wiki/development Wiki |
29 | */ |
30 | |
31 | namespace VuFind\View\Helper\Root; |
32 | |
33 | use function is_array; |
34 | |
35 | /** |
36 | * Make link view helper |
37 | * |
38 | * @category VuFind |
39 | * @package View_Helpers |
40 | * @author Chris Hallberg <crhallberg@gmail.com> |
41 | * @author Demian Katz <demian.katz@villanova.edu> |
42 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
43 | * @link https://vufind.org/wiki/development Wiki |
44 | */ |
45 | class MakeLink extends \Laminas\View\Helper\AbstractHelper |
46 | { |
47 | /** |
48 | * Combine attributes including proxy |
49 | * |
50 | * @param string $href Link destination (null to skip) |
51 | * @param string|array $attrs Link attributes (class name or associative array) |
52 | * @param array $options Additional options |
53 | * |
54 | * @return array (associative) Combined attributes by key |
55 | */ |
56 | protected function mergeAttributes($href, $attrs, $options) |
57 | { |
58 | // If $attrs is not an object, interpret as class name |
59 | if (!is_array($attrs)) { |
60 | $attrs = !empty($attrs) ? ['class' => $attrs] : []; |
61 | } |
62 | |
63 | // Merge all attributes |
64 | $mergedAttrs = array_merge( |
65 | $attrs, |
66 | !empty($href) ? ['href' => $href] : [] |
67 | ); |
68 | |
69 | // Special option: proxy prefixing |
70 | if ($options['proxyUrl'] ?? false) { |
71 | $mergedAttrs['href'] = $options['proxyUrl'] . $mergedAttrs['href']; |
72 | } |
73 | |
74 | return $mergedAttrs; |
75 | } |
76 | |
77 | /** |
78 | * Render an HTML link |
79 | * |
80 | * $href will override $attrs['href'] |
81 | * > Feel free to use like makeLink('text', 'href', $defaults); |
82 | * |
83 | * If no $href, will try to find an href in $attrs |
84 | * > makeLink('text', null, ['href' => '#', 'class' => 'btn-link']) |
85 | * |
86 | * If $attrs is a string, will be treated like a class |
87 | * > makeLink('text', $href, 'btn-link') |
88 | * |
89 | * Additional options |
90 | * - proxyUrl: proxy url prefix before href |
91 | * - escapeContent: Default true, set to false to skip escaping (like for HTML). |
92 | * |
93 | * @param string $contents Link contents (must be properly-formed HTML) |
94 | * @param string $href Link destination (null to skip) |
95 | * @param string|array $attrs Link attributes (class name / associative array) |
96 | * @param array $options Additional options |
97 | * |
98 | * @return string HTML for an anchor tag |
99 | */ |
100 | public function __invoke( |
101 | string $contents, |
102 | string $href = null, |
103 | $attrs = [], |
104 | $options = [] |
105 | ) { |
106 | $mergedAttrs = $this->mergeAttributes($href, $attrs, $options); |
107 | |
108 | // Span instead of anchor when no href present |
109 | $tag = empty($mergedAttrs['href']) ? 'span' : 'a'; |
110 | |
111 | // Forward to makeTag helper |
112 | $makeTag = $this->getView()->plugin('makeTag'); |
113 | return $makeTag($tag, $contents, $mergedAttrs, $options); |
114 | } |
115 | } |