Passed
Push — int32EntityId ( fa80fb )
by no
05:13
created

EntityIdValue::getNumericId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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
		$this->entityId = $entityId;
22
	}
23
24
	/**
25
	 * @see Serializable::serialize
26
	 *
27
	 * @since 0.5
28
	 *
29
	 * @return string
30
	 */
31
	public function serialize() {
32
		return json_encode( array(
33
			$this->entityId->getEntityType(),
34
			$this->getNumericId()
35
		) );
36
	}
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
		return floatval( substr( $this->entityId->getSerialization(), 1 ) );
48
	}
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
		list( $entityType, $numericId ) = json_decode( $serialized );
61
62
		try {
63
			$entityId = LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId );
64
		} catch ( InvalidArgumentException $ex ) {
65
			throw new IllegalValueException( 'Invalid EntityIdValue serialization.' );
66
		}
67
68
		$this->__construct( $entityId );
69
	}
70
71
	/**
72
	 * @see DataValue::getType
73
	 *
74
	 * @since 0.5
75
	 *
76
	 * @return string
77
	 */
78
	public static function getType() {
79
		return 'wikibase-entityid';
80
	}
81
82
	/**
83
	 * @see DataValue::getSortKey
84
	 *
85
	 * @since 0.5
86
	 *
87
	 * @return string|float|int
88
	 */
89
	public function getSortKey() {
90
		return $this->entityId->getSerialization();
91
	}
92
93
	/**
94
	 * @see DataValue::getValue
95
	 *
96
	 * @since 0.5
97
	 *
98
	 * @return self
99
	 */
100
	public function getValue() {
101
		return $this;
102
	}
103
104
	/**
105
	 * @since 0.5
106
	 *
107
	 * @return EntityId
108
	 */
109
	public function getEntityId() {
110
		return $this->entityId;
111
	}
112
113
	/**
114
	 * @see DataValue::getArrayValue
115
	 *
116
	 * @since 0.5
117
	 *
118
	 * @return array
119
	 */
120
	public function getArrayValue() {
121
		return array(
122
			'entity-type' => $this->entityId->getEntityType(),
123
			'numeric-id' => $this->getNumericId(),
124
		);
125
	}
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
		if ( !is_array( $data ) ) {
141
			throw new IllegalValueException( '$data must be an array; got ' . gettype( $data ) );
142
		}
143
144
		if ( !array_key_exists( 'entity-type', $data ) ) {
145
			throw new IllegalValueException( "'entity-type' field required" );
146
		}
147
148
		if ( !array_key_exists( 'numeric-id', $data ) ) {
149
			throw new IllegalValueException( "'numeric-id' field required" );
150
		}
151
152
		try {
153
			$id = LegacyIdInterpreter::newIdFromTypeAndNumber(
154
				$data['entity-type'],
155
				$data['numeric-id']
156
			);
157
		} catch ( InvalidArgumentException $ex ) {
158
			throw new IllegalValueException( $ex->getMessage(), 0, $ex );
159
		}
160
161
		return new static( $id );
162
	}
163
164
}
165