Completed
Pull Request — master (#666)
by no
10:55 queued 07:27
created

EntityIdValue::getSortKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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
 */
16
class EntityIdValue extends DataValueObject {
17
18
	private $entityId;
19
20
	public function __construct( EntityId $entityId ) {
21 19
		$this->entityId = $entityId;
22 19
	}
23 19
24
	/**
25
	 * @see Serializable::serialize
26
	 *
27
	 * @since 0.5
28
	 *
29
	 * @return string
30
	 */
31
	public function serialize() {
32 8
		return json_encode( array(
33 8
			$this->entityId->getEntityType(),
34 8
			$this->getNumericId()
35 8
		) );
36 8
	}
37
38
	/**
39
	 * This method gets the numeric id from the serialization.
40
	 * It makes assumptions we do not want to make about the id format,
41
	 * though cannot be removed until we ditch the "numeric id" part
42
	 * from the serialization.
43
	 *
44
	 * @return float Numeric id as a whole number. Can not be int because of 32-bit PHP.
45
	 */
46
	protected function getNumericId() {
47 16
		return floatval( substr( $this->entityId->getSerialization(), 1 ) );
48 16
	}
49
50
	/**
51
	 * @see Serializable::unserialize
52
	 *
53
	 * @since 0.5
54
	 *
55
	 * @param string $serialized
56
	 *
57
	 * @throws IllegalValueException
58
	 */
59
	public function unserialize( $serialized ) {
60 8
		list( $entityType, $numericId ) = json_decode( $serialized );
61 8
62
		try {
63
			$entityId = LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId );
64 8
		} catch ( InvalidArgumentException $ex ) {
65 8
			throw new IllegalValueException( 'Invalid EntityIdValue serialization.' );
66
		}
67
68
		$this->__construct( $entityId );
69 8
	}
70 8
71
	/**
72
	 * @see DataValue::getType
73
	 *
74
	 * @since 0.5
75
	 *
76
	 * @return string
77
	 */
78
	public static function getType() {
79 7
		return 'wikibase-entityid';
80 7
	}
81
82
	/**
83
	 * @see DataValue::getSortKey
84
	 *
85
	 * @since 0.5
86
	 *
87
	 * @return string|float|int
88
	 */
89
	public function getSortKey() {
90 7
		return $this->entityId->getSerialization();
91 7
	}
92
93
	/**
94
	 * @see DataValue::getValue
95
	 *
96
	 * @since 0.5
97
	 *
98
	 * @return self
99
	 */
100
	public function getValue() {
101 7
		return $this;
102 7
	}
103
104
	/**
105
	 * @since 0.5
106
	 *
107
	 * @return EntityId
108
	 */
109
	public function getEntityId() {
110 1
		return $this->entityId;
111 1
	}
112
113
	/**
114
	 * @see DataValue::getArrayValue
115
	 *
116
	 * @since 0.5
117
	 *
118
	 * @return array
119
	 */
120
	public function getArrayValue() {
121 8
		return array(
122
			'entity-type' => $this->entityId->getEntityType(),
123 8
			'numeric-id' => $this->getNumericId(),
124 8
		);
125 8
	}
126
127
	/**
128
	 * Constructs a new instance of the DataValue from the provided data.
129
	 * This can round-trip with
130
	 * @see getArrayValue
131
	 *
132
	 * @since 0.5
133
	 *
134
	 * @param mixed $data
135
	 *
136
	 * @throws IllegalValueException
137
	 * @return self
138
	 */
139
	public static function newFromArray( $data ) {
140 17
		if ( !is_array( $data ) ) {
141 17
			throw new IllegalValueException( '$data must be an array; got ' . gettype( $data ) );
142 2
		}
143
144
		if ( !array_key_exists( 'entity-type', $data ) ) {
145 15
			throw new IllegalValueException( "'entity-type' field required" );
146 2
		}
147
148
		if ( !array_key_exists( 'numeric-id', $data ) ) {
149 13
			throw new IllegalValueException( "'numeric-id' field required" );
150 1
		}
151
152
		try {
153
			$id = LegacyIdInterpreter::newIdFromTypeAndNumber(
154 12
				$data['entity-type'],
155 12
				$data['numeric-id']
156 12
			);
157 12
		} catch ( InvalidArgumentException $ex ) {
158 12
			throw new IllegalValueException( $ex->getMessage(), 0, $ex );
159 4
		}
160
161
		return new static( $id );
162 8
	}
163
164
}
165