Completed
Push — main ( b55b92...e8d442 )
by
unknown
05:41
created

ValueFormatter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 46
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Addwiki\Wikibase\Api\Service;
4
5
use Addwiki\Mediawiki\Api\Client\MediawikiApi;
6
use Addwiki\Mediawiki\Api\Client\SimpleRequest;
7
use Addwiki\Wikibase\Api\GenericOptions;
8
use DataValues\DataValue;
9
use Serializers\Serializer;
10
11
/**
12
 * @access private
13
 *
14
 * @author Addshore
15
 */
16
class ValueFormatter {
17
18
	private MediawikiApi $api;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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