Skip to content
Snippets Groups Projects
Commit 897b65f9 authored by Demian Katz's avatar Demian Katz
Browse files

Less redundant approach to opt-in/opt-out SwitchQuery options.

parent 18f888b6
Branches
Tags
No related merge requests found
......@@ -356,11 +356,11 @@ CallNumber = callnumber-sort
; improve it. [backend] is the name of the search backend currently in use,
; which will help with accurate analysis (default = Solr). [opt-out checks
; to skip] is a comma-separated list of checks which are on by default but
; which you wish to disable; see the check*() methods in the module's code
; for a complete list of available checks. [opt-in checks to add] is a
; comma-separated list of transforms that are off by default but which you
; wish to enable; see the transform*() methods in the module's code for a
; complete list of available checks.
; which you wish to disable. [opt-in checks to add] is a comma-separated
; list of transforms that are off by default but which you wish to enable.
; See the check*() methods in the module's code for a complete list of
; available checks. The $optInMethods property specifies which checks are
; turned off by default.
; SwitchType:[field]:[field description]
; If the current search type is not the same as [field], display a link
; suggesting that the user try switching to [field]. [field description]
......
......@@ -74,13 +74,11 @@ class SwitchQuery implements RecommendInterface
protected $skipChecks = [];
/**
* Names of transforms to apply. These should correspond
* with transform method names -- e.g. to apply the transform found in the
* transformTruncateChar() method, you would put 'truncatechar' into this array.
* List of 'opt-in' methods (others are 'opt-out' by default).
*
* @var array
*/
protected $transforms = [];
protected $optInMethods = ['fuzzy', 'truncatechar'];
/**
* Search results object.
......@@ -113,10 +111,14 @@ class SwitchQuery implements RecommendInterface
$callback = function ($i) {
return trim(strtolower($i));
};
// Get a list of "opt out" preferences from the user...
$this->skipChecks = !empty($params[1])
? array_map($callback, explode(',', $params[1])) : [];
$this->transforms = !empty($params[2])
$optIns = !empty($params[2])
? explode(',', $params[2]) : [];
$this->skipChecks = array_merge(
$this->skipChecks, array_diff($this->optInMethods, $optIns)
);
}
/**
......@@ -167,27 +169,12 @@ class SwitchQuery implements RecommendInterface
if (substr($method, 0, 5) == 'check') {
$currentCheck = strtolower(substr($method, 5));
if (!in_array($currentCheck, $this->skipChecks)) {
$result = $this->$method($query);
if ($result) {
if ($result = $this->$method($query)) {
$this->suggestions['switchquery_' . $currentCheck] = $result;
}
}
}
}
// Perform all transforms (based on naming convention):
$methods = get_class_methods($this);
foreach ($methods as $method) {
if (substr($method, 0, 9) == 'transform') {
$currTrans = strtolower(substr($method, 9));
if (in_array($currTrans, $this->transforms)) {
$result = $this->$method($query);
if ($result) {
$this->suggestions['switchquery_' . $currTrans] = $result;
}
}
}
}
}
/**
......@@ -215,7 +202,7 @@ class SwitchQuery implements RecommendInterface
*
* @return string|bool
*/
protected function transformFuzzy($query)
protected function checkFuzzy($query)
{
// Don't stack tildes:
if (strpos($query, '~') !== false) {
......@@ -307,7 +294,7 @@ class SwitchQuery implements RecommendInterface
*
* @return string|bool
*/
protected function transformTruncatechar($query)
protected function checkTruncatechar($query)
{
// Don't truncate phrases:
if (substr($query, -1) == '"') {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment