Passed
Push — rm-hasharray ( 40b512...e23e3c )
by no
28:33 queued 25:39
created

EntityIdValue::newFromArray()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 4
nop 1
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
22
	public function __construct( EntityId $entityId ) {
23
		$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
	 *
33
	 * @return string
34
	 */
35
	public function serialize() {
36
		return serialize( $this->entityId );
37
	}
38
39
	/**
40
	 * @see Serializable::unserialize
41
	 *
42
	 * @param string $serialized
43
	 *
44
	 * @throws IllegalValueException
45
	 */
46
	public function unserialize( $serialized ) {
47
		$array = json_decode( $serialized );
48
49
		if ( !is_array( $array ) ) {
50
			$this->entityId = unserialize( $serialized );
51
			return;
52
		}
53
54
		list( $entityType, $numericId ) = $array;
55
56
		try {
57
			$entityId = LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId );
58
		} catch ( InvalidArgumentException $ex ) {
59
			throw new IllegalValueException( 'Invalid EntityIdValue serialization.', 0, $ex );
60
		}
61
62
		$this->__construct( $entityId );
63
	}
64
65
	/**
66
	 * @see DataValue::getType
67
	 *
68
	 * @return string
69
	 */
70
	public static function getType() {
71
		return 'wikibase-entityid';
72
	}
73
74
	/**
75
	 * @see DataValue::getSortKey
76
	 *
77
	 * @return string|float|int
78
	 */
79
	public function getSortKey() {
80
		return $this->entityId->getSerialization();
81
	}
82
83
	/**
84
	 * @see DataValue::getValue
85
	 *
86
	 * @return self
87
	 */
88
	public function getValue() {
89
		return $this;
90
	}
91
92
	/**
93
	 * @return EntityId
94
	 */
95
	public function getEntityId() {
96
		return $this->entityId;
97
	}
98
99
	/**
100
	 * @see DataValue::getArrayValue
101
	 *
102
	 * @return array
103
	 */
104
	public function getArrayValue() {
105
		$array = [
106
			'entity-type' => $this->entityId->getEntityType(),
107
		];
108
109
		if ( $this->entityId instanceof Int32EntityId ) {
110
			$array['numeric-id'] = $this->entityId->getNumericId();
111
		}
112
113
		$array['id'] = $this->entityId->getSerialization();
114
		return $array;
115
	}
116
117
	/**
118
	 * Constructs a new instance from the provided data. Required for @see DataValueDeserializer.
119
	 * This is expected to round-trip with @see getArrayValue.
120
	 *
121
	 * @deprecated since 7.1. Static DataValue::newFromArray constructors like this are
122
	 *  underspecified (not in the DataValue interface), and misleadingly named (should be named
123
	 *  newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.
124
	 *
125
	 * @param mixed $data Warning! Even if this is expected to be a value as returned by
126
	 *  @see getArrayValue, callers of this specific newFromArray implementation can not guarantee
127
	 *  this. This is not even guaranteed to be an array!
128
	 *
129
	 * @throws IllegalValueException if $data is not in the expected format. Subclasses of
130
	 *  InvalidArgumentException are expected and properly handled by @see DataValueDeserializer.
131
	 * @return self
132
	 */
133
	public static function newFromArray( $data ) {
134
		if ( !is_array( $data ) ) {
135
			throw new IllegalValueException( '$data must be an array' );
136
		}
137
138
		if ( array_key_exists( 'entity-type', $data ) && array_key_exists( 'numeric-id', $data ) ) {
139
			return self::newIdFromTypeAndNumber( $data['entity-type'], $data['numeric-id'] );
140
		} elseif ( array_key_exists( 'id', $data ) ) {
141
			throw new IllegalValueException(
142
				'Not able to parse "id" strings, use callbacks in DataValueDeserializer instead'
143
			);
144
		}
145
146
		throw new IllegalValueException( 'Either "id" or "entity-type" and "numeric-id" fields required' );
147
	}
148
149
	/**
150
	 * @param string $entityType
151
	 * @param int|float|string $numericId
152
	 *
153
	 * @throws IllegalValueException
154
	 * @return self
155
	 */
156
	private static function newIdFromTypeAndNumber( $entityType, $numericId ) {
157
		try {
158
			return new self( LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId ) );
159
		} catch ( InvalidArgumentException $ex ) {
160
			throw new IllegalValueException( $ex->getMessage(), 0, $ex );
161
		}
162
	}
163
164
}
165