Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SortFacetList
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __invoke
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 facetValueToString
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Sort facet list view helper
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  View_Helpers
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\View\Helper\Root;
31
32use Laminas\View\Helper\AbstractHelper;
33
34/**
35 * Sort facet list view helper
36 *
37 * @category VuFind
38 * @package  View_Helpers
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development Wiki
42 */
43class SortFacetList extends AbstractHelper implements
44    \VuFind\I18n\HasSorterInterface
45{
46    use \VuFind\I18n\HasSorterTrait;
47
48    /**
49     * Turns facet information into an alphabetical list.
50     *
51     * @param \VuFind\Search\Base\Results $results      Search result object
52     * @param string                      $field        Facet field to sort
53     * @param array                       $list         Facet value list extract from
54     * the search result object's getFacetList method
55     * @param array                       $searchRoute  Route to use to generate
56     * search URLs for individual facet values
57     * @param string                      $formatString String for formatting facet
58     * values (e.g. with %%displayText%% and %%count%% tokens)
59     *
60     * @return array      Associative URL => description array sorted by description
61     */
62    public function __invoke($results, $field, $list, $searchRoute, $formatString = '%%displayText%%')
63    {
64        $facets = [];
65        // avoid limit on URL
66        $results->getParams()->setLimit($results->getOptions()->getDefaultLimit());
67        $urlHelper = $this->getView()->plugin('url');
68        foreach ($list as $value) {
69            $url = $urlHelper($searchRoute) . $results->getUrlQuery()
70                ->addFacet($field, $value['value'], ($value['operator'] ?? 'AND'))
71                ->getParams();
72            $facets[$url] = $this->facetValueToString($value, $formatString);
73        }
74        $this->getSorter()->natsort($facets);
75        return $facets;
76    }
77
78    /**
79     * Convert a facet array to a string.
80     *
81     * @param array  $values Facet value array
82     * @param string $format Format string
83     *
84     * @return string
85     */
86    protected function facetValueToString(array $values, string $format): string
87    {
88        $search = $replace = [];
89        foreach ($values as $key => $value) {
90            $search[] = "%%$key%%";
91            $replace[] = $value;
92        }
93        return str_replace($search, $replace, $format);
94    }
95}