Wikibase
MediaWiki Wikibase extension
Loading...
Searching...
No Matches
Wikibase\Lib\Formatters\OutputFormatValueFormatterFactory Class Reference

Factory for ValueFormatters, based on factory callbacks. More...

Public Member Functions

 __construct (array $factoryFunctions, Language $defaultLanguage, LanguageFallbackChainFactory $fallbackChainFactory)
 
 setFormatterFactoryCallback (string $type, ?callable $factoryFunction)
 
 applyLanguageDefaults (FormatterOptions $options)
 Initializes the options keys ValueFormatter::OPT_LANG and FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN if they are not yet set.
 
 getValueFormatter ( $format, FormatterOptions $options)
 Returns a ValueFormatter for the given format, suitable for formatting DataValues of any any type supported by the formatter factory functions supplied to the constructor.
 

Private Member Functions

 buildDefinedFormatters ( $format, FormatterOptions $options)
 Instantiates the formatters defined for the given format in WikibaseValueFormatterBuilders::$valueFormatterSpecs.
 

Private Attributes

 $factoryFunctions
 
 $defaultLanguage
 
 $languageFallbackChainFactory
 

Detailed Description

Factory for ValueFormatters, based on factory callbacks.

This class provides a mapping between factory callbacks organized by data type to ValueFormatters by target type. This reflects the fact that formatters for a single data type are typically defined by code that has knowledge about that specific data type, while ValueFormatters are typically used by code that doesn't know anything about specific data types, but requires a specific output format.

This class implements a fallback mechanism for target formats, that allows some target formats to stand in for others (with escaping applies if necessary). E.g. if there is not HTML formatter defined for a data type, the plain text formatter plus HTML escaping would be used.

Author
Daniel Kinzler

Constructor & Destructor Documentation

◆ __construct()

Wikibase\Lib\Formatters\OutputFormatValueFormatterFactory::__construct ( array $factoryFunctions,
Language $defaultLanguage,
LanguageFallbackChainFactory $fallbackChainFactory )
Parameters
callable[]$factoryFunctionsAn associative array mapping types to factory functions. Type names must use the "PT:" prefix for property types (data types), and "VT:" for value types, to be compatible with the convention used by DispatchingValueFormatter. The factory functions will be called with two parameters, the desired target type (see the SnakFormatter::FORMAT_XXX constants) and a FormatterOptions object. The factory function must return an instance of ValueFormatter suitable for the given target format, or null if no formatter for the requested target format is known.
Language$defaultLanguage
LanguageFallbackChainFactory$fallbackChainFactory

Member Function Documentation

◆ applyLanguageDefaults()

Wikibase\Lib\Formatters\OutputFormatValueFormatterFactory::applyLanguageDefaults ( FormatterOptions $options)

Initializes the options keys ValueFormatter::OPT_LANG and FormatterLabelDescriptionLookupFactory::OPT_LANGUAGE_FALLBACK_CHAIN if they are not yet set.

Parameters
FormatterOptions$optionsThe options to modify.
Exceptions
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.

this shouldn't be public at all. Perhaps factor it out into a helper class.

◆ buildDefinedFormatters()

Wikibase\Lib\Formatters\OutputFormatValueFormatterFactory::buildDefinedFormatters ( $format,
FormatterOptions $options )
private

Instantiates the formatters defined for the given format in WikibaseValueFormatterBuilders::$valueFormatterSpecs.

See also
WikibaseValueFormatterBuilders::$valueFormatterSpecs
Parameters
string$formatOne of the SnakFormatter::FORMAT_... constants.
FormatterOptions$options
Returns
callable[] A map from prefixed type IDs to ValueFormatter factories.

◆ getValueFormatter()

Wikibase\Lib\Formatters\OutputFormatValueFormatterFactory::getValueFormatter ( $format,
FormatterOptions $options )

Returns a ValueFormatter for the given format, suitable for formatting DataValues of any any type supported by the formatter factory functions supplied to the constructor.

Parameters
string$format
FormatterOptions$options
Exceptions
InvalidArgumentException
Returns
DispatchingValueFormatter

◆ setFormatterFactoryCallback()

Wikibase\Lib\Formatters\OutputFormatValueFormatterFactory::setFormatterFactoryCallback ( string $type,
?callable $factoryFunction )
Parameters
string$typeThe data type or value type to register the formatter factory for. Use the "PT:" prefix for data types and "VT:" for value types.
callable | null$factoryFunctionThe factory method. Will be called with two parameters, a string indicating the desired target format, and a FormatterOptions object. The callback must return a ValueFormatter suitable for emitting the given output format, or null.

Member Data Documentation

◆ $defaultLanguage

Wikibase\Lib\Formatters\OutputFormatValueFormatterFactory::$defaultLanguage
private

◆ $factoryFunctions

Wikibase\Lib\Formatters\OutputFormatValueFormatterFactory::$factoryFunctions
private

◆ $languageFallbackChainFactory

Wikibase\Lib\Formatters\OutputFormatValueFormatterFactory::$languageFallbackChainFactory
private

The documentation for this class was generated from the following file: