public function testFormatWithInvalidPrecision_fallsBackToDefaultPrecision() { $options = new FormatterOptions(); $options->setOption(GeoCoordinateFormatter::OPT_PRECISION, 0); $formatter = new GlobeCoordinateFormatter($options); $formatted = $formatter->format(new GlobeCoordinateValue(new LatLongValue(1.2, 3.4), null)); $this->assertEquals('1.2, 3.4', $formatted); }
/** * @return HtmlTimeFormatter */ private function getFormatter() { $options = new FormatterOptions(); $options->setOption(ValueFormatter::OPT_LANG, 'qqx'); $dateTimeFormatter = $this->getMock('ValueFormatters\\ValueFormatter'); $dateTimeFormatter->expects($this->any())->method('format')->will($this->returnValue('MOCKDATE')); return new HtmlTimeFormatter($options, $dateTimeFormatter); }
/** * @param string $formattedHeading * * @return TimeDetailsFormatter */ private function getFormatter($formattedHeading = '') { $options = new FormatterOptions(); $options->setOption(ValueFormatter::OPT_LANG, 'qqx'); $timeFormatter = $this->getMock('ValueFormatters\\ValueFormatter'); $timeFormatter->expects($this->any())->method('format')->will($this->returnValue($formattedHeading)); return new TimeDetailsFormatter($options, $timeFormatter); }
public function testSetOption() { $formatterOptions = new FormatterOptions(array('foo' => 'bar')); $values = array(array('foo', 'baz'), array('foo', 'bar'), array('onoez', ''), array('hax', 'zor'), array('nyan', 9001), array('cat', 4.2), array('spam', array('~=[,,_,,]:3'))); foreach ($values as $value) { $formatterOptions->setOption($value[0], $value[1]); $this->assertEquals($value[1], $formatterOptions->getOption($value[0])); } }
/** * @param string|null $optionsParam * * @return FormatterOptions */ private function getOptionsObject($optionsParam) { $formatterOptions = new FormatterOptions(); $formatterOptions->setOption(ValueFormatter::OPT_LANG, $this->getLanguage()->getCode()); if (is_string($optionsParam) && $optionsParam !== '') { $options = json_decode($optionsParam, true); if (is_array($options)) { foreach ($options as $name => $value) { $formatterOptions->setOption($name, $value); } } } return $formatterOptions; }
/** * 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.'); } }
public function applyUnitOptionProvider() { $noUnit = new FormatterOptions(); $noUnit->setOption(QuantityHtmlFormatter::OPT_APPLY_UNIT, false); return array('Disabled without unit' => array($noUnit, '1', '<b>+2</b>'), 'Disabled with unit' => array($noUnit, '<b>m</b>', '<b>+2</b>'), 'Default without unit' => array(null, '1', '<b>+2</b>'), 'Default with unit' => array(null, '<b>m</b>', '<b>+2</b> <span class="wb-unit"><b>m</b></span>')); }
/** * Returns a ValueFormatter like the one that should be used internally for generating * summaries. * * @return ValueFormatter */ protected function getPropertyValueFormatter() { if (!$this->propertyValueFormatter) { $idFormatter = $this->getEntityIdFormatter(); $options = new FormatterOptions(); $options->setOption('formatter-builders-text/plain', array('VT:wikibase-entityid' => function () use($idFormatter) { return new EntityIdValueFormatter($idFormatter); })); $factory = WikibaseRepo::getDefaultInstance()->getValueFormatterFactory(); $this->propertyValueFormatter = $factory->getValueFormatter(SnakFormatter::FORMAT_PLAIN, $options); } return $this->propertyValueFormatter; }
/** * @param string $format The desired target format, see SnakFormatter::FORMAT_XXX * @param FormatterOptions $options * * @return GlobeCoordinateFormatter */ public function newGlobeCoordinateFormatter($format, FormatterOptions $options) { if ($format === SnakFormatter::FORMAT_HTML_DIFF) { return new GlobeCoordinateDetailsFormatter($options); } else { $options->setOption(GeoCoordinateFormatter::OPT_FORMAT, GeoCoordinateFormatter::TYPE_DMS); $options->setOption(GeoCoordinateFormatter::OPT_SPACING_LEVEL, array(GeoCoordinateFormatter::OPT_SPACE_LATLONG)); $options->setOption(GeoCoordinateFormatter::OPT_DIRECTIONAL, true); $plainFormatter = new GlobeCoordinateFormatter($options); return $this->escapeValueFormatter($format, $plainFormatter); } }
public function monolingualHtmlFormatProvider() { $options = new FormatterOptions(); $options->setOption(ValueFormatter::OPT_LANG, 'en'); return array('formatting' => array(new MonolingualTextValue('de', 'Hallo Welt'), $options, '@^<span lang="de".*?>Hallo Welt<\\/span>.*\\Deutsch.*$@'), 'html/wikitext escaping' => array(new MonolingualTextValue('de', '[[Hallo&Welt]]'), $options, '@^<span .*?>(\\[\\[|[[)Hallo(&|&)Welt(\\]\\]|]])<\\/span>.*$@'), 'evil html' => array(new MonolingualTextValue('" onclick="alert(\'gotcha!\')"', 'Hallo<script>alert(\'gotcha!\')</script>Welt' . '<a href="javascript:alert(\'gotcha!\')">evil</a>'), $options, '@ onclick="alert|<script|<a @', 'not')); }