JsonLdResourceFormatter::getName()   B
last analyzed

Complexity

Conditions 6
Paths 11

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 18
cts 18
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 17
nc 11
nop 1
crap 6
1
<?php
2
3
namespace PPP\Wikidata\ValueFormatters;
4
5
use InvalidArgumentException;
6
use PPP\DataModel\JsonLdResourceNode;
7
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdDataValueFormatter;
8
use PPP\Wikidata\WikibaseResourceNode;
9
use stdClass;
10
use ValueFormatters\FormatterOptions;
11
use ValueFormatters\ValueFormatter;
12
use ValueFormatters\ValueFormatterBase;
13
use Wikibase\DataModel\Entity\EntityIdValue;
14
use Wikibase\DataModel\Snak\PropertyValueSnak;
15
16
/**
17
 * @licence AGPLv3+
18
 * @author Thomas Pellissier Tanon
19
 */
20
class JsonLdResourceFormatter extends ValueFormatterBase {
21
22
	const RDF_VALUE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#value';
23
24
	/**
25
	 * @var JsonLdDataValueFormatter
26
	 */
27
	private $jsonLdFormatter;
28
29
	/**
30
	 * @var ValueFormatter
31
	 */
32
	private $snakFormatter;
33
34
	/**
35
	 * @param JsonLdDataValueFormatter $jsonLdFormatter
36
	 * @param ValueFormatter $snakFormatter
37
	 * @param FormatterOptions $options
38
	 */
39
	public function __construct(JsonLdDataValueFormatter $jsonLdFormatter, ValueFormatter $snakFormatter, FormatterOptions $options) {
40
		$this->jsonLdFormatter = $jsonLdFormatter;
41
		$this->snakFormatter = $snakFormatter;
42
43
		parent::__construct($options);
44
	}
45
46
	/**
47
	 * @see ValueFormatter::format
48
	 */
49 5
	public function format($value) {
50 5
		if(!($value instanceof WikibaseResourceNode)) {
51
			throw new InvalidArgumentException('$value is not a WikibaseResourceNode.');
52
		}
53
54 5
		$resource = $this->buildResourceFromNode($this->jsonLdFormatter->format($value->getDataValue()));
55 5
		$resource->{'@context'} = 'http://schema.org';
56 5
		$this->addContextToResource($value, $resource);
57
58 5
		return new JsonLdResourceNode(
59 5
			$this->getName($resource),
60
			$resource
61 5
		);
62
	}
63
64 5
	private function buildResourceFromNode($node) {
65 5
		if(!property_exists($node, '@value')) {
66 3
			return $node;
67
		}
68
69 2
		$resource = new stdClass();
70 2
		$resource->{self::RDF_VALUE} = $node;
71
72 2
		if(property_exists($node, '@type')) {
73 1
			$resource->{'@type'} = $node->{'@type'};
74 1
		} else {
75 1
			$resource->{'@type'} = 'Text';
76
		}
77
78 2
		return $resource;
79
	}
80
81 5
	private function getName(stdClass $resource) {
82 5
		if(property_exists($resource, self::RDF_VALUE)) {
83 2
			return $resource->{self::RDF_VALUE}->{'@value'};
84
		}
85
86 3
		$namesByLanguage = array();
87 3
		$names = $resource->name;
88
89 3
		if(!is_array($names)) {
90 2
			$names = array($names);
91 2
		}
92
93 3
		foreach($names as $name) {
94 3
			if(is_object($name)) {
95 2
				$namesByLanguage[$name->{'@language'}] = $name->{'@value'};
96 2
			} else {
97 1
				return $name;
98
			}
99 2
		}
100
101 2
		$language = $this->getOption(self::OPT_LANG);
102 2
		if(array_key_exists($language, $namesByLanguage)) {
103 1
			return $namesByLanguage[$language];
104
		} else {
105 1
			return reset($namesByLanguage);
106
		}
107
	}
108
109 5
	private function addContextToResource(WikibaseResourceNode $resourceNode, stdClass $resource) {
110 5
		$fromPredicate = $resourceNode->getFromPredicate();
111 5
		$fromSubject = $resourceNode->getFromSubject();
112 5
		if($fromPredicate === null || $fromSubject === null) {
113 2
			return;
114
		}
115
116 3
		$formatted = $this->snakFormatter->format(new PropertyValueSnak($fromPredicate, new EntityIdValue($fromSubject)));
117
118 3
		if(empty($formatted)) {
119 1
			return;
120
		}
121
122 2
		if(!property_exists($resource, '@reverse')) {
123 1
			$resource->{'@reverse'} = new stdClass();
124 1
		}
125
126 2
		foreach($formatted as $property => $value) {
127 2
			$resource->{'@reverse'}->{$property}[] = $value;
128 2
		}
129 2
	}
130
}
131