Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
64.29% covered (warning)
64.29%
9 / 14
16.67% covered (danger)
16.67%
1 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
FacetLimitTrait
64.29% covered (warning)
64.29%
9 / 14
16.67% covered (danger)
16.67%
1 / 6
14.56
0.00% covered (danger)
0.00%
0 / 1
 initFacetLimitsFromConfig
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 setFacetLimit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFacetLimitByField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHierarchicalFacetLimit
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setHierarchicalFacetLimit
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFacetLimitForField
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
1<?php
2
3/**
4 * Trait to add facet limiting settings to a Params object.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2018.
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:plugins:record_drivers Wiki
28 */
29
30namespace VuFind\Search\Params;
31
32use Laminas\Config\Config;
33
34use function in_array;
35
36/**
37 * Trait to add facet limiting settings to a Params object.
38 *
39 * @category VuFind
40 * @package  Search
41 * @author   Demian Katz <demian.katz@villanova.edu>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development:plugins:record_drivers Wiki
44 */
45trait FacetLimitTrait
46{
47    /**
48     * Default facet result limit
49     *
50     * @var int
51     */
52    protected $facetLimit = 30;
53
54    /**
55     * Per-field facet result limit
56     *
57     * @var array
58     */
59    protected $facetLimitByField = [];
60
61    /**
62     * Hierarchical facet limit when facets are requested.
63     *
64     * -1 = unlimited
65     *
66     * @var int
67     */
68    protected $hierarchicalFacetLimit = -1;
69
70    /**
71     * Initialize facet limit from a Config object.
72     *
73     * @param Config $config Configuration
74     *
75     * @return void
76     */
77    protected function initFacetLimitsFromConfig(Config $config = null)
78    {
79        if (is_numeric($config->facet_limit ?? null)) {
80            $this->setFacetLimit($config->facet_limit);
81        }
82        foreach ($config->facet_limit_by_field ?? [] as $k => $v) {
83            $this->facetLimitByField[$k] = $v;
84        }
85    }
86
87    /**
88     * Set Facet Limit
89     *
90     * @param int $l the new limit value
91     *
92     * @return void
93     */
94    public function setFacetLimit($l)
95    {
96        $this->facetLimit = $l;
97    }
98
99    /**
100     * Set Facet Limit by Field
101     *
102     * @param array $new Associative array of $field name => $limit
103     *
104     * @return void
105     */
106    public function setFacetLimitByField(array $new)
107    {
108        $this->facetLimitByField = $new;
109    }
110
111    /**
112     * Get current limit for hierarchical facets
113     *
114     * @return int
115     */
116    public function getHierarchicalFacetLimit()
117    {
118        return $this->hierarchicalFacetLimit;
119    }
120
121    /**
122     * Set limit for hierarchical facets
123     *
124     * @param int $limit New limit
125     *
126     * @return void
127     */
128    public function setHierarchicalFacetLimit($limit)
129    {
130        $this->hierarchicalFacetLimit = $limit;
131    }
132
133    /**
134     * Get the facet limit for the specified field.
135     *
136     * @param string $field Field to look up
137     *
138     * @return int
139     */
140    protected function getFacetLimitForField($field)
141    {
142        $limit = $this->facetLimitByField[$field] ?? $this->facetLimit;
143
144        // Check for a different limit for hierarchical facets:
145        if ($limit !== $this->hierarchicalFacetLimit) {
146            $hierarchicalFacets = $this->getOptions()->getHierarchicalFacets();
147            if (in_array($field, $hierarchicalFacets)) {
148                $limit = $this->hierarchicalFacetLimit;
149            }
150        }
151
152        return $limit;
153    }
154}