Completed
Push — master ( 2a3272...f39d74 )
by Thomas
08:55
created

JsonLdEntityFormatter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 57.69%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 63
ccs 15
cts 26
cp 0.5769
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A format() 0 7 2
A toJsonLd() 0 12 2
A addFingerprintToResource() 0 15 3
A newResourceFromTerm() 0 6 1
1
<?php
2
3
namespace PPP\Wikidata\ValueFormatters\JsonLd\Entity;
4
5
use InvalidArgumentException;
6
use OutOfBoundsException;
7
use stdClass;
8
use ValueFormatters\FormatterOptions;
9
use ValueFormatters\ValueFormatter;
10
use ValueFormatters\ValueFormatterBase;
11
use Wikibase\DataModel\Entity\EntityDocument;
12
use Wikibase\DataModel\Term\Fingerprint;
13
use Wikibase\DataModel\Term\FingerprintProvider;
14
use Wikibase\DataModel\Term\Term;
15
16
/**
17
 * @licence GPLv2+
18
 * @author Thomas Pellissier Tanon
19
 */
20
class JsonLdEntityFormatter extends ValueFormatterBase {
21
22
	/**
23
	 * Base URI for Wikibase entities. For Wikidata it is "http://www.wikidata.org/entity/"
24
	 */
25
	const OPT_ENTITY_BASE_URI = 'entity-baseuri';
26
27
	/**
28
	 * @param FormatterOptions $options
29
	 */
30
	public function __construct(FormatterOptions $options) {
31
		parent::__construct($options);
32
33
		$this->requireOption(self::OPT_ENTITY_BASE_URI);
34
	}
35
36
	/**
37
	 * @see ValueFormatter::format
38
	 */
39 4
	public function format($value) {
40 4
		if(!($value instanceof EntityDocument)) {
41
			throw new InvalidArgumentException('$value is not an EntityDocument.');
42
		}
43
44
		return $this->toJsonLd($value);
45
	}
46
47 4
	private function toJsonLd(EntityDocument $entity) {
48
		$resource = new stdClass();
49 4
		$resource->{'@type'} = 'Thing';
50
		$resource->name = $entity->getId()->getSerialization();
51
		$resource->{'@id'} = $this->getOption(JsonLdEntityFormatter::OPT_ENTITY_BASE_URI) . $entity->getId()->getSerialization();
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
52
53 4
		if($entity instanceof FingerprintProvider) {
54
			$this->addFingerprintToResource($entity->getFingerprint(), $resource);
55
		}
56
57 4
		return $resource;
58 4
	}
59
60 4
	private function addFingerprintToResource(Fingerprint $fingerprint, stdClass $resource) {
61
		$languageCode = $this->getOption(ValueFormatter::OPT_LANG);
62
63
		try {
64 1
			$resource->name = $this->newResourceFromTerm($fingerprint->getLabel($languageCode));
65
		} catch(OutOfBoundsException $e) {
66
			//Just ignore it
67 4
		}
68
69
		try {
70 3
			$resource->description = $this->newResourceFromTerm($fingerprint->getDescription($languageCode));
71
		} catch(OutOfBoundsException $e) {
72
			//Just ignore it
73 4
		}
74
	}
75
76 2
	private function newResourceFromTerm(Term $term) {
77
		$literal = new stdClass();
78
		$literal->{'@language'} = $term->getLanguageCode();
79
		$literal->{'@value'} = $term->getText();
80 2
		return $literal;
81 2
	}
82
}
83