Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractHandlerMap
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 prepare
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getInvariants
n/a
0 / 0
n/a
0 / 0
0
 getDefaults
n/a
0 / 0
n/a
0 / 0
0
 getAppends
n/a
0 / 0
n/a
0 / 0
0
 apply
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Base class for search backend handler maps.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
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   David Maus <maus@hab.de>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org
28 */
29
30namespace VuFindSearch\Backend;
31
32use VuFindSearch\ParamBag;
33
34/**
35 * Base class for search backend handler maps.
36 *
37 * The handler map maps search functions to parameterizable backend request
38 * handlers. The base class implements the parameter preparation method which
39 * applies query defaults, appends, and invariants to an existing set of
40 * parameters.
41 *
42 * @category VuFind
43 * @package  Search
44 * @author   David Maus <maus@hab.de>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org
47 */
48abstract class AbstractHandlerMap
49{
50    /**
51     * Prepare final set of parameters for search function.
52     *
53     * Applies the query defaults, appends, and invariants.
54     *
55     * The concept of defaults, appends, and invariants follows SOLR with
56     * regards to the order of the application process: Invariants come last
57     * and overwrite runtime parameters, defaults, and appends.
58     *
59     * @param string   $function Name of search function
60     * @param ParamBag $params   Parameters
61     *
62     * @return void
63     */
64    public function prepare($function, ParamBag $params)
65    {
66        $defaults   = $this->getDefaults($function)->getArrayCopy();
67        $invariants = $this->getInvariants($function)->getArrayCopy();
68        $appends    = $this->getAppends($function)->getArrayCopy();
69        $this->apply($params, $defaults, $appends, $invariants);
70    }
71
72    /**
73     * Return query invariants for search function.
74     *
75     * @param string $function Name of search function
76     *
77     * @return ParamBag Query invariants
78     */
79    abstract public function getInvariants($function);
80
81    /**
82     * Return query defaults for search function.
83     *
84     * @param string $function Name of search function
85     *
86     * @return ParamBag Query defaults
87     */
88    abstract public function getDefaults($function);
89
90    /**
91     * Return query appends for search function.
92     *
93     * @param string $function Name of search function
94     *
95     * @return ParamBag Query appends
96     */
97    abstract public function getAppends($function);
98
99    /// Internal API
100
101    /**
102     * Apply query defaults, appends, invariants.
103     *
104     * @param ParamBag $params     Parameters
105     * @param array    $defaults   Query defaults
106     * @param array    $appends    Query appends
107     * @param array    $invariants Query invariants
108     *
109     * @return void
110     */
111    protected function apply(
112        ParamBag $params,
113        array $defaults,
114        array $appends,
115        array $invariants
116    ) {
117        $final = $params->getArrayCopy();
118        $final = array_replace($defaults, $final);
119        $final = array_merge_recursive($final, $appends);
120        $final = array_replace($final, $invariants);
121        $params->exchangeArray($final);
122    }
123}