Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FunctionScoreBuilder
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getOverriddenFactor
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2
3namespace CirrusSearch\Search\Rescore;
4
5use CirrusSearch\SearchConfig;
6
7/**
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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 along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 */
23
24abstract class FunctionScoreBuilder implements BoostFunctionBuilder {
25    /**
26     * @var SearchConfig
27     */
28    protected $config;
29
30    /**
31     * @var float global weight of this function score builder
32     */
33    protected $weight;
34
35    /**
36     * @param SearchConfig $config the search config
37     * @param float $weight the global weight
38     */
39    public function __construct( $config, $weight ) {
40        $this->config = $config;
41        $this->weight = $this->getOverriddenFactor( $weight );
42    }
43
44    /**
45     * Utility method to extract a factor (float) that can
46     * be overridden by a config value or an URI param
47     *
48     * @param float|array $value
49     * @return float
50     */
51    protected function getOverriddenFactor( $value ) {
52        if ( is_array( $value ) ) {
53            $returnValue = (float)$value['value'];
54
55            if ( isset( $value['config_override'] ) ) {
56                // Override factor with config
57                $fromConfig = $this->config->get( $value['config_override'] );
58                if ( $fromConfig !== null ) {
59                    $returnValue = (float)$fromConfig;
60                }
61            }
62
63            if ( isset( $value['uri_param_override'] ) ) {
64                // Override factor with uri param
65                $uriParam = $value['uri_param_override'];
66                $request = \RequestContext::getMain()->getRequest();
67                if ( $request ) {
68                    $fromUri = $request->getVal( $uriParam );
69                    if ( $fromUri !== null && is_numeric( $fromUri ) ) {
70                        $returnValue = (float)$fromUri;
71                    }
72                }
73            }
74            return $returnValue;
75        } else {
76            return (float)$value;
77        }
78    }
79}