Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
CRAP
92.00% covered (success)
92.00%
23 / 25
TermSearchApiInteractor
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
11.06
92.00% covered (success)
92.00%
23 / 25
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 searchForEntities
0.00% covered (danger)
0.00%
0 / 1
3.00
92.31% covered (success)
92.31%
12 / 13
 parseDatum
0.00% covered (danger)
0.00%
0 / 1
7.07
88.89% covered (warning)
88.89%
8 / 9
<?php
namespace ArticlePlaceholder;
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\DataModel\Term\Term;
use Wikibase\Lib\Interactors\TermSearchInteractor;
use Wikibase\Lib\Interactors\TermSearchResult;
/**
 * Repository's API-based term search interactor
 *
 * @license GPL-2.0-or-later
 */
class TermSearchApiInteractor implements TermSearchInteractor {
    /**
     * @var RepoApiInteractor
     */
    private $repoApiInteractor;
    /**
     * @var EntityIdParser
     */
    private $entityIdParser;
    /**
     * @param RepoApiInteractor $repoApiInteractor
     * @param EntityIdParser $entityIdParser
     */
    public function __construct(
        RepoApiInteractor $repoApiInteractor,
        EntityIdParser $entityIdParser
    ) {
        $this->repoApiInteractor = $repoApiInteractor;
        $this->entityIdParser = $entityIdParser;
    }
    /**
     * @param string $text Term text to search for
     * @param string $languageCode Language code to search in
     * @param string $entityType Type of Entity to return
     * @param string[] $termTypes Types of Term to return, array of Wikibase\Lib\TermIndexEntry::TYPE_*
     *
     * @return TermSearchResult[]
     */
    public function searchForEntities( $text, $languageCode, $entityType, array $termTypes ) {
        $params = [
            'action' => 'wbsearchentities',
            'language' => $languageCode,
            'strictlanguage' => 1,
            'search' => $text,
            'format' => 'json',
            'type' => $entityType
        ];
        $data = $this->repoApiInteractor->request( $params );
        if ( !isset( $data['search'] ) ) {
            return [];
        }
        $result = [];
        foreach ( $data['search'] as $datum ) {
            $result[] = $this->parseDatum( $datum, $languageCode );
        }
        return array_filter( $result );
    }
    /**
     * @param array $datum
     * @param string $languageCode
     * @return null|TermSearchResult
     */
    private function parseDatum( array $datum, $languageCode ) {
        if ( !isset( $datum['title'] ) || !isset( $datum['match'] ) ||
            !isset( $datum['match']['text'] ) || !isset( $datum['match']['type'] )
        ) {
            return null;
        }
        return new TermSearchResult(
            new Term( $languageCode, $datum['match']['text'] ),
            $datum['match']['type'],
            $this->entityIdParser->parse( $datum['title'] ),
            isset( $datum['label'] ) ? new Term( $languageCode, $datum['label'] ) : null,
            isset( $datum['description'] ) ? new Term( $languageCode, $datum['description'] ) : null
        );
    }
}