Completed
Pull Request — master (#728)
by no
05:47 queued 02:45
created

EntityIdValue::getSortKey()   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 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
	 * @return string
34 8
	 */
35 8
	public function serialize() {
36 8
		return serialize( $this->entityId );
37
	}
38
39
	/**
40
	 * @see Serializable::unserialize
41
	 *
42
	 * @since 0.5
43
	 *
44
	 * @param string $serialized
45
	 *
46
	 * @throws IllegalValueException
47 16
	 */
48 16
	public function unserialize( $serialized ) {
49
		$array = json_decode( $serialized );
50
51
		if ( !is_array( $array ) ) {
52
			$this->entityId = unserialize( $serialized );
53
			return;
54
		}
55
56
		list( $entityType, $numericId ) = $array;
57
58
		try {
59
			$entityId = LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId );
60 8
		} catch ( InvalidArgumentException $ex ) {
61 8
			throw new IllegalValueException( 'Invalid EntityIdValue serialization.', 0, $ex );
62
		}
63
64 8
		$this->__construct( $entityId );
65 8
	}
66
67
	/**
68
	 * @see DataValue::getType
69 8
	 *
70 8
	 * @since 0.5
71
	 *
72
	 * @return string
73
	 */
74
	public static function getType() {
75
		return 'wikibase-entityid';
76
	}
77
78
	/**
79 7
	 * @see DataValue::getSortKey
80 7
	 *
81
	 * @since 0.5
82
	 *
83
	 * @return string|float|int
84
	 */
85
	public function getSortKey() {
86
		return $this->entityId->getSerialization();
87
	}
88
89
	/**
90 7
	 * @see DataValue::getValue
91 7
	 *
92
	 * @since 0.5
93
	 *
94
	 * @return self
95
	 */
96
	public function getValue() {
97
		return $this;
98
	}
99
100
	/**
101 7
	 * @since 0.5
102 7
	 *
103
	 * @return EntityId
104
	 */
105
	public function getEntityId() {
106
		return $this->entityId;
107
	}
108
109
	/**
110 1
	 * @see DataValue::getArrayValue
111 1
	 *
112
	 * @since 0.5
113
	 *
114
	 * @return array
115
	 */
116
	public function getArrayValue() {
117
		$array = [
118
			'entity-type' => $this->entityId->getEntityType(),
119
		];
120
121 8
		if ( $this->entityId instanceof Int32EntityId ) {
122
			$array['numeric-id'] = $this->entityId->getNumericId();
123 8
		}
124 8
125 8
		$array['id'] = $this->entityId->getSerialization();
126
		return $array;
127
	}
128
129
	/**
130
	 * Constructs a new instance of the DataValue from the provided data.
131
	 * This can round-trip with
132
	 * @see getArrayValue
133
	 *
134
	 * @since 0.5
135
	 *
136
	 * @param mixed $data
137
	 *
138
	 * @throws IllegalValueException
139
	 * @return self
140 17
	 */
141 17
	public static function newFromArray( $data ) {
142 2
		if ( !is_array( $data ) ) {
143
			throw new IllegalValueException( '$data must be an array' );
144
		}
145 15
146 2
		if ( array_key_exists( 'entity-type', $data ) && array_key_exists( 'numeric-id', $data ) ) {
147
			return self::newIdFromTypeAndNumber( $data['entity-type'], $data['numeric-id'] );
148
		} elseif ( array_key_exists( 'id', $data ) ) {
149 13
			throw new IllegalValueException(
150 1
				'Not able to parse "id" strings, use callbacks in DataValueDeserializer instead'
151
			);
152
		}
153
154 12
		throw new IllegalValueException( 'Either "id" or "entity-type" and "numeric-id" fields required' );
155 12
	}
156 12
157 12
	/**
158 12
	 * @param string $entityType
159 4
	 * @param int|float|string $numericId
160
	 *
161
	 * @throws IllegalValueException
162 8
	 * @return self
163
	 */
164
	private static function newIdFromTypeAndNumber( $entityType, $numericId ) {
165
		try {
166
			return new self( LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId ) );
167
		} catch ( InvalidArgumentException $ex ) {
168
			throw new IllegalValueException( $ex->getMessage(), 0, $ex );
169
		}
170
	}
171
172
}
173