Current File : /home/jvzmxxx/wiki/extensions/Wikibase/lib/includes/Store/EntityInfoTermLookup.php
<?php

namespace Wikibase\Lib\Store;

use OutOfBoundsException;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Services\Lookup\TermLookup;
use Wikibase\DataModel\Services\Lookup\TermLookupException;

/**
 * TermLookup based on plain array data structures.
 * This allows term lookups to be performed directly on prefetched data,
 * such as the data structured generated by EntityInfoBuilder.
 *
 * @see EntityInfoBuilder
 *
 * @since 0.5
 *
 * @license GPL-2.0+
 * @author Daniel Kinzler
 */
class EntityInfoTermLookup implements TermLookup {

	/**
	 * @var EntityInfo
	 */
	private $entityInfo;

	/**
	 * @param EntityInfo $entityInfo
	 */
	public function __construct( EntityInfo $entityInfo ) {
		$this->entityInfo = $entityInfo;
	}

	/**
	 * Gets the label of an Entity with the specified EntityId and language code.
	 *
	 * @param EntityId $entityId
	 * @param string $languageCode
	 *
	 * @throws TermLookupException
	 * @return string|null
	 */
	public function getLabel( EntityId $entityId, $languageCode ) {
		try {
			return $this->entityInfo->getLabel( $entityId, $languageCode );
		} catch ( OutOfBoundsException $ex ) {
			throw new TermLookupException(
				$entityId,
				array( $languageCode ),
				$ex->getMessage(),
				$ex
			);
		}
	}

	/**
	 * Gets all labels of an Entity with the specified EntityId.
	 *
	 * @param EntityId $entityId
	 * @param string[] $languageCodes
	 *
	 * @throws TermLookupException
	 * @return string[]
	 */
	public function getLabels( EntityId $entityId, array $languageCodes ) {
		try {
			return $this->entityInfo->getLabels( $entityId, $languageCodes );
		} catch ( OutOfBoundsException $ex ) {
			throw new TermLookupException( $entityId, $languageCodes, $ex->getMessage(), $ex );
		}
	}

	/**
	 * Gets the description of an Entity with the specified EntityId and language code.
	 *
	 * @param EntityId $entityId
	 * @param string $languageCode
	 *
	 * @throws TermLookupException
	 * @return string|null
	 */
	public function getDescription( EntityId $entityId, $languageCode ) {
		try {
			return $this->entityInfo->getDescription( $entityId, $languageCode );
		} catch ( OutOfBoundsException $ex ) {
			throw new TermLookupException(
				$entityId,
				array( $languageCode ),
				$ex->getMessage(),
				$ex
			);
		}
	}

	/**
	 * Gets all descriptions of an Entity with the specified EntityId.
	 *
	 * @param EntityId $entityId
	 * @param string[] $languageCodes
	 *
	 * @throws TermLookupException
	 * @return string[]
	 */
	public function getDescriptions( EntityId $entityId, array $languageCodes ) {
		try {
			return $this->entityInfo->getDescriptions( $entityId, $languageCodes );
		} catch ( OutOfBoundsException $ex ) {
			throw new TermLookupException( $entityId, $languageCodes, $ex->getMessage(), $ex );
		}
	}

}