Completed
Pull Request — master (#123)
by Valentin
07:18
created

WikibaseValueParserFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 31.58%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 3
c 3
b 0
f 1
lcom 1
cbo 6
dl 0
loc 50
ccs 6
cts 19
cp 0.3158
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A newWikibaseValueParser() 0 13 1
A newWikibaseEntityParser() 0 11 1
1
<?php
2
3
namespace PPP\Wikidata\ValueParsers;
4
5
use DataValues\Geo\Parsers\GlobeCoordinateParser;
6
use ValueParsers\ParserOptions;
7
use ValueParsers\ValueParser;
8
use Wikibase\EntityStore\EntityStore;
9
10
/**
11
 * Build a parser for Wikibase value
12
 *
13
 * @licence GPLv2+
14
 * @author Thomas Pellissier Tanon
15
 */
16
class WikibaseValueParserFactory {
17
18
	/**
19
	 * @var string language code
20
	 */
21
	private $languageCode;
22
23
	/**
24
	 * @var EntityStore
25
	 */
26
	private $entityStore;
27
28
	/**
29
	 * @param $languageCode
30
	 * @param EntityStore $entityStore
31
	 */
32 7
	public function __construct($languageCode, EntityStore $entityStore) {
33 7
		$this->languageCode = $languageCode;
34 7
		$this->entityStore = $entityStore;
35
	}
36
37
	/**
38
	 * @return WikibaseValueParser
39
	 */
40 7
	public function newWikibaseValueParser() {
41
		return new WikibaseValueParser(array(
42
			'commonsMedia' => new StringParser(),
43
			'globe-coordinate' => new GlobeCoordinateParser(),
44
			//TODO 'quantity' => ,
45
			'monolingualtext' => new MonolingualTextParser(),
46
			'string' => new StringParser(),
47
			//TODO 'time' => ,
48
			'url' => new StringParser(),
49 7
			'wikibase-item' => $this->newWikibaseEntityParser('item'),
50 7
			'wikibase-property' => $this->newWikibaseEntityParser('property')
51
		));
52
	}
53
54
	private function newWikibaseEntityParser($type) {
55
		$parserOptions = new ParserOptions(array(
56
			ValueParser::OPT_LANG => $this->languageCode,
57
			WikibaseEntityIdParser::OPT_ENTITY_TYPE => $type
58
		));
59
60
		return new WikibaseEntityIdParser(
61
			$this->entityStore,
62
			$parserOptions
63
		);
64
	}
65
}
66