PropertyDeserializer   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 112
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A deserialize() 0 5 1
A getDeserialized() 0 12 1
A setIdFromSerialization() 0 9 2
A setTermsFromSerialization() 0 22 4
A setStatementListFromSerialization() 0 9 2
1
<?php
2
3
namespace Wikibase\DataModel\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Deserializers\TypedObjectDeserializer;
8
use Wikibase\DataModel\Entity\Property;
9
use Wikibase\DataModel\Entity\PropertyId;
10
use Wikibase\DataModel\Statement\StatementList;
11
use Wikibase\DataModel\Term\AliasGroupList;
12
use Wikibase\DataModel\Term\TermList;
13
14
/**
15
 * Package private
16
 *
17
 * @license GPL-2.0-or-later
18
 * @author Thomas Pellissier Tanon
19
 * @author Bene* < [email protected] >
20
 */
21
class PropertyDeserializer extends TypedObjectDeserializer {
22
23
	/**
24
	 * @var Deserializer
25
	 */
26
	private $entityIdDeserializer;
27
28
	/**
29
	 * @var Deserializer
30
	 */
31
	private $termListDeserializer;
32
33
	/**
34
	 * @var Deserializer
35
	 */
36
	private $aliasGroupListDeserializer;
37
38
	/**
39
	 * @var Deserializer
40
	 */
41
	private $statementListDeserializer;
42
43 25
	public function __construct(
44
		Deserializer $entityIdDeserializer,
45
		Deserializer $termListDeserializer,
46
		Deserializer $aliasGroupListDeserializer,
47
		Deserializer $statementListDeserializer
48
	) {
49 25
		parent::__construct( 'property', 'type' );
50
51 25
		$this->entityIdDeserializer = $entityIdDeserializer;
52 25
		$this->termListDeserializer = $termListDeserializer;
53 25
		$this->aliasGroupListDeserializer = $aliasGroupListDeserializer;
54 25
		$this->statementListDeserializer = $statementListDeserializer;
55 25
	}
56
57
	/**
58
	 * @see Deserializer::deserialize
59
	 *
60
	 * @param array $serialization
61
	 *
62
	 * @throws DeserializationException
63
	 * @return Property
64
	 */
65 15
	public function deserialize( $serialization ) {
66 15
		$this->assertCanDeserialize( $serialization );
67
68 12
		return $this->getDeserialized( $serialization );
69
	}
70
71
	/**
72
	 * @param array $serialization
73
	 *
74
	 * @return Property
75
	 */
76 12
	private function getDeserialized( array $serialization ) {
77 12
		$this->requireAttribute( $serialization, 'datatype' );
0 ignored issues
show
Deprecated Code introduced by
The method Deserializers\TypedObjec...zer::requireAttribute() has been deprecated with message: since 4.0, just do your own "if ( array_key_exists( … ) )" or "if ( isset( … ) )" instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
78 12
		$this->assertAttributeInternalType( $serialization, 'datatype', 'string' );
0 ignored issues
show
Deprecated Code introduced by
The method Deserializers\TypedObjec...AttributeInternalType() has been deprecated with message: since 4.0, just do your own "if ( is_string( … ) )" and such instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
79
80 12
		$property = Property::newFromType( $serialization['datatype'] );
81
82 12
		$this->setIdFromSerialization( $serialization, $property );
83 12
		$this->setTermsFromSerialization( $serialization, $property );
84 12
		$this->setStatementListFromSerialization( $serialization, $property );
85
86 12
		return $property;
87
	}
88
89 12
	private function setIdFromSerialization( array $serialization, Property $property ) {
90 12
		if ( !array_key_exists( 'id', $serialization ) ) {
91 8
			return;
92
		}
93
94
		/** @var PropertyId $id */
95 4
		$id = $this->entityIdDeserializer->deserialize( $serialization['id'] );
96 4
		$property->setId( $id );
97 4
	}
98
99 12
	private function setTermsFromSerialization( array $serialization, Property $property ) {
100 12
		if ( array_key_exists( 'labels', $serialization ) ) {
101 4
			$this->assertAttributeIsArray( $serialization, 'labels' );
0 ignored issues
show
Deprecated Code introduced by
The method Deserializers\TypedObjec...ssertAttributeIsArray() has been deprecated with message: since 4.0, just do your own "if ( is_array( … ) )" instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
102
			/** @var TermList $labels */
103 4
			$labels = $this->termListDeserializer->deserialize( $serialization['labels'] );
104 4
			$property->getFingerprint()->setLabels( $labels );
105
		}
106
107 12
		if ( array_key_exists( 'descriptions', $serialization ) ) {
108 4
			$this->assertAttributeIsArray( $serialization, 'descriptions' );
0 ignored issues
show
Deprecated Code introduced by
The method Deserializers\TypedObjec...ssertAttributeIsArray() has been deprecated with message: since 4.0, just do your own "if ( is_array( … ) )" instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
109
			/** @var TermList $descriptions */
110 4
			$descriptions = $this->termListDeserializer->deserialize( $serialization['descriptions'] );
111 4
			$property->getFingerprint()->setDescriptions( $descriptions );
112
		}
113
114 12
		if ( array_key_exists( 'aliases', $serialization ) ) {
115 4
			$this->assertAttributeIsArray( $serialization, 'aliases' );
0 ignored issues
show
Deprecated Code introduced by
The method Deserializers\TypedObjec...ssertAttributeIsArray() has been deprecated with message: since 4.0, just do your own "if ( is_array( … ) )" instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
116
			/** @var AliasGroupList $aliases */
117 4
			$aliases = $this->aliasGroupListDeserializer->deserialize( $serialization['aliases'] );
118 4
			$property->getFingerprint()->setAliasGroups( $aliases );
119
		}
120 12
	}
121
122 12
	private function setStatementListFromSerialization( array $serialization, Property $property ) {
123 12
		if ( !array_key_exists( 'claims', $serialization ) ) {
124 9
			return;
125
		}
126
127
		/** @var StatementList $statements */
128 3
		$statements = $this->statementListDeserializer->deserialize( $serialization['claims'] );
129 3
		$property->setStatements( $statements );
130 3
	}
131
132
}
133