Skip to content

Commit

Permalink
Tidy (DataTypeRegistry) (#2315)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Mar 5, 2017
1 parent 6aee84f commit 9d465f1
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 134 deletions.
2 changes: 1 addition & 1 deletion includes/dataitems/SMW_DI_Property.php
Expand Up @@ -313,7 +313,7 @@ public function getRedirectTarget() {
*/
public function setPropertyTypeId( $propertyTypeId ) {

if ( !DataTypeRegistry::getInstance()->isKnownTypeId( $propertyTypeId ) ) {
if ( !DataTypeRegistry::getInstance()->isKnownByType( $propertyTypeId ) ) {
throw new PropertyDataTypeLookupExeption( "{$propertyTypeId} is an unknown type id" );
}

Expand Down
2 changes: 1 addition & 1 deletion includes/datavalues/SMW_DV_Types.php
Expand Up @@ -59,7 +59,7 @@ protected function parseUserValue( $value ) {
$this->m_typeId = $value;
} else {
$this->m_givenLabel = smwfNormalTitleText( $value );
$this->m_typeId = DataTypeRegistry::getInstance()->findTypeIdByLanguage( $this->m_givenLabel, $contentLanguage );
$this->m_typeId = DataTypeRegistry::getInstance()->findTypeByLanguage( $this->m_givenLabel, $contentLanguage );
}

if ( $this->m_typeId === '' ) {
Expand Down
91 changes: 48 additions & 43 deletions src/DataTypeRegistry.php
Expand Up @@ -26,6 +26,11 @@ class DataTypeRegistry {
*/
protected static $instance = null;

/**
* @var ExtraneousLanguage
*/
private $extraneousLanguage;

/**
* Array of type labels indexed by type ids. Used for datatype resolution.
*
Expand Down Expand Up @@ -78,7 +83,7 @@ class DataTypeRegistry {
*
* @var string[]
*/
private $defaultDataItemTypeIds = array(
private $defaultDataItemTypeMap = array(
DataItem::TYPE_BLOB => '_txt', // Text type
DataItem::TYPE_URI => '_uri', // URL/URI type
DataItem::TYPE_WIKIPAGE => '_wpg', // Page type
Expand Down Expand Up @@ -150,28 +155,15 @@ public static function clear() {
* @param ExtraneousLanguage $extraneousLanguage
*/
public function __construct( ExtraneousLanguage $extraneousLanguage ) {
$typeLabels = $extraneousLanguage->getDatatypeLabels();
$typeAliases = $extraneousLanguage->getDatatypeAliases();
$canonicalLabels = $extraneousLanguage->getCanonicalDatatypeLabels();

foreach ( $typeLabels as $typeId => $typeLabel ) {
$this->registerTypeLabel( $typeId, $typeLabel );
}

foreach ( $typeAliases as $typeAlias => $typeId ) {
$this->registerDataTypeAlias( $typeId, $typeAlias );
}

foreach ( $canonicalLabels as $label => $id ) {
$this->canonicalLabels[$id] = $label;
}
$this->extraneousLanguage = $extraneousLanguage;
$this->registerLabels();
}

/**
* @deprecated since 2.5, use DataTypeRegistry::getDataItemByTypeId
* @deprecated since 2.5, use DataTypeRegistry::getDataItemByType
*/
public function getDataItemId( $typeId ) {
return $this->getDataItemByTypeId( $typeId );
return $this->getDataItemByType( $typeId );
}

/**
Expand All @@ -186,7 +178,7 @@ public function getDataItemId( $typeId ) {
* @param $typeId string id string for the given type
* @return integer data item ID
*/
public function getDataItemByTypeId( $typeId ) {
public function getDataItemByType( $typeId ) {

if ( isset( $this->typeDataItemIds[$typeId] ) ) {
return $this->typeDataItemIds[$typeId];
Expand All @@ -199,9 +191,10 @@ public function getDataItemByTypeId( $typeId ) {
* @since 2.0
*
* @param string
*
* @return boolean
*/
public function isKnownTypeId( $typeId ) {
public function isKnownByType( $typeId ) {
return isset( $this->typeDataItemIds[$typeId] );
}

Expand All @@ -225,7 +218,7 @@ public function isSubDataType( $typeId ) {
* @return boolean
*/
public function isEqualByType( $srcType, $tagType ) {
return $this->getDataItemByTypeId( $srcType ) === $this->getDataItemByTypeId( $tagType );
return $this->getDataItemByType( $srcType ) === $this->getDataItemByType( $tagType );
}

/**
Expand Down Expand Up @@ -254,7 +247,7 @@ private function registerTypeLabel( $typeId, $typeLabel ) {
}

private function addTextToIdLookupMap( $dataTypeId, $text ) {
$this->typeByLabelOrAliasLookup[strtolower($text)] = $dataTypeId;
$this->typeByLabelOrAliasLookup[mb_strtolower($text)] = $dataTypeId;
}

/**
Expand Down Expand Up @@ -284,7 +277,7 @@ public function registerDataTypeAlias( $typeId, $typeAlias ) {
*/
public function findTypeId( $label ) {

$label = strtolower( $label );
$label = mb_strtolower( $label );

if ( isset( $this->typeByLabelOrAliasLookup[$label] ) ) {
return $this->typeByLabelOrAliasLookup[$label];
Expand All @@ -301,27 +294,17 @@ public function findTypeId( $label ) {
*
* @return string
*/
public function findTypeIdByLanguage( $label, $languageCode = false ) {

$label = mb_strtolower( $label );
public function findTypeByLanguage( $label, $languageCode = false ) {

if ( !$languageCode ) {
return $this->findTypeId( $label );
}

$extraneousLanguage = Localizer::getInstance()->getExtraneousLanguage( $languageCode );

$datatypeLabels = $extraneousLanguage->getDatatypeLabels();
$datatypeLabels = array_flip( $datatypeLabels );
$datatypeLabels += $extraneousLanguage->getDatatypeAliases();

foreach ( $datatypeLabels as $key => $id ) {
if ( mb_strtolower( $key ) === $label ) {
return $id;
}
}
$extraneousLanguage = $this->extraneousLanguage->fetchByLanguageCode(
$languageCode
);

return '';
return $extraneousLanguage->findDatatypeByLabel( $label );
}

/**
Expand Down Expand Up @@ -395,18 +378,25 @@ public function getKnownTypeAliases() {
}

/**
* Returns a default DataItemId
* @deprecated since 2.5, use DataTypeRegistry::getDefaultDataItemByType
*/
public function getDefaultDataItemTypeId( $diType ) {
return $this->getDefaultDataItemByType( $typeId );
}

/**
* Returns a default DataItem for a matchable type ID
*
* @since 1.9
* @since 2.5
*
* @param string $diType
*
* @return string|null
*/
public function getDefaultDataItemTypeId( $diType ) {
public function getDefaultDataItemByType( $typeId ) {

if ( isset( $this->defaultDataItemTypeIds[$diType] ) ) {
return $this->defaultDataItemTypeIds[$diType];
if ( isset( $this->defaultDataItemTypeMap[$typeId] ) ) {
return $this->defaultDataItemTypeMap[$typeId];
}

return null;
Expand Down Expand Up @@ -513,4 +503,19 @@ public function setOption( $key, $value ) {
$this->getOptions()->set( $key, $value );
}

private function registerLabels() {

foreach ( $this->extraneousLanguage->getDatatypeLabels() as $typeId => $typeLabel ) {
$this->registerTypeLabel( $typeId, $typeLabel );
}

foreach ( $this->extraneousLanguage->getDatatypeAliases() as $typeAlias => $typeId ) {
$this->registerDataTypeAlias( $typeId, $typeAlias );
}

foreach ( $this->extraneousLanguage->getCanonicalDatatypeLabels() as $label => $id ) {
$this->canonicalLabels[$id] = $label;
}
}

}
2 changes: 1 addition & 1 deletion src/DataValueFactory.php
Expand Up @@ -164,7 +164,7 @@ public function newDataValueByItem( DataItem $dataItem, DIProperty $property = n
if ( $property !== null ) {
$typeId = $property->findPropertyTypeID();
} else {
$typeId = $this->dataTypeRegistry->getDefaultDataItemTypeId( $dataItem->getDiType() );
$typeId = $this->dataTypeRegistry->getDefaultDataItemByType( $dataItem->getDiType() );
}

$dataValue = $this->newDataValueByType( $typeId, false, $caption, $property );
Expand Down
24 changes: 24 additions & 0 deletions src/ExtraneousLanguage/ExtraneousLanguage.php
Expand Up @@ -226,6 +226,30 @@ public function getDatatypeLabels() {
return $datatypeLabels;
}

/**
* @since 2.5
*
* @param string $label
*
* @return string
*/
public function findDatatypeByLabel( $label ) {

$label = mb_strtolower( $label );

$datatypeLabels = $this->getDatatypeLabels();
$datatypeLabels = array_flip( $datatypeLabels );
$datatypeLabels += $this->getDatatypeAliases();

foreach ( $datatypeLabels as $key => $id ) {
if ( mb_strtolower( $key ) === $label ) {
return $id;
}
}

return '';
}

/**
* @since 2.4
*
Expand Down
28 changes: 5 additions & 23 deletions tests/phpunit/Unit/DataTypeRegistryTest.php
Expand Up @@ -109,7 +109,7 @@ public function testRegisterDatatypeIdAndAlias() {
$this->dataTypeRegistry->registerDataTypeAlias( '_foo', 'FooBar' );

$this->assertTrue(
$this->dataTypeRegistry->isKnownTypeId( '_foo' )
$this->dataTypeRegistry->isKnownByType( '_foo' )
);

$this->assertEquals(
Expand All @@ -122,13 +122,13 @@ public function testRegisterDatatypeIdAndAlias() {
public function testGetDefaultDataItemTypeIdForValidDataItemType() {
$this->assertInternalType(
'string',
$this->dataTypeRegistry->getDefaultDataItemTypeId( 1 )
$this->dataTypeRegistry->getDefaultDataItemByType( 1 )
);
}

public function testGetDefaultDataItemTypeIdForInvalidDataItemType() {
$this->assertNull(
$this->dataTypeRegistry->getDefaultDataItemTypeId( 9999 )
$this->dataTypeRegistry->getDefaultDataItemByType( 9999 )
);
}

Expand Down Expand Up @@ -253,32 +253,14 @@ public function testLookupByLabelIsCaseInsensitive() {

public function testFindTypeIdByLanguage() {

$extraneousLanguage = $this->getMockBuilder( '\SMW\ExtraneousLanguage\ExtraneousLanguage' )
->disableOriginalConstructor()
->getMock();

$extraneousLanguage->expects( $this->once() )
->method( 'getDatatypeLabels' )
->will( $this->returnValue( array() ) );

$extraneousLanguage->expects( $this->once() )
->method( 'getDatatypeAliases' )
->will( $this->returnValue( array() ) );

$extraneousLanguage->expects( $this->once() )
->method( 'getCanonicalDatatypeLabels' )
->will( $this->returnValue( array() ) );

$instance = new DataTypeRegistry( $extraneousLanguage );

$this->assertSame(
'_num',
$instance->findTypeIdByLanguage( 'Número', 'es' )
$this->dataTypeRegistry->findTypeByLanguage( 'Número', 'es' )
);

$this->assertSame(
'_num',
$instance->findTypeIdByLanguage( '数值型', 'zh-Hans' )
$this->dataTypeRegistry->findTypeByLanguage( '数值型', 'zh-Hans' )
);
}

Expand Down

0 comments on commit 9d465f1

Please sign in to comment.