Daniel Werner has submitted this change and it was merged.

Change subject: Added getType, toArray and getArrayValue methods to descriptions
......................................................................


Added getType, toArray and getArrayValue methods to descriptions

Change-Id: If3be44a4628307e948e60c79f2b268b412c5167c
---
M Ask.classes.php
A includes/Ask/Arrayable.php
M includes/Ask/Language/Description/AnyValue.php
M includes/Ask/Language/Description/Conjunction.php
M includes/Ask/Language/Description/Description.php
M includes/Ask/Language/Description/DescriptionCollection.php
M includes/Ask/Language/Description/Disjunction.php
M includes/Ask/Language/Description/SomeProperty.php
M includes/Ask/Language/Description/ValueDescription.php
M tests/phpunit/Language/Description/DescriptionTest.php
10 files changed, 264 insertions(+), 9 deletions(-)

Approvals:
  Daniel Werner: Verified; Looks good to me, approved
  Daniel Kinzler: Looks good to me, but someone else must approve
  jenkins-bot: Checked



diff --git a/Ask.classes.php b/Ask.classes.php
index 4a2a7c3..6b17b9f 100644
--- a/Ask.classes.php
+++ b/Ask.classes.php
@@ -16,6 +16,7 @@
        // PSR-0 compliant :)
 
        $classes = array(
+               'Ask\Arrayable',
                'Ask\Immutable',
 
                'Ask\Language\Description\AnyValue',
diff --git a/includes/Ask/Arrayable.php b/includes/Ask/Arrayable.php
new file mode 100644
index 0000000..e3ba24c
--- /dev/null
+++ b/includes/Ask/Arrayable.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Ask;
+
+/**
+ * Interface for objects that have a toArray method.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.1
+ *
+ * @file
+ * @ingroup Ask
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+interface Arrayable {
+
+       /**
+        * Returns a representation of the object in primitive form,
+        * using only primitive values and arrays. The return value
+        * is thus does not contain any objects and can be fed to
+        * json_encode and similar. The result should typically have
+        * an understandable and stable format.
+        *
+        * The returned value might contain an absolute or relative type
+        * identifier that can be used to construct an object from the
+        * return value.
+        *
+        * @since 0.1
+        *
+        * @return array|null|bool|int|float|string
+        */
+       public function toArray();
+
+};
\ No newline at end of file
diff --git a/includes/Ask/Language/Description/AnyValue.php 
b/includes/Ask/Language/Description/AnyValue.php
index 5ade57b..0ccde37 100644
--- a/includes/Ask/Language/Description/AnyValue.php
+++ b/includes/Ask/Language/Description/AnyValue.php
@@ -32,7 +32,7 @@
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
  */
-class AnyValue implements Description, \Ask\Immutable {
+class AnyValue extends  Description implements \Ask\Immutable {
 
        /**
         * {@inheritdoc}
@@ -56,4 +56,26 @@
                return 0;
        }
 
+       /**
+        * {@inheritdoc}
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       public function getType() {
+               return 'anyvalue';
+       }
+
+       /**
+        * {@inheritdoc}
+        *
+        * @since 0.1
+        *
+        * @return array|null|bool|int|float|string
+        */
+       public function getArrayValue() {
+               return null;
+       }
+
 }
\ No newline at end of file
diff --git a/includes/Ask/Language/Description/Conjunction.php 
b/includes/Ask/Language/Description/Conjunction.php
index e82621b..cbc721a 100644
--- a/includes/Ask/Language/Description/Conjunction.php
+++ b/includes/Ask/Language/Description/Conjunction.php
@@ -35,4 +35,15 @@
  */
 class Conjunction extends DescriptionCollection {
 
+       /**
+        * {@inheritdoc}
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       public function getType() {
+               return 'conjunction';
+       }
+
 }
\ No newline at end of file
diff --git a/includes/Ask/Language/Description/Description.php 
b/includes/Ask/Language/Description/Description.php
index c5cdb15..f767588 100644
--- a/includes/Ask/Language/Description/Description.php
+++ b/includes/Ask/Language/Description/Description.php
@@ -3,7 +3,7 @@
 namespace Ask\Language\Description;
 
 /**
- * Interface for query condition descriptions.
+ * Base class for query condition descriptions.
  *
  * Based on SMWDescription
  *
@@ -30,7 +30,7 @@
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
  */
-interface Description {
+abstract class Description implements \Ask\Arrayable {
 
        /**
         * Returns the size of the description.
@@ -39,7 +39,7 @@
         *
         * @return integer
         */
-       public function getSize();
+       public abstract function getSize();
 
        /**
         * Returns the depth of the description.
@@ -48,6 +48,45 @@
         *
         * @return integer
         */
-       public function getDepth();
+       public abstract function getDepth();
+
+       /**
+        * Returns a string identifier for the description's type.
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       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/Description/DescriptionCollection.php 
b/includes/Ask/Language/Description/DescriptionCollection.php
index d83f18f..82a06f7 100644
--- a/includes/Ask/Language/Description/DescriptionCollection.php
+++ b/includes/Ask/Language/Description/DescriptionCollection.php
@@ -28,7 +28,7 @@
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
  */
-abstract class DescriptionCollection implements Description, \Ask\Immutable {
+abstract class DescriptionCollection extends  Description implements 
\Ask\Immutable {
 
        /**
         * @since 0.1
@@ -97,4 +97,22 @@
                return $this->descriptions;
        }
 
+       /**
+        * {@inheritdoc}
+        *
+        * @since 0.1
+        *
+        * @return array|null|bool|int|float|string
+        */
+       public function getArrayValue() {
+               return array(
+                       'descriptions' => array_map(
+                               function( Description $description ) {
+                                       return $description->toArray();
+                               },
+                               $this->descriptions
+                       )
+               );
+       }
+
 }
diff --git a/includes/Ask/Language/Description/Disjunction.php 
b/includes/Ask/Language/Description/Disjunction.php
index edcec2b..243b6fd 100644
--- a/includes/Ask/Language/Description/Disjunction.php
+++ b/includes/Ask/Language/Description/Disjunction.php
@@ -35,5 +35,15 @@
  */
 class Disjunction extends DescriptionCollection {
 
+       /**
+        * {@inheritdoc}
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       public function getType() {
+               return 'disjunction';
+       }
 
 }
\ No newline at end of file
diff --git a/includes/Ask/Language/Description/SomeProperty.php 
b/includes/Ask/Language/Description/SomeProperty.php
index eff5f10..fc22ae8 100644
--- a/includes/Ask/Language/Description/SomeProperty.php
+++ b/includes/Ask/Language/Description/SomeProperty.php
@@ -36,7 +36,7 @@
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
  */
-class SomeProperty implements Description, \Ask\Immutable {
+class SomeProperty extends Description implements \Ask\Immutable {
 
        /**
         * @since 0.1
@@ -109,4 +109,29 @@
                return $this->description->getDepth() + 1;
        }
 
-}
\ No newline at end of file
+       /**
+        * {@inheritdoc}
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       public function getType() {
+               return 'someproperty';
+       }
+
+       /**
+        * {@inheritdoc}
+        *
+        * @since 0.1
+        *
+        * @return array|null|bool|int|float|string
+        */
+       public function getArrayValue() {
+               return array(
+                       'property' => $this->property->toArray(),
+                       'description' => $this->description->toArray(),
+               );
+       }
+
+}
diff --git a/includes/Ask/Language/Description/ValueDescription.php 
b/includes/Ask/Language/Description/ValueDescription.php
index f32c4c1..7255bfa 100644
--- a/includes/Ask/Language/Description/ValueDescription.php
+++ b/includes/Ask/Language/Description/ValueDescription.php
@@ -39,7 +39,7 @@
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
  */
-class ValueDescription implements Description, \Ask\Immutable {
+class ValueDescription extends  Description implements \Ask\Immutable {
 
        const COMP_EQUAL = 1;
        const COMP_LEQ = 2; // Less than or equal
@@ -127,4 +127,29 @@
                return 0;
        }
 
+       /**
+        * {@inheritdoc}
+        *
+        * @since 0.1
+        *
+        * @return array|null|bool|int|float|string
+        */
+       public function getArrayValue() {
+               return array(
+                       'value' => $this->value->toArray(),
+                       'comparator' => $this->comparator,
+               );
+       }
+
+       /**
+        * {@inheritdoc}
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       public function getType() {
+               return 'valuedescription';
+       }
+
 }
diff --git a/tests/phpunit/Language/Description/DescriptionTest.php 
b/tests/phpunit/Language/Description/DescriptionTest.php
index a55020a..391e471 100644
--- a/tests/phpunit/Language/Description/DescriptionTest.php
+++ b/tests/phpunit/Language/Description/DescriptionTest.php
@@ -77,4 +77,58 @@
                $this->assertEquals( $depth, $description->getDepth() );
        }
 
+       /**
+        * @dataProvider instanceProvider
+        *
+        * @since 0.1
+        *
+        * @param Description $description
+        */
+       public function testReturnValueOfToArray( Description $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 Description $description
+        */
+       public function testReturnTypeOfGetArrayValue( Description $description 
) {
+               $array = $description->getArrayValue();
+
+               $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' );
+               }
+       }
+
 }
\ No newline at end of file

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If3be44a4628307e948e60c79f2b268b412c5167c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Ask
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: John Erling Blad <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to