|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Trait to add facet prefix and matches settings to a Params object. |
| 4 | + * |
| 5 | + * PHP version 7 |
| 6 | + * |
| 7 | + * Copyright (C) Villanova University 2018. |
| 8 | + * |
| 9 | + * This program is free software; you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU General Public License version 2, |
| 11 | + * as published by the Free Software Foundation. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with this program; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | + * |
| 22 | + * @category VuFind |
| 23 | + * @package Search |
| 24 | + * @author Demian Katz <demian.katz@villanova.edu> |
| 25 | + * @author Hajo Seng <hajo.seng@sub.uni-hamburg.de> |
| 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 | +namespace FacetPrefix\Search\Params; |
| 30 | + |
| 31 | +use Zend\Config\Config; |
| 32 | + |
| 33 | +/** |
| 34 | + * Trait to add facet limiting settings to a Params object. |
| 35 | + * |
| 36 | + * @category VuFind |
| 37 | + * @package Search |
| 38 | + * @author Demian Katz <demian.katz@villanova.edu> |
| 39 | + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
| 40 | + * @link https://vufind.org/wiki/development:plugins:record_drivers Wiki |
| 41 | + */ |
| 42 | +trait FacetRestrictionsTrait |
| 43 | +{ |
| 44 | + /** |
| 45 | + * Per-field facet prefix |
| 46 | + * |
| 47 | + * @var array |
| 48 | + */ |
| 49 | + protected $facetPrefixByField = []; |
| 50 | + |
| 51 | + /** |
| 52 | + * Per-field facet matches |
| 53 | + * |
| 54 | + * @var array |
| 55 | + */ |
| 56 | + protected $facetMatchesByField = []; |
| 57 | + |
| 58 | + /** |
| 59 | + * Initialize facet prefix and matches from a Config object. |
| 60 | + * |
| 61 | + * @param Config $config Configuration |
| 62 | + * |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + protected function initFacetRestrictionsFromConfig(Config $config = null) |
| 66 | + { |
| 67 | + foreach ($config->facet_prefix_by_field ?? [] as $k => $v) { |
| 68 | + $this->facetPrefixByField[$k] = $v; |
| 69 | + } |
| 70 | + foreach ($config->facet_matches_by_field ?? [] as $k => $v) { |
| 71 | + $this->facetMatchesByField[$k] = $v; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Set Facet Prefix by Field |
| 77 | + * |
| 78 | + * @param array $new Associative array of $field name => $limit |
| 79 | + * |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + public function setFacetPrefixByField(array $new) |
| 83 | + { |
| 84 | + $this->facetPrefixByField = $new; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Set Facet Matches by Field |
| 89 | + * |
| 90 | + * @param array $new Associative array of $field name => $limit |
| 91 | + * |
| 92 | + * @return void |
| 93 | + */ |
| 94 | + public function setFacetMatchesByField(array $new) |
| 95 | + { |
| 96 | + $this->facetMatchesByField = $new; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get the facet prefix for the specified field. |
| 101 | + * |
| 102 | + * @param string $field Field to look up |
| 103 | + * |
| 104 | + * @return string |
| 105 | + */ |
| 106 | + protected function getFacetPrefixForField($field) |
| 107 | + { |
| 108 | + $prefix = $this->facetPrefixByField[$field] ?? ''; |
| 109 | + return $prefix; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get the facet matches for the specified field. |
| 114 | + * |
| 115 | + * @param string $field Field to look up |
| 116 | + * |
| 117 | + * @return string |
| 118 | + */ |
| 119 | + protected function getFacetMatchesForField($field) |
| 120 | + { |
| 121 | + $matches = $this->facetMatchesByField[$field] ?? ''; |
| 122 | + return $matches; |
| 123 | + } |
| 124 | +} |
0 commit comments