Mwjames has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/50211


Change subject: SMW\DISerializer support unit output
......................................................................

SMW\DISerializer support unit output

Change-Id: I17a3e04653b3f55c294559a06f6b6cbba10e2f8d
---
M includes/dataitems/DISerializer.php
M tests/phpunit/includes/dataitems/DISerializerTest.php
2 files changed, 41 insertions(+), 8 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki 
refs/changes/11/50211/1

diff --git a/includes/dataitems/DISerializer.php 
b/includes/dataitems/DISerializer.php
index c2848a0..25185d8 100644
--- a/includes/dataitems/DISerializer.php
+++ b/includes/dataitems/DISerializer.php
@@ -45,7 +45,7 @@
         *
         * @return mixed
         */
-       public static function getSerialization( SMWDataItem $dataItem ) {
+       public static function getSerialization( SMWDataItem $dataItem, 
$printRequest = null ) {
                switch ( $dataItem->getDIType() ) {
                        case SMWDataItem::TYPE_WIKIPAGE:
                                $title = $dataItem->getTitle();
@@ -57,7 +57,23 @@
                                );
                                break;
                        case SMWDataItem::TYPE_NUMBER:
-                               $result = $dataItem->getNumber();
+                               // dataitems and datavalues
+                               // Quantity is a datavalue type that belongs to 
dataitem
+                               // type number which means in order to identify 
the correct
+                               // unit, we have re-factor the corresponding 
datavalue otherwise
+                               // we will not be able to determine the unit
+                               // (unit is part of the datavalue object)
+                               if ( $printRequest !== null && 
$printRequest->getTypeID() === '_qty' ) {
+                                       $diProperty = 
$printRequest->getData()->getDataItem();
+                                       $dataValue = 
\SMWDataValueFactory::newDataItemValue( $dataItem, $diProperty );
+
+                                       $result = array(
+                                               'value' => 
$dataValue->getNumber(),
+                                               'unit' => $dataValue->getUnit()
+                                       );
+                               } else {
+                                       $result = $dataItem->getNumber();
+                               }
                                break;
                        case SMWDataItem::TYPE_GEO:
                                $result = $dataItem->getCoordinateSet();
@@ -102,15 +118,13 @@
 
                                if ( $printRequest->getMode() === 
SMWPrintRequest::PRINT_THIS ) {
                                        $dataItems = 
$resultAarray->getContent();
-                                       $result += self::getSerialization( 
array_shift( $dataItems ) );
-                               }
-                               else {
+                                       $result += self::getSerialization( 
array_shift( $dataItems ), $printRequest );
+                               } else {
                                        
$result['printouts'][$printRequest->getLabel()] = array_map(
                                                array( __CLASS__, 
'getSerialization' ),
-                                               $resultAarray->getContent()
+                                               $resultAarray->getContent(), 
array ( $printRequest )
                                        );
                                }
-
                        }
 
                        $results[$diWikiPage->getTitle()->getFullText()] = 
$result;
diff --git a/tests/phpunit/includes/dataitems/DISerializerTest.php 
b/tests/phpunit/includes/dataitems/DISerializerTest.php
index a0766d8..26ef0be 100644
--- a/tests/phpunit/includes/dataitems/DISerializerTest.php
+++ b/tests/phpunit/includes/dataitems/DISerializerTest.php
@@ -92,4 +92,23 @@
                $this->assertEquals( $queryResultToArray['printrequests'][1], 
$printrequests[1] );
 
        }
-}
\ No newline at end of file
+
+       /**
+        * @covers DISerializer::getSerialization
+        *
+        * @since  1.9
+        */
+       public function testGetSerialization( ) {
+
+               // Number
+               $dataItem = new \SMWDINumber( 1001 );
+               $results = DISerializer::getSerialization( $dataItem );
+               $this->assertEquals( $results, 1001 );
+
+               // Quantity
+               // Test the quantity here but after spending hours to figure out
+               // how to mock the $printRequest object which come to no 
fruitful
+               // success we miss out a test
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/50211
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I17a3e04653b3f55c294559a06f6b6cbba10e2f8d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to