Skip to content

Commit

Permalink
NumberValueFormatter service, refs 2275
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Mar 5, 2017
1 parent 8f5c4e8 commit 9cc1fe3
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 43 deletions.
15 changes: 10 additions & 5 deletions includes/datavalues/SMW_DV_Number.php
Expand Up @@ -42,6 +42,11 @@
*/
class SMWNumberValue extends SMWDataValue {

/**
* DV identifier
*/
const TYPE_ID = '_num';

/**
* Internal state to ensure no precision limitation is applied to an output
*/
Expand Down Expand Up @@ -257,7 +262,7 @@ public function getNormalizedFormattedNumber( $value ) {
* @return string
*/
public function getShortWikiText( $linker = null ) {
return $this->getDataValueFormatter()->format( DataValueFormatter::WIKI_SHORT, $linker );
return $this->dataValueServiceFactory->getValueFormatter( $this )->format( DataValueFormatter::WIKI_SHORT, $linker );
}

/**
Expand All @@ -266,7 +271,7 @@ public function getShortWikiText( $linker = null ) {
* @return string
*/
public function getShortHTMLText( $linker = null ) {
return $this->getDataValueFormatter()->format( DataValueFormatter::HTML_SHORT, $linker );
return $this->dataValueServiceFactory->getValueFormatter( $this )->format( DataValueFormatter::HTML_SHORT, $linker );
}

/**
Expand All @@ -275,7 +280,7 @@ public function getShortHTMLText( $linker = null ) {
* @return string
*/
public function getLongWikiText( $linker = null ) {
return $this->getDataValueFormatter()->format( DataValueFormatter::WIKI_LONG, $linker );
return $this->dataValueServiceFactory->getValueFormatter( $this )->format( DataValueFormatter::WIKI_LONG, $linker );
}

/**
Expand All @@ -284,15 +289,15 @@ public function getLongWikiText( $linker = null ) {
* @return string
*/
public function getLongHTMLText( $linker = null ) {
return $this->getDataValueFormatter()->format( DataValueFormatter::HTML_LONG, $linker );
return $this->dataValueServiceFactory->getValueFormatter( $this )->format( DataValueFormatter::HTML_LONG, $linker );
}

public function getNumber() {
return $this->isValid() ? $this->m_dataitem->getNumber() : 32202;
}

public function getWikiValue() {
return $this->getDataValueFormatter()->format( DataValueFormatter::VALUE );
return $this->dataValueServiceFactory->getValueFormatter( $this )->format( DataValueFormatter::VALUE );
}

/**
Expand Down
5 changes: 5 additions & 0 deletions includes/datavalues/SMW_DV_Quantity.php
Expand Up @@ -18,6 +18,11 @@
*/
class SMWQuantityValue extends SMWNumberValue {

/**
* DV identifier
*/
const TYPE_ID = '_qty';

/**
* Array with format (canonical unit ID string) => (conversion factor)
* @var float[]|bool
Expand Down
7 changes: 6 additions & 1 deletion src/DataValues/TemperatureValue.php
Expand Up @@ -18,11 +18,16 @@
*/
class TemperatureValue extends NumberValue {

/**
* DV identifier
*/
const TYPE_ID = '_tem';

/**
* @param string $typeid
*/
public function __construct( $typeid = '' ) {
parent::__construct( '_tem' );
parent::__construct( self::TYPE_ID );
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/DataValues/TypeList.php
Expand Up @@ -5,6 +5,8 @@
use SMWDataItem as DataItem;
use SMWPropertyValue as PropertyValue;
use SMWStringValue as StringValue;
use SMWQuantityValue as QuantityValue;
use SMWNumberValue as NumberValue;

/**
* @private
Expand Down Expand Up @@ -60,9 +62,9 @@ public static function getList() {
// Form page type for Semantic Forms
'_wpf' => array( 'SMWWikiPageValue', DataItem::TYPE_WIKIPAGE, false ),
// Number type
'_num' => array( 'SMWNumberValue', DataItem::TYPE_NUMBER, false ),
NumberValue::TYPE_ID => array( NumberValue::class, DataItem::TYPE_NUMBER, false ),
// Temperature type
'_tem' => array( TemperatureValue::class, DataItem::TYPE_NUMBER, false ),
TemperatureValue::TYPE_ID => array( TemperatureValue::class, DataItem::TYPE_NUMBER, false ),
// Time type
'_dat' => array( 'SMWTimeValue', DataItem::TYPE_TIME, false ),
// Boolean type
Expand All @@ -78,7 +80,7 @@ public static function getList() {
// External identifier
ExternalIdentifierValue::TYPE_ID => array( ExternalIdentifierValue::class, DataItem::TYPE_BLOB, false ),
// Type for numbers with units of measurement
'_qty' => array( 'SMWQuantityValue', DataItem::TYPE_NUMBER, false ),
QuantityValue::TYPE_ID => array( QuantityValue::class, DataItem::TYPE_NUMBER, false ),
// Special types are not avaialble directly for users (and have no local language name):
// Special type page type
'__typ' => array( 'SMWTypesValue', DataItem::TYPE_URI, false ),
Expand Down
6 changes: 5 additions & 1 deletion src/Services/DataValueServiceFactory.php
Expand Up @@ -12,6 +12,7 @@
use SMW\DataValues\ValueFormatters\StringValueFormatter;
use SMW\DataValues\ValueFormatters\TimeValueFormatter;
use SMWStringValue as StringValue;
use SMWNumberValue as NumberValue;

/**
* @private
Expand Down Expand Up @@ -169,7 +170,10 @@ private function newDispatchingDataValueFormatter() {
$this->containerBuilder->singleton( self::TYPE_FORMATTER . StringValue::TYPE_ID )
);

$dispatchingDataValueFormatter->addDefaultDataValueFormatter( new NumberValueFormatter() );
$dispatchingDataValueFormatter->addDefaultDataValueFormatter(
$this->containerBuilder->singleton( self::TYPE_FORMATTER . NumberValue::TYPE_ID )
);

$dispatchingDataValueFormatter->addDefaultDataValueFormatter( new TimeValueFormatter() );
$dispatchingDataValueFormatter->addDefaultDataValueFormatter( new NoValueFormatter() );

Expand Down
22 changes: 22 additions & 0 deletions src/Services/DataValueServices.php
Expand Up @@ -24,6 +24,9 @@
use SMW\DataValues\ValueValidators\PatternConstraintValueValidator;
use SMW\DataValues\ValueValidators\AllowsListConstraintValueValidator;
use SMW\DataValues\ValueValidators\PropertySpecificationConstraintValueValidator;
use SMWNumberValue as NumberValue;
use SMWQuantityValue as QuantityValue;
use SMW\DataValues\ValueFormatters\NumberValueFormatter;

/**
* @codeCoverageIgnore
Expand Down Expand Up @@ -243,4 +246,23 @@
return new MonolingualTextValueFormatter();
},

/**
* NumberValueFormatter
*
* @return callable
*/
DataValueServiceFactory::TYPE_FORMATTER . QuantityValue::TYPE_ID => function( $containerBuilder ) {
return $containerBuilder->create( DataValueServiceFactory::TYPE_FORMATTER . NumberValue::TYPE_ID );
},

DataValueServiceFactory::TYPE_FORMATTER . NumberValue::TYPE_ID => function( $containerBuilder ) {

$containerBuilder->registerExpectedReturnType(
DataValueServiceFactory::TYPE_FORMATTER . NumberValue::TYPE_ID,
NumberValueFormatter::class
);

return new NumberValueFormatter();
},

);
33 changes: 3 additions & 30 deletions tests/phpunit/Unit/DataValues/InfoLinksProviderTest.php
Expand Up @@ -9,7 +9,6 @@
use SMW\Tests\TestEnvironment;
use SMWNumberValue as NumberValue;
use SMWStringValue as StringValue;
use SMW\DataValues\ValueFormatters\StringValueFormatter;

/**
* @covers \SMW\DataValues\InfoLinksProvider
Expand Down Expand Up @@ -77,11 +76,7 @@ public function testGetInfolinkTextOnNumberValue() {
->method( 'getPropertyValues' )
->will( $this->returnValue( array() ) );

$numberValue = new NumberValue();

$numberValue->setDataValueServiceFactory(
$this->dataValueServiceFactory
);
$numberValue = $this->dataValueFactory->newDataValueByType( NumberValue::TYPE_ID );

$numberValue->setOption( 'user.language', 'en' );
$numberValue->setOption( 'content.language', 'en' );
Expand Down Expand Up @@ -115,18 +110,7 @@ public function testGetInfolinkTextOnStringValue() {
->method( 'getPropertyValues' )
->will( $this->returnValue( array() ) );

$stringValue = new StringValue( '_txt' );

$stringValueFormatter = new StringValueFormatter();
$stringValueFormatter->setDataValue( $stringValue );

$this->dataValueServiceFactory->expects( $this->any() )
->method( 'getValueFormatter' )
->will( $this->returnValue( $stringValueFormatter ) );

$stringValue->setDataValueServiceFactory(
$this->dataValueServiceFactory
);
$stringValue = $this->dataValueFactory->newDataValueByType( StringValue::TYPE_ID );

$stringValue->setOption( 'user.language', 'en' );
$stringValue->setOption( 'content.language', 'en' );
Expand Down Expand Up @@ -216,18 +200,7 @@ public function testGetInfolinkTextOnStringValueWithServiceLinks() {
'SERVICELINK-A|SERVICELINK-B'
);

$stringValue = new StringValue( '_txt' );

$stringValueFormatter = new StringValueFormatter();
$stringValueFormatter->setDataValue( $stringValue );

$this->dataValueServiceFactory->expects( $this->any() )
->method( 'getValueFormatter' )
->will( $this->returnValue( $stringValueFormatter ) );

$stringValue->setDataValueServiceFactory(
$this->dataValueServiceFactory
);
$stringValue = $this->dataValueFactory->newDataValueByType( StringValue::TYPE_ID );

$stringValue->setOption( StringValue::OPT_USER_LANGUAGE, 'en' );
$stringValue->setOption( StringValue::OPT_CONTENT_LANGUAGE, 'en' );
Expand Down
62 changes: 62 additions & 0 deletions tests/phpunit/Unit/DataValues/TemperatureValueTest.php
Expand Up @@ -5,6 +5,7 @@
use SMW\DataItemFactory;
use SMW\DataValues\TemperatureValue;
use SMW\Tests\TestEnvironment;
use SMW\DataValues\ValueFormatters\NumberValueFormatter;

/**
* @covers \SMW\DataValues\TemperatureValue
Expand All @@ -20,8 +21,11 @@ class TemperatureValueTest extends \PHPUnit_Framework_TestCase {
private $testEnvironment;
private $dataItemFactory;
private $propertySpecificationLookup;
private $dataValueServiceFactory;

protected function setUp() {
parent::setUp();

$this->testEnvironment = new TestEnvironment();
$this->dataItemFactory = new DataItemFactory();

Expand All @@ -30,6 +34,18 @@ protected function setUp() {
->getMock();

$this->testEnvironment->registerObject( 'PropertySpecificationLookup', $this->propertySpecificationLookup );

$constraintValueValidator = $this->getMockBuilder( '\SMW\DataValues\ValueValidators\ConstraintValueValidator' )
->disableOriginalConstructor()
->getMock();

$this->dataValueServiceFactory = $this->getMockBuilder( '\SMW\Services\DataValueServiceFactory' )
->disableOriginalConstructor()
->getMock();

$this->dataValueServiceFactory->expects( $this->any() )
->method( 'getConstraintValueValidator' )
->will( $this->returnValue( $constraintValueValidator ) );
}

protected function tearDown() {
Expand All @@ -47,6 +63,18 @@ public function testCanConstruct() {
public function testSetUserValueToReturnKelvinForAnyNonPreferredDisplayUnit() {

$instance = new TemperatureValue();

$numberValueFormatter = new NumberValueFormatter();
$numberValueFormatter->setDataValue( $instance );

$this->dataValueServiceFactory->expects( $this->any() )
->method( 'getValueFormatter' )
->will( $this->returnValue( $numberValueFormatter ) );

$instance->setDataValueServiceFactory(
$this->dataValueServiceFactory
);

$instance->setUserValue( '100 掳C' );

$this->assertContains(
Expand All @@ -70,6 +98,18 @@ public function testSetUserValueToReturnKelvinForAnyNonPreferredDisplayUnit() {
public function testSetUserValueOnUnknownUnit() {

$instance = new TemperatureValue();

$numberValueFormatter = new NumberValueFormatter();
$numberValueFormatter->setDataValue( $instance );

$this->dataValueServiceFactory->expects( $this->any() )
->method( 'getValueFormatter' )
->will( $this->returnValue( $numberValueFormatter ) );

$instance->setDataValueServiceFactory(
$this->dataValueServiceFactory
);

$instance->setUserValue( '100 Unknown' );

$this->assertContains(
Expand All @@ -86,6 +126,17 @@ public function testSetUserValueToReturnOnPreferredDisplayUnit() {

$instance = new TemperatureValue();

$numberValueFormatter = new NumberValueFormatter();
$numberValueFormatter->setDataValue( $instance );

$this->dataValueServiceFactory->expects( $this->any() )
->method( 'getValueFormatter' )
->will( $this->returnValue( $numberValueFormatter ) );

$instance->setDataValueServiceFactory(
$this->dataValueServiceFactory
);

$instance->setProperty(
$this->dataItemFactory->newDIProperty( 'Foo' )
);
Expand Down Expand Up @@ -116,6 +167,17 @@ public function testSetUserValueToReturnOnPreferredDisplayPrecision() {

$instance = new TemperatureValue();

$numberValueFormatter = new NumberValueFormatter();
$numberValueFormatter->setDataValue( $instance );

$this->dataValueServiceFactory->expects( $this->any() )
->method( 'getValueFormatter' )
->will( $this->returnValue( $numberValueFormatter ) );

$instance->setDataValueServiceFactory(
$this->dataValueServiceFactory
);

$instance->setProperty(
$this->dataItemFactory->newDIProperty( 'Foo' )
);
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/Unit/Services/DataValueServiceFactoryTest.php
Expand Up @@ -76,7 +76,7 @@ public function testGetValueParser() {
$instance->getValueParser( $dataValue );
}

public function testGetValueFormatterOnRegistered() {
public function testGetValueFormatterOnRegisteredFormatters() {

$dataValueFormatter = $this->getMockBuilder( '\SMW\DataValues\ValueFormatters\DataValueFormatter' )
->disableOriginalConstructor()
Expand All @@ -102,7 +102,7 @@ public function testGetValueFormatterOnRegistered() {
$instance->getValueFormatter( $dataValue );
}

public function testGetValueFormatterOnNonRegistered() {
public function testGetValueFormatterOnNonRegisteredFormatters() {

$dataValueFormatter = $this->getMockBuilder( '\SMW\DataValues\ValueFormatters\DataValueFormatter' )
->disableOriginalConstructor()
Expand All @@ -116,7 +116,7 @@ public function testGetValueFormatterOnNonRegistered() {
->method( 'isRegistered' )
->will( $this->returnValue( false ) );

$this->containerBuilder->expects( $this->once() )
$this->containerBuilder->expects( $this->atLeastOnce() )
->method( 'singleton' )
->with( $this->stringContains( DataValueServiceFactory::TYPE_FORMATTER ) )
->will( $this->returnValue( $dataValueFormatter ) );
Expand Down
Expand Up @@ -23,6 +23,8 @@
use SMW\DataValues\ValueFormatters\MonolingualTextValueFormatter;
use SMW\DataValues\MonolingualTextValue;
use SMW\DataValues\ValueParsers\MonolingualTextValueParser;
use SMWNumberValue as NumberValue;
use SMW\DataValues\ValueFormatters\NumberValueFormatter;

/**
* @group semantic-mediawiki
Expand Down Expand Up @@ -140,6 +142,12 @@ public function servicesProvider() {
MonolingualTextValueFormatter::class
);

$provider[] = array(
DataValueServiceFactory::TYPE_FORMATTER . NumberValue::TYPE_ID,
array(),
NumberValueFormatter::class
);

return $provider;
}

Expand Down

0 comments on commit 9cc1fe3

Please sign in to comment.