Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
83.33% covered (warning)
83.33%
5 / 6
CRAP
96.00% covered (success)
96.00%
24 / 25
FTQueryClassifiersRepository
0.00% covered (danger)
0.00%
0 / 1
83.33% covered (warning)
83.33%
5 / 6
10
96.00% covered (success)
96.00%
24 / 25
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 registerClassifier
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
7 / 7
 registerClassifierAsCallable
0.00% covered (danger)
0.00%
0 / 1
1.00
87.50% covered (warning)
87.50%
7 / 8
 getConfig
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getClassifier
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 getKnownClassifiers
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
namespace CirrusSearch\Parser;
use CirrusSearch\CirrusSearchHookRunner;
use CirrusSearch\Parser\AST\ParsedQuery;
use CirrusSearch\SearchConfig;
use Wikimedia\Assert\Assert;
/**
 * Repository of query classifiers
 */
class FTQueryClassifiersRepository implements ParsedQueryClassifiersRepository {
    /**
     * @var ParsedQueryClassifier[]
     */
    private $classifiers = [];
    /**
     * @var SearchConfig
     */
    private $config;
    /**
     * @var bool
     */
    private $frozen = false;
    /**
     * @param SearchConfig $config
     * @param CirrusSearchHookRunner $cirrusSearchHookRunner
     * @throws ParsedQueryClassifierException
     */
    public function __construct( SearchConfig $config, CirrusSearchHookRunner $cirrusSearchHookRunner ) {
        $this->config = $config;
        $this->registerClassifier( new BasicQueryClassifier() );
        $cirrusSearchHookRunner->onCirrusSearchRegisterFullTextQueryClassifiers( $this );
        $this->frozen = true;
    }
    /**
     * @param ParsedQueryClassifier $classifier
     * @throws ParsedQueryClassifierException
     */
    public function registerClassifier( ParsedQueryClassifier $classifier ) {
        if ( $this->frozen ) {
            throw new ParsedQueryClassifierException( 'Repository is frozen' );
        }
        foreach ( $classifier->classes() as $class ) {
            if ( array_key_exists( $class, $this->classifiers ) ) {
                throw new ParsedQueryClassifierException( "Classifier with $class already registered" );
            }
            $this->classifiers[$class] = $classifier;
        }
    }
    /**
     * @param string[] $classes list of $classes that this classifier can produce
     * @param callable $callable called as ParsedQueryClassifier::classify( ParsedQuery $query )
     * @throws ParsedQueryClassifierException
     * @see ParsedQueryClassifier::classify()
     */
    public function registerClassifierAsCallable( array $classes, callable $callable ) {
        Assert::parameter( $classes !== [], '$classes', 'A classifier must support at least one class' );
        $factory = new class( $classes, $callable ) implements ParsedQueryClassifier {
            /**
             * @var string[]
             */
            private $classes;
            /**
             * @var callable
             */
            private $callable;
            /**
             * @param string[] $classes
             * @param callable $callable
             */
            public function __construct( $classes, callable $callable ) {
                $this->classes = $classes;
                $this->callable = $callable;
            }
            /**
             * @param ParsedQuery $query
             * @return string[]
             */
            public function classify( ParsedQuery $query ) {
                return ( $this->callable )( $query );
            }
            /**
             * @return string[]
             */
            public function classes() {
                return $this->classes;
            }
        };
        $this->registerClassifier( $factory );
    }
    /**
     * The host wiki SearchConfig
     * @return SearchConfig
     */
    public function getConfig() {
        return $this->config;
    }
    /**
     * @param string $name
     * @return ParsedQueryClassifier
     * @throws ParsedQueryClassifierException
     */
    public function getClassifier( $name ) {
        if ( array_key_exists( $name, $this->classifiers ) ) {
            return $this->classifiers[$name];
        }
        throw new ParsedQueryClassifierException( "Classifier $name not found" );
    }
    /**
     * List known classifiers
     * @return string[]
     */
    public function getKnownClassifiers() {
        return array_keys( $this->classifiers );
    }
}