Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.83% covered (warning)
70.83%
17 / 24
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Minified
70.83% covered (warning)
70.83%
17 / 24
66.67% covered (warning)
66.67%
2 / 3
17.19
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
 deminify
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 populateClassNames
63.16% covered (warning)
63.16%
12 / 19
0.00% covered (danger)
0.00%
0 / 1
17.05
1<?php
2
3/**
4 * VuFind Minified Search Object
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   Demian Katz <demian.katz@villanova.edu>
26 * @author   Juha Luoma <juha.luoma@helsinki.fi>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development Wiki
29 */
30
31namespace VuFind\Search;
32
33use VuFind\Search\Base\Results;
34
35/**
36 * A minified search object used exclusively for trimming a search object down to its
37 * barest minimum size before storage in a cookie or database.
38 *
39 * It still contains enough data granularity to programmatically recreate search
40 * URLs.
41 *
42 * This class isn't intended for general use, but simply a way of storing/retrieving
43 * data from a search object:
44 *
45 * eg. Store
46 * $searchHistory[] = serialize($this->minify());
47 *
48 * eg. Retrieve
49 * $searchObject = unserialize($search);
50 * $searchObject->deminify($manager);
51 *
52 * @category VuFind
53 * @package  Search
54 * @author   Demian Katz <demian.katz@villanova.edu>
55 * @author   Juha Luoma <juha.luoma@helsinki.fi>
56 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
57 * @link     https://vufind.org/wiki/development Wiki
58 */
59class Minified
60{
61    /**
62     * Search terms
63     *
64     * @var array
65     */
66    public $t = [];
67
68    /**
69     * Filters
70     *
71     * @var array
72     */
73    public $f = [];
74
75    /**
76     * Hidden Filters
77     *
78     * @var array
79     */
80    public $hf = [];
81
82    /**
83     * Search ID
84     *
85     * @var int
86     */
87    public $id;
88
89    /**
90     * Search start time
91     *
92     * @var float
93     */
94    public $i;
95
96    /**
97     * Search duration
98     *
99     * @var float
100     */
101    public $s;
102
103    /**
104     * Total result count
105     *
106     * @var int
107     */
108    public $r;
109
110    /**
111     * Search type
112     *
113     * @var string
114     */
115    public $ty;
116
117    /**
118     * Search class
119     *
120     * @var string
121     */
122    public $cl;
123
124    /**
125     * Extra data (not used by default)
126     *
127     * @var array
128     */
129    public $ex = [];
130
131    /**
132     * Extra params data (not used by default)
133     *
134     * @var array
135     */
136    public $exp = [];
137
138    /**
139     * Search context parameters
140     *
141     * @var array
142     */
143    public $scp = [];
144
145    /**
146     * Constructor.
147     *
148     * Builds minified object from the Results passed in.
149     *
150     * @param Results $results Results object to minify
151     */
152    public function __construct(Results $results)
153    {
154        $results->minify($this);
155    }
156
157    /**
158     * Turn the current object into search results.
159     *
160     * @param \VuFind\Search\Results\PluginManager $manager Search manager
161     *
162     * @return Results
163     */
164    public function deminify(\VuFind\Search\Results\PluginManager $manager): Results
165    {
166        // Figure out the parameter and result classes based on the search class ID:
167        $this->populateClassNames();
168
169        // Deminify everything:
170        $results = $manager->get($this->cl);
171        $results->deminify($this);
172
173        return $results;
174    }
175
176    /**
177     * Support method for deminify -- populate parameter class and results class
178     * if missing (for legacy compatibility).
179     *
180     * @return void
181     */
182    protected function populateClassNames(): void
183    {
184        // If this is a legacy entry from VuFind 1.x, we need to figure out the
185        // search class ID for the object we're about to construct:
186        if (!isset($this->cl)) {
187            $fixType = true;    // by default, assume we need to fix type
188            switch ($this->ty) {
189                case 'Summon':
190                case 'SummonAdvanced':
191                    $this->cl = 'Summon';
192                    break;
193                case 'WorldCat':
194                case 'WorldCatAdvanced':
195                    $this->cl = 'WorldCat';
196                    break;
197                case 'Authority':
198                case 'AuthorityAdvanced':
199                    $this->cl = 'SolrAuth';
200                    break;
201                default:
202                    $this->cl = 'Solr';
203                    $fixType = false;
204                    break;
205            }
206
207            // Now rewrite the type if necessary (only needed for legacy objects):
208            if ($fixType) {
209                $this->ty = str_ends_with($this->ty, 'Advanced') ? 'advanced' : 'basic';
210            }
211        }
212    }
213}