Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
FilterFieldConversionListener
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 attach
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 onSearchPre
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3/**
4 * Listener to convert one field to another in filters (for legacy purposes).
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2013.
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   Oliver Goldschmidt <o.goldschmidt@tuhh.de>
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\Solr;
31
32use Laminas\EventManager\EventInterface;
33use Laminas\EventManager\SharedEventManagerInterface;
34use VuFindSearch\Service;
35
36use function is_array;
37
38/**
39 * Listener to convert one field to another in filters (for legacy purposes).
40 *
41 * @category VuFind
42 * @package  Search
43 * @author   Oliver Goldschmidt <o.goldschmidt@tuhh.de>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org Main Site
46 */
47class FilterFieldConversionListener
48{
49    /**
50     * Map of old field => new field.
51     *
52     * @var array
53     */
54    protected $map;
55
56    /**
57     * Constructor.
58     *
59     * @param array $map Map of old field => new field.
60     *
61     * @return void
62     */
63    public function __construct(array $map)
64    {
65        $this->map = $map;
66    }
67
68    /**
69     * Attach listener to shared event manager.
70     *
71     * @param SharedEventManagerInterface $manager Shared event manager
72     *
73     * @return void
74     */
75    public function attach(SharedEventManagerInterface $manager)
76    {
77        $manager->attach(
78            Service::class,
79            Service::EVENT_PRE,
80            [$this, 'onSearchPre']
81        );
82    }
83
84    /**
85     * Set up conditional hidden filters.
86     *
87     * @param EventInterface $event Event
88     *
89     * @return EventInterface
90     */
91    public function onSearchPre(EventInterface $event)
92    {
93        $params = $event->getParam('command')->getSearchParameters();
94        $fq = $params->get('fq');
95        if (is_array($fq) && !empty($fq)) {
96            // regex lookahead to ignore strings inside quotes:
97            $lookahead = '(?=(?:[^\"]*+\"[^\"]*+\")*+[^\"]*+$)';
98            $new_fq = [];
99            foreach ($fq as $currentFilter) {
100                foreach ($this->map as $oldField => $newField) {
101                    $currentFilter = preg_replace(
102                        "/\b$oldField:$lookahead/",
103                        "$newField:",
104                        $currentFilter
105                    );
106                }
107                $new_fq[] = $currentFilter;
108            }
109            $params->set('fq', $new_fq);
110        }
111
112        return $event;
113    }
114}