Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.76% covered (warning)
87.76%
43 / 49
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
UrlQueryHelperFactory
87.76% covered (warning)
87.76%
43 / 49
75.00% covered (warning)
75.00%
3 / 4
20.73
0.00% covered (danger)
0.00%
0 / 1
 getDefaults
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 addDefaultsToConfig
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 getUrlParams
76.92% covered (warning)
76.92%
20 / 26
0.00% covered (danger)
0.00%
0 / 1
15.08
 fromParams
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Factory to build UrlQueryHelper.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2016.
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  Search
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\Search\Factory;
31
32use VuFind\Search\Base\Params;
33use VuFind\Search\UrlQueryHelper;
34
35/**
36 * Factory to build UrlQueryHelper.
37 *
38 * @category VuFind
39 * @package  Search
40 * @author   Demian Katz <demian.katz@villanova.edu>
41 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
42 * @link     https://vufind.org Main Site
43 */
44class UrlQueryHelperFactory
45{
46    /**
47     * Name of class built by factory.
48     *
49     * @var string
50     */
51    protected $helperClass = UrlQueryHelper::class;
52
53    /**
54     * Extract default settings from the search parameters.
55     *
56     * @param Params $params VuFind search parameters
57     *
58     * @return array
59     */
60    protected function getDefaults(Params $params)
61    {
62        $options = $params->getOptions();
63        return [
64            'handler' => $options->getDefaultHandler(),
65            'limit' => $options->getDefaultLimit(),
66            'selectedShards' => $options->getDefaultSelectedShards(),
67            'sort' => $params->getDefaultSort(),
68            'view' => $options->getDefaultView(),
69        ];
70    }
71
72    /**
73     * Load default settings into the user-provided configuration.
74     *
75     * @param Params $params VuFind search parameters
76     * @param array  $config Config options
77     *
78     * @return array
79     */
80    protected function addDefaultsToConfig(Params $params, array $config)
81    {
82        // Load defaults unless they have been overridden in existing config
83        // array.
84        foreach ($this->getDefaults($params) as $key => $value) {
85            if (!isset($config['defaults'][$key])) {
86                $config['defaults'][$key] = $value;
87            }
88        }
89
90        // Load useful callbacks if they have not been specifically overridden
91        if (!isset($config['parseFilterCallback'])) {
92            $config['parseFilterCallback'] = [$params, 'parseFilter'];
93        }
94        if (!isset($config['getAliasesForFacetFieldCallback'])) {
95            $config['getAliasesForFacetFieldCallback']
96                = [$params, 'getAliasesForFacetField'];
97        }
98        return $config;
99    }
100
101    /**
102     * Extract URL query parameters from VuFind search parameters.
103     *
104     * @param Params $params VuFind search parameters
105     * @param array  $config Config options
106     *
107     * @return array
108     */
109    protected function getUrlParams(Params $params, array $config)
110    {
111        $urlParams = [];
112        $sort = $params->getSort();
113        if (null !== $sort && $sort != $config['defaults']['sort']) {
114            $urlParams['sort'] = $sort;
115        }
116        $limit = $params->getLimit();
117        if (null !== $limit && $limit != $config['defaults']['limit']) {
118            $urlParams['limit'] = $limit;
119        }
120        $view = $params->getView();
121        if (null !== $view && $view != $config['defaults']['view']) {
122            $urlParams['view'] = $view;
123        }
124        if ($params->getPage() != 1) {
125            $urlParams['page'] = $params->getPage();
126        }
127        if ($filters = $params->getFiltersAsQueryParams()) {
128            $urlParams['filter'] = $filters;
129        }
130        if ($hiddenFilters = $params->getHiddenFiltersAsQueryParams()) {
131            $urlParams['hiddenFilters'] = $hiddenFilters;
132        }
133        $shards = $params->getSelectedShards();
134        if (!empty($shards)) {
135            sort($shards);
136            $defaultShards = $config['defaults']['selectedShards'];
137            sort($defaultShards);
138            if (implode(':::', $shards) != implode(':::', $defaultShards)) {
139                $urlParams['shard'] = $shards;
140            }
141        }
142        if ($params->hasDefaultsApplied()) {
143            $urlParams['dfApplied'] = 1;
144        }
145        return $urlParams;
146    }
147
148    /**
149     * Construct the UrlQueryHelper
150     *
151     * @param Params $params VuFind search parameters
152     * @param array  $config Config options
153     *
154     * @return UrlQueryHelper
155     */
156    public function fromParams(Params $params, array $config = [])
157    {
158        $finalConfig = $this->addDefaultsToConfig($params, $config);
159        return new $this->helperClass(
160            $this->getUrlParams($params, $finalConfig),
161            $params->getQuery(),
162            $finalConfig
163        );
164    }
165}