Completed
Push — master ( 7d7b0c...47c96c )
by Leszek
39s
created

EntityIdDeserializer::assertEntityIdIsString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Wikibase\DataModel\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Wikibase\DataModel\Entity\EntityId;
8
use Wikibase\DataModel\Entity\EntityIdParser;
9
use Wikibase\DataModel\Entity\EntityIdParsingException;
10
11
/**
12
 * Package private
13
 *
14
 * @license GPL-2.0+
15
 * @author Thomas Pellissier Tanon
16
 */
17
class EntityIdDeserializer implements Deserializer {
18
19
	/**
20
	 * @var EntityIdParser
21
	 */
22
	private $entityIdParser;
23
24
	/**
25
	 * @param EntityIdParser $entityIdParser
26
	 */
27
	public function __construct( EntityIdParser $entityIdParser ) {
28
		$this->entityIdParser = $entityIdParser;
29
	}
30
31
	/**
32
	 * @see Deserializer::deserialize
33
	 *
34
	 * @param string $serialization
35
	 *
36
	 * @throws DeserializationException
37
	 * @return EntityId
38
	 */
39
	public function deserialize( $serialization ) {
40
		if ( !is_string( $serialization ) ) {
41
			throw new DeserializationException( 'The serialization of an entity ID should be a string' );
42
		}
43
44
		try {
45
			return $this->entityIdParser->parse( $serialization );
46
		} catch ( EntityIdParsingException $e ) {
47
			throw new DeserializationException( "'$serialization' is not a valid entity ID", $e );
48
		}
49
	}
50
51
}
52