Completed
Push — master ( 37045e...c0656f )
by Marius
12:38 queued 02:38
created

src/Deserializers/PropertyDeserializer.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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+
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