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

src/Deserializers/ItemDeserializer.php (4 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\Item;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\SiteLink;
11
use Wikibase\DataModel\SiteLinkList;
12
use Wikibase\DataModel\Statement\StatementList;
13
use Wikibase\DataModel\Term\AliasGroupList;
14
use Wikibase\DataModel\Term\TermList;
15
16
/**
17
 * Package private
18
 *
19
 * @license GPL-2.0+
20
 * @author Thomas Pellissier Tanon
21
 * @author Bene* < [email protected] >
22
 */
23
class ItemDeserializer extends TypedObjectDeserializer {
24
25
	/**
26
	 * @var Deserializer
27
	 */
28
	private $entityIdDeserializer;
29
30
	/**
31
	 * @var Deserializer
32
	 */
33
	private $termListDeserializer;
34
35
	/**
36
	 * @var Deserializer
37
	 */
38
	private $aliasGroupListDeserializer;
39
40
	/**
41
	 * @var Deserializer
42
	 */
43
	private $statementListDeserializer;
44
45
	/**
46
	 * @var Deserializer
47
	 */
48
	private $siteLinkDeserializer;
49
50 30
	public function __construct(
51
		Deserializer $entityIdDeserializer,
52
		Deserializer $termListDeserializer,
53
		Deserializer $aliasGroupListDeserializer,
54
		Deserializer $statementListDeserializer,
55
		Deserializer $siteLinkDeserializer
56
	) {
57 30
		parent::__construct( 'item', 'type' );
58
59 30
		$this->entityIdDeserializer = $entityIdDeserializer;
60 30
		$this->termListDeserializer = $termListDeserializer;
61 30
		$this->aliasGroupListDeserializer = $aliasGroupListDeserializer;
62 30
		$this->statementListDeserializer = $statementListDeserializer;
63 30
		$this->siteLinkDeserializer = $siteLinkDeserializer;
64 30
	}
65
66
	/**
67
	 * @see Deserializer::deserialize
68
	 *
69
	 * @param array $serialization
70
	 *
71
	 * @throws DeserializationException
72
	 * @return Item
73
	 */
74 21
	public function deserialize( $serialization ) {
75 21
		$this->assertCanDeserialize( $serialization );
76
77 18
		return $this->getDeserialized( $serialization );
78
	}
79
80 18
	private function getDeserialized( array $serialization ) {
81 18
		$item = new Item();
82
83 18
		$this->setIdFromSerialization( $serialization, $item );
84 18
		$this->setTermsFromSerialization( $serialization, $item );
85 18
		$this->setStatementListFromSerialization( $serialization, $item );
86 18
		$this->setSiteLinksFromSerialization( $item->getSiteLinkList(), $serialization );
87
88 18
		return $item;
89
	}
90
91 18
	private function setIdFromSerialization( array $serialization, Item $item ) {
92 18
		if ( !array_key_exists( 'id', $serialization ) ) {
93 12
			return;
94
		}
95
96
		/** @var ItemId $id */
97 6
		$id = $this->entityIdDeserializer->deserialize( $serialization['id'] );
98 6
		$item->setId( $id );
99 6
	}
100
101 18
	private function setTermsFromSerialization( array $serialization, Item $item ) {
102 18
		if ( array_key_exists( 'labels', $serialization ) ) {
103 10
			$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...
104
			/** @var TermList $labels */
105 10
			$labels = $this->termListDeserializer->deserialize( $serialization['labels'] );
106 10
			$item->getFingerprint()->setLabels( $labels );
107
		}
108
109 18
		if ( array_key_exists( 'descriptions', $serialization ) ) {
110 10
			$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...
111
			/** @var TermList $descriptions */
112 10
			$descriptions = $this->termListDeserializer->deserialize( $serialization['descriptions'] );
113 10
			$item->getFingerprint()->setDescriptions( $descriptions );
114
		}
115
116 18
		if ( array_key_exists( 'aliases', $serialization ) ) {
117 10
			$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...
118
			/** @var AliasGroupList $aliases */
119 10
			$aliases = $this->aliasGroupListDeserializer->deserialize( $serialization['aliases'] );
120 10
			$item->getFingerprint()->setAliasGroups( $aliases );
121
		}
122 18
	}
123
124 18
	private function setStatementListFromSerialization( array $serialization, Item $item ) {
125 18
		if ( !array_key_exists( 'claims', $serialization ) ) {
126 8
			return;
127
		}
128
129
		/** @var StatementList $statements */
130 10
		$statements = $this->statementListDeserializer->deserialize( $serialization['claims'] );
131 10
		$item->setStatements( $statements );
132 10
	}
133
134 18
	private function setSiteLinksFromSerialization(
135
		SiteLinkList $siteLinkList,
136
		array $serialization
137
	) {
138 18
		if ( !array_key_exists( 'sitelinks', $serialization ) ) {
139 8
			return;
140
		}
141
142 10
		$this->assertAttributeIsArray( $serialization, 'sitelinks' );
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...
143
144 10
		foreach ( $serialization['sitelinks'] as $siteLinksSerialization ) {
145
			/** @var SiteLink $siteLink */
146 5
			$siteLink = $this->siteLinkDeserializer->deserialize( $siteLinksSerialization );
147 5
			$siteLinkList->addSiteLink( $siteLink );
148
		}
149 10
	}
150
151
}
152