Jeroen De Dauw has uploaded a new change for review.
https://gerrit.wikimedia.org/r/49920
Change subject: Added toArray to selection requests
......................................................................
Added toArray to selection requests
Change-Id: I2b9bad32e365d3c84d248962cbb5103aac8ffae3
---
M includes/Ask/Language/Selection/PropertySelection.php
M includes/Ask/Language/Selection/SelectionRequest.php
M includes/Ask/Language/Selection/SubjectSelection.php
M tests/phpunit/AskTestCase.php
M tests/phpunit/Language/Description/DescriptionTest.php
M tests/phpunit/Language/Selection/SelectionRequestTest.php
6 files changed, 111 insertions(+), 20 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Ask
refs/changes/20/49920/1
diff --git a/includes/Ask/Language/Selection/PropertySelection.php
b/includes/Ask/Language/Selection/PropertySelection.php
index 02bab1d..ec16703 100644
--- a/includes/Ask/Language/Selection/PropertySelection.php
+++ b/includes/Ask/Language/Selection/PropertySelection.php
@@ -50,7 +50,7 @@
}
/**
- * @see Selection::getType
+ * @see SelectionRequest::getType
*
* @since 0.1
*
@@ -71,4 +71,17 @@
return $this->property;
}
+ /**
+ * @see SelectionRequest::getArrayValue
+ *
+ * @since 0.1
+ *
+ * @return mixed
+ */
+ public function getArrayValue() {
+ return array(
+ 'property' => $this->property->toArray()
+ );
+ }
+
}
diff --git a/includes/Ask/Language/Selection/SelectionRequest.php
b/includes/Ask/Language/Selection/SelectionRequest.php
index 1300227..e4ad14b 100644
--- a/includes/Ask/Language/Selection/SelectionRequest.php
+++ b/includes/Ask/Language/Selection/SelectionRequest.php
@@ -31,7 +31,7 @@
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
*/
-abstract class SelectionRequest {
+abstract class SelectionRequest implements \Ask\Arrayable {
const TYPE_PROP = 1;
const TYPE_SUBJECT = 2;
@@ -45,4 +45,34 @@
*/
public abstract function getType();
+ /**
+ * Returns the value in a format that contains only primitive values
+ * and arrays. This format is typically stable and easy to understand,
+ * and thus ideal for serialization such as json_encode.
+ *
+ * @since 0.1
+ *
+ * @return string
+ */
+ public abstract function getArrayValue();
+
+ /**
+ * @see \Ask\Arrayable::toArray
+ *
+ * This method has a more specific return format then
Arrayable::toArray.
+ * The return value is always an array that holds a type key pointing
+ * to string type identifier (the same one as obtained via ->getType())
+ * and a value key pointing to a mixed (though simple) value.
+ *
+ * @since 0.1
+ *
+ * @return array
+ */
+ public final function toArray() {
+ return array(
+ 'type' => $this->getType(),
+ 'value' => $this->getArrayValue(),
+ );
+ }
+
}
diff --git a/includes/Ask/Language/Selection/SubjectSelection.php
b/includes/Ask/Language/Selection/SubjectSelection.php
index 71eb687..d56bed8 100644
--- a/includes/Ask/Language/Selection/SubjectSelection.php
+++ b/includes/Ask/Language/Selection/SubjectSelection.php
@@ -31,7 +31,7 @@
class SubjectSelection extends SelectionRequest implements \Ask\Immutable {
/**
- * @see Selection::getType
+ * @see SelectionRequest::getType
*
* @since 0.1
*
@@ -41,4 +41,15 @@
return SelectionRequest::TYPE_SUBJECT;
}
+ /**
+ * @see SelectionRequest::getArrayValue
+ *
+ * @since 0.1
+ *
+ * @return mixed
+ */
+ public function getArrayValue() {
+ return null;
+ }
+
}
diff --git a/tests/phpunit/AskTestCase.php b/tests/phpunit/AskTestCase.php
index ec9f18b..6e071c5 100644
--- a/tests/phpunit/AskTestCase.php
+++ b/tests/phpunit/AskTestCase.php
@@ -50,4 +50,21 @@
);
}
+ protected function assertPrimitiveStructure( $value ) {
+ if ( is_array( $value ) ) {
+ if ( empty( $value ) ) {
+ $this->assertTrue( true );
+ }
+
+ foreach ( $value as $subValue ) {
+ $this->assertPrimitiveStructure( $subValue );
+ }
+ }
+ else {
+ $this->assertFalse( is_object( $value ), 'Value should
not be an object' );
+ $this->assertFalse( is_resource( $value ), 'Value
should not be a resource' );
+ $this->assertFalse( is_callable( $value ), 'Value
should not be a callable' );
+ }
+ }
+
}
diff --git a/tests/phpunit/Language/Description/DescriptionTest.php
b/tests/phpunit/Language/Description/DescriptionTest.php
index 43da1ca..23e96b2 100644
--- a/tests/phpunit/Language/Description/DescriptionTest.php
+++ b/tests/phpunit/Language/Description/DescriptionTest.php
@@ -114,23 +114,6 @@
$this->assertPrimitiveStructure( $array );
}
- protected function assertPrimitiveStructure( $value ) {
- if ( is_array( $value ) ) {
- if ( empty( $value ) ) {
- $this->assertTrue( true );
- }
-
- foreach ( $value as $subValue ) {
- $this->assertPrimitiveStructure( $subValue );
- }
- }
- else {
- $this->assertFalse( is_object( $value ), 'Value should
not be an object' );
- $this->assertFalse( is_resource( $value ), 'Value
should not be a resource' );
- $this->assertFalse( is_callable( $value ), 'Value
should not be a callable' );
- }
- }
-
/**
* @dataProvider instanceProvider
*
diff --git a/tests/phpunit/Language/Selection/SelectionRequestTest.php
b/tests/phpunit/Language/Selection/SelectionRequestTest.php
index 97bc7ad..2c5722b 100644
--- a/tests/phpunit/Language/Selection/SelectionRequestTest.php
+++ b/tests/phpunit/Language/Selection/SelectionRequestTest.php
@@ -58,4 +58,41 @@
$this->assertInternalType( 'integer', $request->getType() );
}
+ /**
+ * @dataProvider instanceProvider
+ *
+ * @since 0.1
+ *
+ * @param SelectionRequest $description
+ */
+ public function testReturnValueOfToArray( SelectionRequest $description
) {
+ $array = $description->toArray();
+
+ $this->assertInternalType( 'array', $array );
+ $this->assertArrayHasKey( 'type', $array );
+ $this->assertArrayHasKey( 'value', $array );
+ $this->assertCount( 2, $array );
+
+ $this->assertEquals(
+ array(
+ 'type' => $description->getType(),
+ 'value' => $description->getArrayValue(),
+ ),
+ $array
+ );
+ }
+
+ /**
+ * @dataProvider instanceProvider
+ *
+ * @since 0.1
+ *
+ * @param SelectionRequest $description
+ */
+ public function testReturnTypeOfGetArrayValue( SelectionRequest
$description ) {
+ $array = $description->getArrayValue();
+
+ $this->assertPrimitiveStructure( $array );
+ }
+
}
--
To view, visit https://gerrit.wikimedia.org/r/49920
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I2b9bad32e365d3c84d248962cbb5103aac8ffae3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Ask
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits