JsonLdDecimalFormatter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 27
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 7 2
A toJsonLd() 0 13 2
1
<?php
2
3
namespace PPP\Wikidata\ValueFormatters\JsonLd;
4
5
use DataValues\DecimalValue;
6
use InvalidArgumentException;
7
use stdClass;
8
use ValueFormatters\ValueFormatterBase;
9
10
/**
11
 * @licence AGPLv3+
12
 * @author Thomas Pellissier Tanon
13
 */
14
class JsonLdDecimalFormatter extends ValueFormatterBase implements JsonLdDataValueFormatter {
15
16
	/**
17
	 * @see ValueFormatter::format
18
	 */
19 2
	public function format($value) {
20 2
		if(!($value instanceof DecimalValue)) {
21
			throw new InvalidArgumentException('$value is not a DecimalValue.');
22
		}
23
24 2
		return $this->toJsonLd($value);
25
	}
26
27 2
	private function toJsonLd(DecimalValue $value) {
28 2
		$literal = new stdClass();
29
30 2
		if($value->getFractionalPart() === '') {
31 1
			$literal->{'@type'} = 'Integer';
32 1
			$literal->{'@value'} = $value->getIntegerPart();
33 1
		} else {
34 1
			$literal->{'@type'} = 'Float';
35 1
			$literal->{'@value'} = $value->getValueFloat();
36
		}
37
38 2
		return $literal;
39
	}
40
}
41