Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
20.00% covered (danger)
20.00%
2 / 10
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Url
20.00% covered (danger)
20.00%
2 / 10
66.67% covered (warning)
66.67%
2 / 3
24.43
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 addQueryParameters
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * Url view helper (extending core Laminas helper with additional functionality)
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2019.
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 <challber@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\View\Helper\Root;
31
32use Laminas\Http\PhpEnvironment\Request;
33
34use function func_get_args;
35use function func_num_args;
36
37/**
38 * Url view helper (extending core Laminas helper with additional functionality)
39 *
40 * @category VuFind
41 * @package  View_Helpers
42 * @author   Chris Hallberg <challber@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org/wiki/development Wiki
45 */
46class Url extends \Laminas\View\Helper\Url
47{
48    /**
49     * Request (or null if unavailable)
50     *
51     * @var Request
52     */
53    protected $request = null;
54
55    /**
56     * Constructor
57     *
58     * @param Request $request Request object for GET parameters
59     */
60    public function __construct(Request $request = null)
61    {
62        $this->request = $request;
63    }
64
65    /**
66     * Generates a url given the name of a route.
67     *
68     * @param string             $name               Name of the route
69     * @param array              $params             Parameters for the link
70     * @param array|\Traversable $options            Options for the route
71     * @param bool               $reuseMatchedParams Whether to reuse matched
72     * parameters
73     *
74     * @see \Laminas\Router\RouteInterface::assemble()
75     *
76     * @throws \Laminas\View\Exception\RuntimeException If no RouteStackInterface was provided
77     * @throws \Laminas\View\Exception\RuntimeException If no RouteMatch was provided
78     * @throws \Laminas\View\Exception\RuntimeException If RouteMatch didn't contain a matched
79     * route name
80     * @throws \Laminas\View\Exception\InvalidArgumentException If the params object was not an
81     * array or Traversable object.
82     *
83     * @return self|string Url For the link href attribute
84     */
85    public function __invoke(
86        $name = null,
87        $params = [],
88        $options = [],
89        $reuseMatchedParams = false
90    ) {
91        // If argument list is empty, return object for method access:
92        return func_num_args() == 0 ? $this : parent::__invoke(...func_get_args());
93    }
94
95    /**
96     * Get URL with current GET parameters and add one
97     *
98     * @param array $params             Key-paired parameters
99     * @param bool  $reuseMatchedParams Whether to reuse matched parameters
100     *
101     * @return string
102     */
103    public function addQueryParameters($params, $reuseMatchedParams = true)
104    {
105        $requestQuery = (null !== $this->request)
106            ? $this->request->getQuery()->toArray() : [];
107        $options = [
108            'query' => array_merge($requestQuery, $params),
109            'normalize_path' => false, // fix for VUFIND-1392
110        ];
111        // If we don't have a route match, direct any url's to default route:
112        $routeName = $this->routeMatch ? null : 'default';
113        return ($this)($routeName, [], $options, $reuseMatchedParams);
114    }
115}