private function newLanguageLabelDescriptionLookup(FormatterOptions $options) { $languageCode = $options->getOption(ValueFormatter::OPT_LANG); if (!is_string($languageCode)) { throw new InvalidArgumentException('ValueFormatter::OPT_LANG must be a ' . 'language code string.'); } return new LanguageLabelDescriptionLookup($this->termLookup, $languageCode); }
public function testDefaultOption() { $options = array('foo' => 42, 'bar' => 4.2, 'baz' => array('o_O', false, null, '42' => 42, array())); $formatterOptions = new FormatterOptions($options); foreach ($options as $option => $value) { $formatterOptions->defaultOption($option, 9001); $this->assertEquals(serialize($value), serialize($formatterOptions->getOption($option)), 'Defaulting a set option should not affect its value'); } $defaults = array('N' => 42, 'y' => 4.2, 'a' => false, 'n' => array('42' => 42, array(''))); foreach ($defaults as $option => $value) { $formatterOptions->defaultOption($option, $value); $this->assertEquals(serialize($value), serialize($formatterOptions->getOption($option)), 'Defaulting a not set option should affect its value'); } }
/** * @param FormatterOptions $options * * @return MediaWikiNumberLocalizer */ private function getNumberLocalizer(FormatterOptions $options) { $language = Language::factory($options->getOption(ValueFormatter::OPT_LANG)); return new MediaWikiNumberLocalizer($language); }
/** * Initializes the options keys ValueFormatter::OPT_LANG and * FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN if they are not yet set. * * @param FormatterOptions $options The options to modify. * * @throws InvalidArgumentException * @todo : Sort out how the desired language is specified. We have two language options, * each accepting different ways of specifying the language. That's not good. * @todo: this shouldn't be public at all. Perhaps factor it out into a helper class. */ public function applyLanguageDefaults(FormatterOptions $options) { $options->defaultOption(ValueFormatter::OPT_LANG, $this->defaultLanguage->getCode()); $lang = $options->getOption(ValueFormatter::OPT_LANG); if (!is_string($lang)) { throw new InvalidArgumentException('The value of OPT_LANG must be a language code. For a fallback chain, use OPT_LANGUAGE_FALLBACK_CHAIN.'); } $fallbackOption = FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN; if (!$options->hasOption($fallbackOption)) { $fallbackMode = LanguageFallbackChainFactory::FALLBACK_ALL; $options->setOption($fallbackOption, $this->languageFallbackChainFactory->newFromLanguageCode($lang, $fallbackMode)); } if (!$options->getOption($fallbackOption) instanceof LanguageFallbackChain) { throw new InvalidArgumentException('The value of OPT_LANGUAGE_FALLBACK_CHAIN must be ' . 'an instance of LanguageFallbackChain.'); } }
private function assertFormatsCorrectly(LatLongValue $latLong, FormatterOptions $options, $expected) { $formatter = new GeoCoordinateFormatter($options); $this->assertEquals($expected, $formatter->format($latLong), 'format()'); $precision = $options->getOption(GeoCoordinateFormatter::OPT_PRECISION); $this->assertEquals($expected, $formatter->formatLatLongValue($latLong, $precision), 'formatLatLongValue()'); }