EntityOntology::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PPP\Wikidata\ValueFormatters\JsonLd\Entity;
4
5
use DataValues\StringValue;
6
use OutOfBoundsException;
7
use Wikibase\DataModel\Entity\Item;
8
use Wikibase\DataModel\Entity\Property;
9
use Wikibase\DataModel\Entity\PropertyId;
10
use Wikibase\DataModel\Snak\PropertyValueSnak;
11
use Wikibase\DataModel\Statement\StatementListProvider;
12
13
/**
14
 * @licence AGPLv3+
15
 * @author Thomas Pellissier Tanon
16
 */
17
class EntityOntology {
18
19
	const OWL_EQUIVALENT_PROPERTY = 'http://www.w3.org/2002/07/owl#equivalentProperty';
20
21
	const QUDT_SYMBOL = 'http://qudt.org/schema/qudt#symbol';
22
23
	/**
24
	 * @var PropertyId[]
25
	 */
26
	private $propertyForIris;
27
28
	/**
29
	 * @param PropertyId[] $propertyForIris
30
	 */
31 4
	public function __construct(array $propertyForIris) {
32 4
		$this->propertyForIris = $propertyForIris;
33 4
	}
34
35
	/**
36
	 * @param Property $property
37
	 * @return string[]
38
	 */
39 2
	public function getEquivalentPropertiesIris(Property $property) {
40 2
		return $this->getTextualContentForIri($property, self::OWL_EQUIVALENT_PROPERTY);
41
	}
42
43
	/**
44
	 * @param Item $item
45
	 * @return string
46
	 * @throws OutOfBoundsException
47
	 */
48 2
	public function getUnitSymbol(Item $item) {
49 2
		$symbols = $this->getTextualContentForIri($item, self::QUDT_SYMBOL);
50
51 2
		if(empty($symbols)) {
52 1
			throw new OutOfBoundsException();
53
		} else {
54 1
			return reset($symbols);
55
		}
56
	}
57
58 4
	private function getTextualContentForIri(StatementListProvider $statementListProvider, $iri) {
59 4
		if(!array_key_exists($iri, $this->propertyForIris)) {
60 2
			return array();
61
		}
62
63 2
		return $this->getTextualContentForProperty($statementListProvider, $this->propertyForIris[$iri]);
64
	}
65
66 2
	private function getTextualContentForProperty(StatementListProvider $statementListProvider, PropertyId $propertyId) {
67 2
		$snaks = $statementListProvider->getStatements()->getByPropertyId($propertyId)->getBestStatements()->getMainSnaks();
68
69 2
		$iris = array();
70 2
		foreach($snaks as $snak) {
71 2
			if($snak instanceof PropertyValueSnak) {
72 2
				$dataValue = $snak->getDataValue();
73
74 2
				if($dataValue instanceof StringValue) {
75 2
					$iris[] = $snak->getDataValue()->getValue();
76 2
				}
77 2
			}
78 2
		}
79
80 2
		return $iris;
81
	}
82
}
83