Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
3 / 4
CRAP
83.33% covered (warning)
83.33%
5 / 6
FunctionScoreDecorator
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
3 / 4
4.07
83.33% covered (warning)
83.33%
5 / 6
 addFunction
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 isEmptyFunction
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getSize
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 _getBaseName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
namespace CirrusSearch\Search\Rescore;
use Elastica\Query\AbstractQuery;
use Elastica\Query\FunctionScore;
/**
 * This is useful to check if the function score is empty
 * Function score builders may not add any function if some
 * criteria are not met. If there's no function we should not
 * not build the rescore query.
 * @todo: find another pattern to deal with this problem and avoid
 * this strong dependency to FunctionScore::addFunction signature.
 */
class FunctionScoreDecorator extends FunctionScore {
    /** @var int */
    private $size = 0;
    /**
     * @param string $functionType
     * @param array|float $functionParams
     * @param AbstractQuery|null $filter
     * @param float|null $weight
     * @return self
     */
    public function addFunction( $functionType, $functionParams, AbstractQuery $filter = null,
        $weight = null
    ) {
        $this->size ++;
        parent::addFunction( $functionType, $functionParams, $filter, $weight );
        return $this;
    }
    /**
     * @return bool true if this function score is empty
     */
    public function isEmptyFunction() {
        return $this->size == 0;
    }
    /**
     * @return int the number of added functions.
     */
    public function getSize() {
        return $this->size;
    }
    /**
     * Default elastica behaviour is to use class name
     * as property name. We must override this function
     * to force the name to function_score
     *
     * @return string
     */
    protected function _getBaseName() {
        return "function_score";
    }
}