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

WikibaseValueParserFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 4
cp 0.75
rs 10
cc 1
eloc 3
nc 1
nop 2
crap 1.0156
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