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

HierarchicalEntityIdParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 37
c 0
b 0
f 0
wmc 3
lcom 1
cbo 3
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parse() 0 11 2
newEntityId() 0 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