Completed
Push — master ( ac7e0a...7880aa )
by Thomas
04:53
created

JsonLdPropertyFormatter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 34
ccs 11
cts 12
cp 0.9167
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A format() 0 7 2
A toJsonLd() 0 5 1
1
<?php
2
3
namespace PPP\Wikidata\ValueFormatters\JsonLd\Entity;
4
5
use InvalidArgumentException;
6
use ValueFormatters\FormatterOptions;
7
use ValueFormatters\ValueFormatter;
8
use ValueFormatters\ValueFormatterBase;
9
use Wikibase\DataModel\Entity\Property;
10
11
/**
12
 * @licence GPLv2+
13
 * @author Thomas Pellissier Tanon
14
 */
15
class JsonLdPropertyFormatter extends ValueFormatterBase {
16
17
	/**
18
	 * @var ValueFormatter
19
	 */
20
	private $entityFormatter;
21
22
	/**
23
	 * @param ValueFormatter $entityFormatter
24
	 * @param FormatterOptions $options
25
	 */
26 1
	public function __construct(ValueFormatter $entityFormatter, FormatterOptions $options) {
27 1
		$this->entityFormatter = $entityFormatter;
28
29 1
		parent::__construct($options);
30 1
	}
31
32
	/**
33
	 * @see ValueFormatter::format
34
	 */
35 1
	public function format($value) {
36 1
		if(!($value instanceof Property)) {
37
			throw new InvalidArgumentException('$value is not an Property.');
38
		}
39
40 1
		return $this->toJsonLd($value);
41
	}
42
43 1
	private function toJsonLd(Property $item) {
44 1
		$resource = $this->entityFormatter->format($item);
45 1
		$resource->{'@type'} = 'Property';
46 1
		return $resource;
47
	}
48
}
49