Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
NormalizedSearch
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
7 / 7
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getRawResults
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMinified
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNormalizedResults
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getChecksum
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isEquivalentToMinifiedSearch
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Normalized search object.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2022.
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/wiki/development Wiki
28 */
29
30namespace VuFind\Search;
31
32use minSO;
33use VuFind\Search\Base\Results;
34use VuFind\Search\Results\PluginManager as ResultsManager;
35
36use function get_class;
37
38/**
39 * Normalized search object.
40 *
41 * @category VuFind
42 * @package  Search
43 * @author   Demian Katz <demian.katz@villanova.edu>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org/wiki/development Wiki
46 */
47class NormalizedSearch
48{
49    /**
50     * Search results manager
51     *
52     * @var ResultsManager
53     */
54    protected $resultsManager;
55
56    /**
57     * Raw search object provided to constructor
58     *
59     * @var Results
60     */
61    protected $raw;
62
63    /**
64     * Minified version of search
65     *
66     * @var Minified
67     */
68    protected $minified;
69
70    /**
71     * Normalized search object
72     *
73     * @var Results
74     */
75    protected $normalized;
76
77    /**
78     * Search URL from normalized search object
79     *
80     * @var string
81     */
82    protected $url;
83
84    /**
85     * Checksum of normalized search URL
86     *
87     * @var string
88     */
89    protected $checksum;
90
91    /**
92     * Constructor
93     *
94     * @param ResultsManager $resultsManager ResultsManager
95     * @param Results        $results        Search results object
96     */
97    public function __construct(ResultsManager $resultsManager, Results $results)
98    {
99        $this->resultsManager = $resultsManager;
100        $this->raw = $results;
101        // Normalize the URL params by minifying and deminifying the search object;
102        // note that we use the "minSO" subclass of the Minified class so that it
103        // serializes as small as possible in the database.
104        $this->minified = new minSO($results);
105        $this->normalized = $this->minified->deminify($resultsManager);
106        $this->url = $this->normalized->getUrlQuery()->getParams();
107        // Use crc32 as the checksum but get rid of highest bit so that we don't
108        // need to care about signed/unsigned issues
109        // (note: the checksum doesn't need to be unique)
110        $this->checksum = crc32($this->url) & 0xFFFFFFF;
111    }
112
113    /**
114     * Get raw search object provided to constructor.
115     *
116     * @return Results
117     */
118    public function getRawResults(): Results
119    {
120        return $this->raw;
121    }
122
123    /**
124     * Get minified version of search.
125     *
126     * @return Minified
127     */
128    public function getMinified(): Minified
129    {
130        return $this->minified;
131    }
132
133    /**
134     * Get normalized version of search object.
135     *
136     * @return Results
137     */
138    public function getNormalizedResults(): Results
139    {
140        return $this->normalized;
141    }
142
143    /**
144     * Get search URL from normalized search object.
145     *
146     * @return string
147     */
148    public function getUrl(): string
149    {
150        return $this->url;
151    }
152
153    /**
154     * Get checksum of normalized search URL.
155     *
156     * @return string
157     */
158    public function getChecksum(): string
159    {
160        return $this->checksum;
161    }
162
163    /**
164     * Is this search equivalent to the provided minified search?
165     *
166     * @param Minified $otherSearch Search to compare against
167     *
168     * @return bool
169     */
170    public function isEquivalentToMinifiedSearch(Minified $otherSearch): bool
171    {
172        // Deminify the other search:
173        $searchToCheck = $otherSearch->deminify($this->resultsManager);
174        // Check if classes and URLs match:
175        return $searchToCheck::class === get_class($this->raw)
176            && $this->url === $searchToCheck->getUrlQuery()->getParams();
177    }
178}