/** * @dataProvider provideConstructor */ public function testConstructor($fields) { $term = new TermIndexEntry($fields); $entityId = null; if (isset($fields['entityType']) && isset($fields['entityId'])) { // FIXME: This must be removed once we got rid of all legacy numeric ids. $entityId = LegacyIdInterpreter::newIdFromTypeAndNumber($fields['entityType'], $fields['entityId']); } $this->assertEquals(isset($fields['entityType']) ? $fields['entityType'] : null, $term->getEntityType()); $this->assertEquals($entityId, $term->getEntityId()); $this->assertEquals(isset($fields['termType']) ? $fields['termType'] : null, $term->getType()); $this->assertEquals(isset($fields['termLanguage']) ? $fields['termLanguage'] : null, $term->getLanguage()); $this->assertEquals(isset($fields['termText']) ? $fields['termText'] : null, $term->getText()); $this->assertEquals(isset($fields['termWeight']) ? $fields['termWeight'] : null, $term->getWeight()); }
/** * @param array $data An array representation of an EntityContent object. * @param string $key The key in $data that contains the serialized ID. * * @throws MWContentSerializationException * @return EntityId|null The ID of the entity (resp. redirect), or null if * $key is not set in $data. */ private function extractEntityId(array $data, $key) { if (!isset($data[$key])) { return null; } if (is_array($data[$key])) { try { // Handle the old-style representation of IDs as a two element array. $stubbedId = $data[$key]; return LegacyIdInterpreter::newIdFromTypeAndNumber($stubbedId[0], $stubbedId[1]); } catch (InvalidArgumentException $ex) { throw new MWContentSerializationException($ex->getMessage(), 0, $ex); } } try { return $this->entityIdParser->parse($data[$key]); } catch (EntityIdParsingException $ex) { throw new MWContentSerializationException($ex->getMessage(), 0, $ex); } }
protected function getEntityIdsFromRows($rows) { $entities = array(); foreach ($rows as $row) { // FIXME: this only works for items and properties $entities[] = LegacyIdInterpreter::newIdFromTypeAndNumber($row->entity_type, $row->entity_id); } return $entities; }
/** * Constructs a new instance of the DataValue from the provided data. * This can round-trip with * @see getArrayValue * * @since 0.5 * * @param mixed $data * * @throws IllegalValueException * @return self */ public static function newFromArray($data) { if (!is_array($data)) { throw new IllegalValueException('$data must be an array; got ' . gettype($data)); } if (!array_key_exists('entity-type', $data)) { throw new IllegalValueException("'entity-type' field required"); } if (!array_key_exists('numeric-id', $data)) { throw new IllegalValueException("'numeric-id' field required"); } try { $id = LegacyIdInterpreter::newIdFromTypeAndNumber($data['entity-type'], $data['numeric-id']); } catch (InvalidArgumentException $ex) { throw new IllegalValueException($ex->getMessage(), 0, $ex); } return new static($id); }
/** * @dataProvider invalidInputProvider */ public function testNewIdFromTypeAndNumber_withInvalidInput($type, $number) { $this->setExpectedException('InvalidArgumentException'); LegacyIdInterpreter::newIdFromTypeAndNumber($type, $number); }
/** * Injects terms from a DB result into the $entityInfo structure. * * @note: Keep in sync with EntitySerializer! * * @param ResultWrapper $dbResult * * @throws InvalidArgumentException */ private function injectTerms(ResultWrapper $dbResult) { foreach ($dbResult as $row) { // FIXME: this only works for items and properties $entityId = LegacyIdInterpreter::newIdFromTypeAndNumber($row->term_entity_type, (int) $row->term_entity_id); $key = $entityId->getSerialization(); if (!isset($this->entityInfo[$key])) { continue; } $field = self::$termTypeFields[$row->term_type]; switch ($row->term_type) { case 'label': $this->injectLabel($this->entityInfo[$key][$field], $row->term_language, $row->term_text); break; case 'description': $this->injectDescription($this->entityInfo[$key][$field], $row->term_language, $row->term_text); break; case 'alias': $this->injectAlias($this->entityInfo[$key][$field], $row->term_language, $row->term_text); break; default: wfDebugLog(__CLASS__, __FUNCTION__ . ': unknown term type: ' . $row->term_type); } } }