Passed
Push — sub-entities ( 5e526c )
by Daniel
03:27
created

HierarchicalEntityIdParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Wikibase\DataModel\Entity;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Base class for parsers for HierarchicalEntityIds.
9
 *
10
 * @license GPL-2.0+
11
 * @author Daniel Kinzler
12
 */
13
abstract class HierarchicalEntityIdParser implements EntityIdParser {
14
15
	/**
16
	 * @var EntityIdParser
17
	 */
18
	private $baseIdParser;
19
20
	/**
21
	 * HierarchicalEntityIdParser constructor.
22
	 *
23
	 * @param EntityIdParser $baseIdParser
24
	 */
25
	public function __construct( EntityIdParser $baseIdParser ) {
26
		$this->baseIdParser = $baseIdParser;
27
	}
28
29
	/**
30
	 * @param string $idSerialization
31
	 *
32
	 * @throws EntityIdParsingException
33
	 * @return ItemId
34
	 */
35
	public function parse( $idSerialization ) {
36
		try {
37
			list( $basePart, $relativePart )
38
				= HierarchicalEntityId::splitHierarchicalSerialization( $idSerialization );
39
40
			$base = $this->baseIdParser->parse( $basePart );
41
			return $this->newEntityId( $base, $relativePart );
42
		} catch ( InvalidArgumentException $ex ) {
43
			throw new EntityIdParsingException( $ex->getMessage(), 0, $ex );
44
		}
45
	}
46
47
	protected abstract function newEntityId( EntityId $base, $relativePart );
48
49
}
50