JsonLdItemFormatter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 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\Item;
10
11
/**
12
 * @licence AGPLv3+
13
 * @author Thomas Pellissier Tanon
14
 * @todo output sitelinks
15
 */
16
class JsonLdItemFormatter extends ValueFormatterBase {
17
18
	/**
19
	 * @var ValueFormatter
20
	 */
21
	private $entityFormatter;
22
23
	/**
24
	 * @param ValueFormatter $entityFormatter
25
	 * @param FormatterOptions $options
26
	 */
27 1
	public function __construct(ValueFormatter $entityFormatter, FormatterOptions $options) {
28 1
		$this->entityFormatter = $entityFormatter;
29
30 1
		parent::__construct($options);
31 1
	}
32
33
	/**
34
	 * @see ValueFormatter::format
35
	 */
36 1
	public function format($value) {
37 1
		if(!($value instanceof Item)) {
38
			throw new InvalidArgumentException('$value is not an Item.');
39
		}
40
41 1
		return $this->toJsonLd($value);
42
	}
43
44 1
	private function toJsonLd(Item $item) {
45 1
		$resource = $this->entityFormatter->format($item);
46 1
		return $resource;
47
	}
48
}
49