ValueFormatter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 46
rs 10
c 0
b 0
f 0
ccs 0
cts 14
cp 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A format() 0 14 2
1
<?php
2
3
namespace Wikibase\Api\Service;
4
5
use DataValues\DataValue;
6
use Mediawiki\Api\MediawikiApi;
7
use Mediawiki\Api\SimpleRequest;
8
use Serializers\Serializer;
9
use Wikibase\Api\GenericOptions;
10
11
/**
12
 * @access private
13
 *
14
 * @author Addshore
15
 */
16
class ValueFormatter {
17
18
	/**
19
	 * @var MediawikiApi
20
	 */
21
	private $api;
22
23
	/**
24
	 * @var Serializer
25
	 */
26
	private $dataValueSerializer;
27
28
	/**
29
	 * @param MediawikiApi $api
30
	 * @param Serializer $dataValueSerializer
31
	 */
32
	public function __construct( MediawikiApi $api, Serializer $dataValueSerializer ) {
33
		$this->api = $api;
34
		$this->dataValueSerializer = $dataValueSerializer;
35
	}
36
37
	/**
38
	 * @since 0.2
39
	 *
40
	 * @param DataValue $value
41
	 * @param string $dataTypeId
42
	 * @param GenericOptions $options
43
	 *
44
	 * @returns string
45
	 */
46
	public function format( DataValue $value, $dataTypeId, GenericOptions $options = null ) {
47
		if( $options === null ) {
48
			$options = new GenericOptions();
49
		}
50
51
		$params = array(
52
			'datavalue' => json_encode( $this->dataValueSerializer->serialize( $value ) ),
53
			'datatype' => $dataTypeId,
54
			'options' => json_encode( $options->getOptions() ),
55
		);
56
57
		$result = $this->api->getRequest( new SimpleRequest( 'wbformatvalue', $params ) );
58
		return $result['result'];
59
	}
60
61
}