Completed
Pull Request — master (#728)
by Daniel
05:40 queued 02:59
created

EntityIdValue::newFromArray()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 4
nop 1
crap 5
1
<?php
2
3
namespace Wikibase\DataModel\Entity;
4
5
use DataValues\DataValueObject;
6
use DataValues\IllegalValueException;
7
use InvalidArgumentException;
8
use Wikibase\DataModel\LegacyIdInterpreter;
9
10
/**
11
 * @since 0.5
12
 *
13
 * @license GPL-2.0+
14
 * @author Jeroen De Dauw < [email protected] >
15
 * @author Thiemo Mättig
16
 * @author Daniel Kinzler
17
 */
18
class EntityIdValue extends DataValueObject {
19
20
	private $entityId;
21 19
22 19
	public function __construct( EntityId $entityId ) {
23 19
		$this->entityId = $entityId;
24
	}
25
26
	/**
27
	 * @see Serializable::serialize
28
	 *
29
	 * @since 7.0 serialization format changed in an incompatible way
30
	 *
31
	 * @note Do not use PHP serialization for persistence! Use a DataValueSerializer instead.
32 8
	 *
33 8
	 *
34 8
	 * @return string
35 8
	 */
36 8
	public function serialize() {
37
		return serialize( $this->entityId );
38
	}
39
40
	/**
41
	 * @see Serializable::unserialize
42
	 *
43
	 * @since 0.5
44
	 *
45
	 * @param string $serialized
46
	 *
47 16
	 * @throws IllegalValueException
48 16
	 */
49
	public function unserialize( $serialized ) {
50
		$array = json_decode( $serialized );
51
52
		if ( !is_array( $array ) ) {
53
			$this->entityId = unserialize( $serialized );
54
			return;
55
		}
56
57
		list( $entityType, $numericId ) = $array;
58
59
		try {
60 8
			$entityId = LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId );
61 8
		} catch ( InvalidArgumentException $ex ) {
62
			throw new IllegalValueException( 'Invalid EntityIdValue serialization.', 0, $ex );
63
		}
64 8
65 8
		$this->__construct( $entityId );
66
	}
67
68
	/**
69 8
	 * @see DataValue::getType
70 8
	 *
71
	 * @since 0.5
72
	 *
73
	 * @return string
74
	 */
75
	public static function getType() {
76
		return 'wikibase-entityid';
77
	}
78
79 7
	/**
80 7
	 * @see DataValue::getSortKey
81
	 *
82
	 * @since 0.5
83
	 *
84
	 * @return string|float|int
85
	 */
86
	public function getSortKey() {
87
		return $this->entityId->getSerialization();
88
	}
89
90 7
	/**
91 7
	 * @see DataValue::getValue
92
	 *
93
	 * @since 0.5
94
	 *
95
	 * @return self
96
	 */
97
	public function getValue() {
98
		return $this;
99
	}
100
101 7
	/**
102 7
	 * @since 0.5
103
	 *
104
	 * @return EntityId
105
	 */
106
	public function getEntityId() {
107
		return $this->entityId;
108
	}
109
110 1
	/**
111 1
	 * @see DataValue::getArrayValue
112
	 *
113
	 * @since 0.5
114
	 *
115
	 * @return array
116
	 */
117
	public function getArrayValue() {
118
		$array = [
119
			'entity-type' => $this->entityId->getEntityType(),
120
			'id' => $this->entityId->getSerialization(),
121 8
		];
122
123 8
		if ( $this->entityId instanceof Int32EntityId ) {
124 8
			$array['numeric-id'] = $this->entityId->getNumericId();
125 8
		}
126
127
		return $array;
128
	}
129
130
	/**
131
	 * Constructs a new instance of the DataValue from the provided data.
132
	 * This can round-trip with
133
	 * @see getArrayValue
134
	 *
135
	 * @since 0.5
136
	 *
137
	 * @param mixed $data
138
	 *
139
	 * @deprecated since 7.1, use callbacks in DataValueDeserializer instead
140 17
	 *
141 17
	 * @throws IllegalValueException
142 2
	 * @return self
143
	 */
144
	public static function newFromArray( $data ) {
145 15
		if ( !is_array( $data ) ) {
146 2
			throw new IllegalValueException( '$data must be an array' );
147
		}
148
149 13
		if ( array_key_exists( 'entity-type', $data ) && array_key_exists( 'numeric-id', $data ) ) {
150 1
			return self::newIdFromTypeAndNumber( $data['entity-type'], $data['numeric-id'] );
151
		} elseif ( array_key_exists( 'id', $data ) ) {
152
			throw new IllegalValueException(
153
				'Not able to parse "id" strings, use callbacks in DataValueDeserializer instead'
154 12
			);
155 12
		}
156 12
157 12
		throw new IllegalValueException( 'Either "id" or "entity-type" and "numeric-id" fields required' );
158 12
	}
159 4
160
	/**
161
	 * @param string $entityType
162 8
	 * @param int|float|string $numericId
163
	 *
164
	 * @throws IllegalValueException
165
	 * @return self
166
	 */
167
	private static function newIdFromTypeAndNumber( $entityType, $numericId ) {
168
		try {
169
			return new self( LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId ) );
170
		} catch ( InvalidArgumentException $ex ) {
171
			throw new IllegalValueException( $ex->getMessage(), 0, $ex );
172
		}
173
	}
174
175
}
176