Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| LemmaLanguageField | |
100.00% |
26 / 26 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
| constructOptions | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| validate | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Wikibase\Lexeme\MediaWiki\Specials\HTMLForm; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | use MediaWiki\Context\RequestContext; |
| 7 | use MediaWiki\HTMLForm\Field\HTMLComboboxField; |
| 8 | use Wikibase\Lexeme\WikibaseLexemeServices; |
| 9 | use Wikibase\Lib\ContentLanguages; |
| 10 | use Wikibase\Lib\LanguageNameLookup; |
| 11 | use Wikibase\Repo\WikibaseRepo; |
| 12 | |
| 13 | /** |
| 14 | * Class representing lexeme lemma language selector field |
| 15 | * |
| 16 | * @license GPL-2.0-or-later |
| 17 | */ |
| 18 | class LemmaLanguageField extends HTMLComboboxField { |
| 19 | |
| 20 | /** |
| 21 | * @inheritDoc |
| 22 | * |
| 23 | * @see \HTMLForm There is detailed description of the allowed $params (named $info there). |
| 24 | */ |
| 25 | public function __construct( array $params ) { |
| 26 | if ( isset( $params['options'] ) |
| 27 | || isset( $params['options-message'] ) |
| 28 | || isset( $params['options-messages'] ) |
| 29 | ) { |
| 30 | throw new InvalidArgumentException( |
| 31 | "Cannot set options for content language field. It already has it's own options" |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | $params['options'] = $this->constructOptions( |
| 36 | WikibaseLexemeServices::getTermLanguages(), |
| 37 | WikibaseRepo::getLanguageNameLookupFactory() |
| 38 | ->getForLanguage( RequestContext::getMain()->getLanguage() ) |
| 39 | ); |
| 40 | |
| 41 | parent::__construct( $params ); |
| 42 | } |
| 43 | |
| 44 | private function constructOptions( |
| 45 | ContentLanguages $contentLanguages, |
| 46 | LanguageNameLookup $lookup |
| 47 | ): array { |
| 48 | $languageOptions = []; |
| 49 | |
| 50 | foreach ( $contentLanguages->getLanguages() as $code ) { |
| 51 | $option = $this->msg( |
| 52 | 'wikibase-lexeme-lemma-language-option', |
| 53 | [ |
| 54 | $lookup->getName( $code ), |
| 55 | $code, |
| 56 | ] |
| 57 | )->plain(); |
| 58 | $languageOptions[$option] = $code; |
| 59 | } |
| 60 | |
| 61 | return $languageOptions; |
| 62 | } |
| 63 | |
| 64 | /** @inheritDoc */ |
| 65 | public function validate( $value, $alldata ) { |
| 66 | if ( !in_array( $value, $this->getOptions(), true ) ) { |
| 67 | return $this->msg( 'wikibase-lexeme-lemma-language-not-recognized' ); |
| 68 | } |
| 69 | |
| 70 | return parent::validate( $value, $alldata ); |
| 71 | } |
| 72 | |
| 73 | } |