/**
  * @see ValueFormatter::format
  *
  * @param TimeValue $value
  *
  * @throws InvalidArgumentException
  * @return string HTML
  */
 public function format($value)
 {
     if (!$value instanceof TimeValue) {
         throw new InvalidArgumentException('Data value type mismatch. Expected a TimeValue.');
     }
     $formatted = $this->dateTimeFormatter->format($value);
     if ($this->calendarNameNeeded($value)) {
         $formatted .= '<sup class="wb-calendar-name">' . $this->formatCalendarName($value->getCalendarModel()) . '</sup>';
     }
     return $formatted;
 }
 /**
  * Formats a value.
  *
  * @since 0.1
  *
  * @param mixed $value The value to format
  *
  * @return string The formatted value (as wikitext).
  * @throws FormattingException
  */
 public function format($value)
 {
     if (is_int($value) || is_float($value)) {
         return $this->valueLocalizer->localizeNumber($value);
     } elseif ($value instanceof DataValue) {
         return $this->dataValueFormatter->format($value);
     } elseif (is_object($value)) {
         return $this->formatObject($value);
     } elseif (is_array($value)) {
         return $this->formatValueList($value);
     }
     return wfEscapeWikiText(strval($value));
 }
 /**
  * Format an auto summary argument
  *
  * @since 0.4
  *
  * @param mixed $arg
  *
  * @return string
  */
 protected function formatArg($arg)
 {
     try {
         if ($arg instanceof Snak) {
             return $this->snakFormatter->formatSnak($arg);
         } elseif ($arg instanceof EntityId) {
             return $this->idFormatter->formatEntityId($arg);
         } elseif ($arg instanceof DataValue) {
             return $this->valueFormatter->format($arg);
         } elseif (method_exists($arg, '__toString')) {
             return strval($arg);
         } elseif (is_object($arg)) {
             return '<' . get_class($arg) . '>';
         } elseif (is_array($arg)) {
             if (!empty($arg) && !isset($arg[0])) {
                 // turn assoc array into a list
                 $arg = $this->formatKeyValuePairs($arg);
             }
             $strings = $this->formatArgList($arg);
             return $this->language->commaList($strings);
         } else {
             return strval($arg);
         }
     } catch (Exception $ex) {
         wfWarn(__METHOD__ . ': failed to render value: ' . $ex->getMessage());
     }
     return '?';
 }
Example #4
0
 /**
  * @since 0.6
  *
  * @param string $unit URI
  *
  * @return string|null Text
  */
 protected function formatUnit($unit)
 {
     if ($this->vocabularyUriFormatter === null || !$this->options->getOption(self::OPT_APPLY_UNIT) || $unit === '' || $unit === '1') {
         return null;
     }
     return $this->vocabularyUriFormatter->format($unit);
 }
 /**
  * @param string $unit URI
  *
  * @return string HTML
  */
 private function formatUnit($unit)
 {
     $formattedUnit = $this->vocabularyUriFormatter->format($unit);
     if ($formattedUnit === null || $formattedUnit === $unit) {
         return htmlspecialchars($unit);
     }
     return Html::element('a', array('href' => $unit), $formattedUnit);
 }
 /**
  * Calls the TypedValueFormatter passed to the constructor.
  *
  * @param DataValue $value
  * @param string|null $dataTypeId
  *
  * @throws FormattingException
  * @return string Either plain text, wikitext or HTML, depending on the ValueFormatter
  *  provided.
  */
 private function formatValue(DataValue $value, $dataTypeId = null)
 {
     if (!$this->isUnDeserializableValue($value)) {
         if ($this->valueFormatter instanceof TypedValueFormatter) {
             $text = $this->valueFormatter->formatValue($value, $dataTypeId);
         } else {
             $text = $this->valueFormatter->format($value);
         }
     } else {
         $text = '';
     }
     return $text;
 }
 /**
  * @see SnakFormatter::formatSnak
  *
  * @param Snak $snak
  *
  * @throws InvalidArgumentException
  * @throws FormattingException
  * @return string Either plain text, wikitext or HTML, depending on the SnakFormatter provided.
  */
 public function formatSnak(Snak $snak)
 {
     try {
         return $this->snakFormatter->formatSnak($snak);
     } catch (MismatchingDataValueTypeException $ex) {
         if ($ex->getDataValueType() === UnDeserializableValue::getType()) {
             $warningText = $this->formatWarning('wikibase-undeserializable-value');
         } else {
             $warningText = $this->formatWarning('wikibase-snakformatter-valuetype-mismatch', $ex->getDataValueType(), $ex->getExpectedValueType());
         }
     } catch (PropertyDataTypeLookupException $ex) {
         $warningText = $this->formatWarning('wikibase-snakformatter-property-not-found', $snak->getPropertyId()->getSerialization());
     } catch (FormattingException $ex) {
         $warningText = $this->formatWarning('wikibase-snakformatter-formatting-exception', $ex->getMessage());
     }
     if ($snak instanceof PropertyValueSnak && $this->fallbackFormatter) {
         $value = $snak->getDataValue();
         $valueText = $this->fallbackFormatter->format($value);
         if ($valueText !== '') {
             return $valueText . ' ' . $warningText;
         }
     }
     return $warningText;
 }
 /**
  * @see ValueFormatter::format
  *
  * @param mixed $value
  *
  * @return string Typically wikitext or HTML, depending on the $escapeCallback provided.
  */
 public function format($value)
 {
     $text = $this->formatter->format($value);
     $escaped = call_user_func($this->escapeCallback, $text);
     return $escaped;
 }