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

ExtendedJsonLdEntityFormatter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 51.28%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
lcom 1
cbo 6
dl 0
loc 84
ccs 20
cts 39
cp 0.5128
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A format() 0 7 2
A toJsonLd() 0 13 3
A addFingerprintAliasesToResource() 0 13 3
B addStatementListToResource() 0 12 5
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\Statement\StatementList;
13
use Wikibase\DataModel\StatementListProvider;
14
use Wikibase\DataModel\Term\Fingerprint;
15
use Wikibase\DataModel\Term\FingerprintProvider;
16
use Wikibase\DataModel\Term\Term;
17
18
/**
19
 * @licence GPLv2+
20
 * @author Thomas Pellissier Tanon
21
 */
22
class ExtendedJsonLdEntityFormatter extends ValueFormatterBase {
23
24
	/**
25
	 * @var ValueFormatter
26
	 */
27
	private $entityFormatter;
28
29
	/**
30
	 * @var ValueFormatter
31
	 */
32
	private $snakFormatter;
33
34
	/**
35
	 * @param ValueFormatter $entityFormatter
36
	 * @param ValueFormatter $snakFormatter
37
	 * @param FormatterOptions $options
38
	 */
39 1
	public function __construct(ValueFormatter $entityFormatter, ValueFormatter $snakFormatter, FormatterOptions $options) {
40
41 1
		$this->entityFormatter = $entityFormatter;
42 1
		$this->snakFormatter = $snakFormatter;
43
44
		parent::__construct($options);
45
	}
46
47
	/**
48
	 * @see ValueFormatter::format
49
	 */
50 1
	public function format($value) {
51 1
		if(!($value instanceof EntityDocument)) {
52
			throw new InvalidArgumentException('$value is not an EntityDocument.');
53
		}
54
55
		return $this->toJsonLd($value);
56
	}
57
58 1
	private function toJsonLd(EntityDocument $entity) {
59
		$resource = $this->entityFormatter->format($entity);
60
61 1
		if($entity instanceof FingerprintProvider) {
62
			$this->addFingerprintAliasesToResource($entity->getFingerprint(), $resource);
63
		}
64
65 1
		if($entity instanceof StatementListProvider) {
66
			$this->addStatementListToResource($entity->getStatements(), $resource);
67
		}
68
69 1
		return $resource;
70 1
	}
71
72 1
	private function addFingerprintAliasesToResource(Fingerprint $fingerprint, stdClass $resource) {
73
		$languageCode = $this->getOption(ValueFormatter::OPT_LANG);
74
75
		try {
76
			$aliasGroup = $fingerprint->getAliasGroup($languageCode);
77 1
			$resource->alternateName = array();
78 1
			foreach($aliasGroup->getAliases() as $alias) {
79
				$resource->alternateName[] = $this->newResourceFromTerm(new Term($aliasGroup->getLanguageCode(), $alias));
80
			}
81
		} catch(OutOfBoundsException $e) {
82
			//Just ignore it
83 1
		}
84
	}
85
86 1
	private function addStatementListToResource(StatementList $statementList, stdClass $resource) {
87
		foreach($statementList->getBestStatements()->getMainSnaks() as $snak) {
88
			$formatted = $this->snakFormatter->format($snak);
89
90
			foreach($formatted as $property => $value) {
91 1
				if(isset($resource->{$property}) && !is_array($resource->{$property})) {
92
					continue; //We do not allow to edit special properties
93
				}
94 1
				$resource->{$property}[] = $value;
95
			}
96
		}
97
	}
98
99 1
	private function newResourceFromTerm(Term $term) {
100
		$literal = new stdClass();
101
		$literal->{'@language'} = $term->getLanguageCode();
102
		$literal->{'@value'} = $term->getText();
103 1
		return $literal;
104 1
	}
105
}
106