1212 *
1313 * @license GPL-2.0+
1414 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
15+ * @author Thiemo Mättig
1516 */
1617class EntityIdValue extends DataValueObject {
1718
@@ -43,7 +44,7 @@ public function serialize() {
4344 *
4445 * @return float Numeric id as a whole number. Can not be int because of 32-bit PHP.
4546 */
46- protected function getNumericId () {
47+ private function getNumericId () {
4748 return floatval ( substr ( $ this ->entityId ->getSerialization (), 1 ) );
4849 }
4950
@@ -121,6 +122,7 @@ public function getArrayValue() {
121122 return array (
122123 'entity-type ' => $ this ->entityId ->getEntityType (),
123124 'numeric-id ' => $ this ->getNumericId (),
125+ 'id ' => $ this ->entityId ->getSerialization (),
124126 );
125127 }
126128
@@ -138,27 +140,33 @@ public function getArrayValue() {
138140 */
139141 public static function newFromArray ( $ data ) {
140142 if ( !is_array ( $ data ) ) {
141- throw new IllegalValueException ( '$data must be an array; got ' . gettype ( $ data ) );
143+ throw new IllegalValueException ( '$data must be an array ' );
142144 }
143145
144- if ( !array_key_exists ( 'entity-type ' , $ data ) ) {
145- throw new IllegalValueException ( "'entity-type' field required " );
146+ 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+ throw new IllegalValueException (
150+ 'Not able to parse "id" strings, use callbacks in DataValueDeserializer instead '
151+ );
146152 }
147153
148- if ( !array_key_exists ( 'numeric-id ' , $ data ) ) {
149- throw new IllegalValueException ( "'numeric-id' field required " );
150- }
154+ throw new IllegalValueException ( 'Either "id" or "entity-type" and "numeric-id" fields required ' );
155+ }
151156
157+ /**
158+ * @param string $entityType
159+ * @param int|float|string $numericId
160+ *
161+ * @throws IllegalValueException
162+ * @return self
163+ */
164+ private static function newIdFromTypeAndNumber ( $ entityType , $ numericId ) {
152165 try {
153- $ id = LegacyIdInterpreter::newIdFromTypeAndNumber (
154- $ data ['entity-type ' ],
155- $ data ['numeric-id ' ]
156- );
166+ return new self ( LegacyIdInterpreter::newIdFromTypeAndNumber ( $ entityType , $ numericId ) );
157167 } catch ( InvalidArgumentException $ ex ) {
158168 throw new IllegalValueException ( $ ex ->getMessage (), 0 , $ ex );
159169 }
160-
161- return new static ( $ id );
162170 }
163171
164172}
0 commit comments