LegacyFingerprintDeserializer   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 0
loc 76
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLabels() 0 9 2
A getDescriptions() 0 9 2
A getAliases() 0 11 3
A getArrayFromKey() 0 15 3
A deserialize() 0 18 3
1
<?php
2
3
namespace Wikibase\InternalSerialization\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Deserializers\Exceptions\InvalidAttributeException;
8
use InvalidArgumentException;
9
use Wikibase\DataModel\Entity\Item;
10
use Wikibase\DataModel\Term\AliasGroup;
11
use Wikibase\DataModel\Term\AliasGroupList;
12
use Wikibase\DataModel\Term\Fingerprint;
13
use Wikibase\DataModel\Term\Term;
14
use Wikibase\DataModel\Term\TermList;
15
16
/**
17
 * @license GPL-2.0-or-later
18
 * @author Jeroen De Dauw < [email protected] >
19
 * @author Bene* < [email protected] >
20
 */
21
class LegacyFingerprintDeserializer implements Deserializer {
22
23
	/**
24
	 * @param array $serialization
25
	 *
26
	 * @return Item
27
	 * @throws DeserializationException
28
	 */
29 11
	public function deserialize( $serialization ) {
30 11
		if ( !is_array( $serialization ) ) {
31 1
			throw new DeserializationException( 'Term serialization should be an array' );
32
		}
33
34
		try {
35 10
			return new Fingerprint(
36 10
				$this->getLabels( $serialization ),
37 8
				$this->getDescriptions( $serialization ),
38 7
				$this->getAliases( $serialization )
39
			);
40 4
		} catch ( InvalidArgumentException $ex ) {
41 1
			throw new DeserializationException(
42 1
				'Could not deserialize fingerprint: ' . $ex->getMessage(),
43
				$ex
44
			);
45
		}
46
	}
47
48 10
	private function getLabels( array $serialization ) {
49 10
		$labels = array();
50
51 10
		foreach ( $this->getArrayFromKey( 'label', $serialization ) as $langCode => $text ) {
52 2
			$labels[] = new Term( $langCode, $text );
53
		}
54
55 8
		return new TermList( $labels );
56
	}
57
58 8
	private function getDescriptions( array $serialization ) {
59 8
		$descriptions = array();
60
61 8
		foreach ( $this->getArrayFromKey( 'description', $serialization ) as $langCode => $text ) {
62 1
			$descriptions[] = new Term( $langCode, $text );
63
		}
64
65 7
		return new TermList( $descriptions );
66
	}
67
68 7
	private function getAliases( array $serialization ) {
69 7
		$descriptions = array();
70
71 7
		foreach ( $this->getArrayFromKey( 'aliases', $serialization ) as $langCode => $texts ) {
72 1
			if ( $texts !== array() ) {
73 1
				$descriptions[] = new AliasGroup( $langCode, $texts );
74
			}
75
		}
76
77 6
		return new AliasGroupList( $descriptions );
78
	}
79
80 10
	private function getArrayFromKey( $key, array $serialization ) {
81 10
		if ( !array_key_exists( $key, $serialization ) ) {
82 8
			return array();
83
		}
84
85 10
		if ( !is_array( $serialization[$key] ) ) {
86 3
			throw new InvalidAttributeException(
87 3
				$key,
88 3
				$serialization[$key],
89 3
				'The ' . $key . ' key should point to an array'
90
			);
91
		}
92
93 7
		return $serialization[$key];
94
	}
95
96
}
97