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);
 }
 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);
 }
 /**
  * @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);
 }
 /**
  * Returns an SnakFormatter for rendering snaks in the desired format
  * using the given options.
  *
  * @param string $format Use the SnakFormatter::FORMAT_XXX constants.
  * @param FormatterOptions $options
  *
  * @throws RuntimeException
  * @throws InvalidArgumentException
  * @return SnakFormatter
  */
 public function getSnakFormatter($format, FormatterOptions $options)
 {
     $options->defaultOption(SnakFormatter::OPT_ON_ERROR, SnakFormatter::ON_ERROR_WARN);
     $this->valueFormatterFactory->applyLanguageDefaults($options);
     $lang = $options->getOption(ValueFormatter::OPT_LANG);
     $noValueSnakFormatter = new MessageSnakFormatter('novalue', $this->getMessage('wikibase-snakview-snaktypeselector-novalue', $lang), $format);
     $someValueSnakFormatter = new MessageSnakFormatter('somevalue', $this->getMessage('wikibase-snakview-snaktypeselector-somevalue', $lang), $format);
     $valueFormatter = $this->valueFormatterFactory->getValueFormatter($format, $options);
     $valueSnakFormatter = new PropertyValueSnakFormatter($format, $options, $valueFormatter, $this->propertyDataTypeLookup, $this->dataTypeFactory);
     $formattersBySnakType = array('novalue' => $noValueSnakFormatter, 'somevalue' => $someValueSnakFormatter);
     $formattersByDataType = $this->createSnakFormatters($format, $options);
     // Register default formatter for the special '*' key.
     $formattersByDataType['*'] = $valueSnakFormatter;
     $snakFormatter = new DispatchingSnakFormatter($format, $this->propertyDataTypeLookup, $formattersBySnakType, $formattersByDataType);
     if ($options->getOption(SnakFormatter::OPT_ON_ERROR) === SnakFormatter::ON_ERROR_WARN) {
         $snakFormatter = new ErrorHandlingSnakFormatter($snakFormatter, $valueFormatter, $lang);
     }
     return $snakFormatter;
 }
 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');
     }
 }
Esempio n. 7
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', '&lt;b&gt;+2&lt;/b&gt;'), 'Disabled with unit' => array($noUnit, '<b>m</b>', '&lt;b&gt;+2&lt;/b&gt;'), 'Default without unit' => array(null, '1', '&lt;b&gt;+2&lt;/b&gt;'), 'Default with unit' => array(null, '<b>m</b>', '&lt;b&gt;+2&lt;/b&gt; <span class="wb-unit">&lt;b&gt;m&lt;/b&gt;</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 .*?>(\\[\\[|&#91;&#91;)Hallo(&amp;|&#38;)Welt(\\]\\]|&#93;&#93;)<\\/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'));
 }