Completed
Pull Request — master (#728)
by Daniel
04:05 queued 01:24
created

EntityIdValue::getNumericId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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