Completed
Push — master ( 7880aa...3f359c )
by Thomas
05:14
created

JsonLdPropertyFormatter::toJsonLd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1.008
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
		parent::__construct($options);
30
	}
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
		return $this->toJsonLd($value);
41
	}
42
43 1
	private function toJsonLd(Property $item) {
44
		$resource = $this->entityFormatter->format($item);
45 1
		$resource->{'@type'} = 'Property';
46 1
		return $resource;
47 1
	}
48
}
49