| 1 | <?php |
||
| 16 | class ValueFormatter { |
||
| 17 | |||
| 18 | private MediawikiApi $api; |
||
|
|
|||
| 19 | |||
| 20 | private \Serializers\Serializer $dataValueSerializer; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param MediawikiApi $api |
||
| 24 | * @param Serializer $dataValueSerializer |
||
| 25 | */ |
||
| 26 | public function __construct( MediawikiApi $api, Serializer $dataValueSerializer ) { |
||
| 27 | $this->api = $api; |
||
| 28 | $this->dataValueSerializer = $dataValueSerializer; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @since 0.2 |
||
| 33 | * |
||
| 34 | * @param GenericOptions|null $options |
||
| 35 | * |
||
| 36 | */ |
||
| 37 | public function format( DataValue $value, string $dataTypeId, GenericOptions $options = null ): string { |
||
| 38 | if ( $options === null ) { |
||
| 39 | $options = new GenericOptions(); |
||
| 40 | } |
||
| 41 | |||
| 42 | $params = [ |
||
| 43 | 'datavalue' => json_encode( $this->dataValueSerializer->serialize( $value ) ), |
||
| 44 | 'datatype' => $dataTypeId, |
||
| 45 | 'options' => json_encode( $options->getOptions() ), |
||
| 46 | ]; |
||
| 47 | |||
| 48 | $result = $this->api->getRequest( new SimpleRequest( 'wbformatvalue', $params ) ); |
||
| 49 | return $result['result']; |
||
| 50 | } |
||
| 51 | |||
| 52 | } |
||
| 53 |