Denny Vrandecic has submitted this change and it was merged.

Change subject: Introduced TypedObjectDeserializer to avoid code duplication
......................................................................


Introduced TypedObjectDeserializer to avoid code duplication

Change-Id: I03dbd3321a7ddb66651bf98c8129cd8365bc4483
---
M includes/Ask/Deserializers/DescriptionDeserializer.php
M includes/Ask/Deserializers/SortExpressionDeserializer.php
A includes/Ask/Deserializers/TypedObjectDeserializer.php
3 files changed, 188 insertions(+), 109 deletions(-)

Approvals:
  Denny Vrandecic: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/Ask/Deserializers/DescriptionDeserializer.php 
b/includes/Ask/Deserializers/DescriptionDeserializer.php
index 0eb7de7..553b25d 100644
--- a/includes/Ask/Deserializers/DescriptionDeserializer.php
+++ b/includes/Ask/Deserializers/DescriptionDeserializer.php
@@ -27,7 +27,7 @@
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < jeroended...@gmail.com >
  */
-class DescriptionDeserializer implements Deserializer {
+class DescriptionDeserializer extends TypedObjectDeserializer {
 
        protected $dataValueFactory;
 
@@ -35,62 +35,40 @@
                $this->dataValueFactory = $dataValueFactory;
        }
 
-       public function deserialize( $serialization ) {
-               $this->assertCanDeserialize( $serialization );
-               return $this->getDeserializedDescription( $serialization );
+       /**
+        * @see TypedObjectDeserializer::getObjectType
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       protected function getObjectType() {
+               return 'description';
        }
 
-       protected function assertCanDeserialize( $serialization ) {
-               if ( !$this->hasObjectType( $serialization ) ) {
-                       throw new MissingTypeException( $this );
-               }
-
-               if ( !$this->hasCorrectObjectType( $serialization ) ) {
-                       throw new UnsupportedTypeException( 
$serialization['objectType'], $this );
-               }
+       /**
+        * @see TypedObjectDeserializer::getSubTypeKey
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       protected function getSubTypeKey() {
+               return 'descriptionType';
        }
 
-       public function canDeserialize( $serialization ) {
-               return $this->hasObjectType( $serialization ) && 
$this->hasCorrectObjectType( $serialization );
-       }
-
-       protected function hasCorrectObjectType( $serialization ) {
-               return $serialization['objectType'] === 'description';
-       }
-
-       protected function hasObjectType( $serialization ) {
-               return is_array( $serialization )
-                       && array_key_exists( 'objectType', $serialization );
-       }
-
-       protected function getDeserializedDescription( array $serialization ) {
-               if ( !array_key_exists( 'descriptionType', $serialization ) ) {
-                       throw new MissingAttributeException(
-                               'descriptionType',
-                               $this
-                       );
-               }
-
-               $this->requireAttribute( $serialization, 'descriptionType' );
-               $this->requireAttribute( $serialization, 'value' );
-               $this->assertAttributeIsArray( $serialization, 'value' );
-
-               $descriptionType = $serialization['descriptionType'];
-               $descriptionValue = $serialization['value'];
-
-               return $this->getDeserializedValue( $descriptionType, 
$descriptionValue );
-       }
-
-       protected function requireAttributes( array $array ) {
-               $requiredAttributes = func_get_args();
-               array_shift( $requiredAttributes );
-
-               foreach ( $requiredAttributes as $attribute ) {
-                       $this->requireAttribute( $array, $attribute );
-               }
-       }
-
-       protected function getDeserializedValue( $descriptionType, 
$descriptionValue ) {
+       /**
+        * @see TypedObjectDeserializer::getDeserializedValue
+        *
+        * @since 0.1
+        *
+        * @param string $descriptionType
+        * @param array $descriptionValue
+        *
+        * @return object
+        * @throws DeserializationException
+        */
+       protected function getDeserializedValue( $descriptionType, array 
$descriptionValue ) {
                if ( $descriptionType === 'anyValue' ) {
                        return new AnyValue();
                }
@@ -184,24 +162,6 @@
                }
 
                return $descriptions;
-       }
-
-       protected function requireAttribute( array $array, $attributeName ) {
-               if ( !array_key_exists( $attributeName, $array ) ) {
-                       throw new MissingAttributeException(
-                               $attributeName,
-                               $this
-                       );
-               }
-       }
-
-       protected function assertAttributeIsArray( array $array, $attributeName 
) {
-               if ( !is_array( $array[$attributeName] ) ) {
-                       throw new InvalidAttributeException(
-                               $attributeName,
-                               $this
-                       );
-               }
        }
 
 }
diff --git a/includes/Ask/Deserializers/SortExpressionDeserializer.php 
b/includes/Ask/Deserializers/SortExpressionDeserializer.php
index 29f7e39..7af3006 100644
--- a/includes/Ask/Deserializers/SortExpressionDeserializer.php
+++ b/includes/Ask/Deserializers/SortExpressionDeserializer.php
@@ -20,7 +20,7 @@
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < jeroended...@gmail.com >
  */
-class SortExpressionDeserializer implements Deserializer {
+class SortExpressionDeserializer extends TypedObjectDeserializer {
 
        protected $dataValueFactory;
 
@@ -28,53 +28,52 @@
                $this->dataValueFactory = $dataValueFactory;
        }
 
-       public function deserialize( $serialization ) {
-               $this->assertCanDeserialize( $serialization );
-               return $this->getDeserializedSortExpression( $serialization );
+       /**
+        * @see TypedObjectDeserializer::getObjectType
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       protected function getObjectType() {
+               return 'sortExpression';
        }
 
-       protected function assertCanDeserialize( $serialization ) {
-               if ( !$this->hasObjectType( $serialization ) ) {
-                       throw new MissingTypeException( $this );
-               }
-
-               if ( !$this->hasCorrectObjectType( $serialization ) ) {
-                       throw new UnsupportedTypeException( 
$serialization['objectType'], $this );
-               }
+       /**
+        * @see TypedObjectDeserializer::getSubTypeKey
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       protected function getSubTypeKey() {
+               return 'sortExpressionType';
        }
 
-       public function canDeserialize( $serialization ) {
-               return $this->hasObjectType( $serialization ) && 
$this->hasCorrectObjectType( $serialization );
-       }
-
-       protected function hasObjectType( $serialization ) {
-               return is_array( $serialization )
-               && array_key_exists( 'objectType', $serialization );
-       }
-
-       protected function hasCorrectObjectType( $serialization ) {
-               return $serialization['objectType'] === 'sortExpression';
-       }
-
-       protected function getDeserializedSortExpression( array $serialization 
) {
-               $this->requireAttribute( $serialization, 'sortExpressionType' );
-               $this->requireAttribute( $serialization, 'value' );
-               $this->assertAttributeIsArray( $serialization, 'value' );
-
-               if ( $serialization['sortExpressionType'] !== 'propertyValue' ) 
{
+       /**
+        * @see TypedObjectDeserializer::getDeserializedValue
+        *
+        * @since 0.1
+        *
+        * @param string $sortExpressionType
+        * @param array $valueSerialization
+        *
+        * @return object
+        * @throws DeserializationException
+        */
+       protected function getDeserializedValue( $sortExpressionType, array 
$valueSerialization ) {
+               if ( $sortExpressionType !== 'propertyValue' ) {
                        throw new InvalidAttributeException( 
'sortExpressionType', $this );
                }
 
-               $value = $serialization['value'];
-
-               $this->requireAttribute( $value, 'direction' );
-               $this->requireAttribute( $value, 'property' );
-               $this->assertAttributeIsArray( $value, 'property' );
+               $this->requireAttribute( $valueSerialization, 'direction' );
+               $this->requireAttribute( $valueSerialization, 'property' );
+               $this->assertAttributeIsArray( $valueSerialization, 'property' 
);
 
                try {
                        $expression = new PropertyValueSortExpression(
-                               $this->dataValueFactory->newFromArray( 
$value['property'] ),
-                               $value['direction']
+                               $this->dataValueFactory->newFromArray( 
$valueSerialization['property'] ),
+                               $valueSerialization['direction']
                        );
                }
                catch ( InvalidArgumentException $ex ) {
diff --git a/includes/Ask/Deserializers/TypedObjectDeserializer.php 
b/includes/Ask/Deserializers/TypedObjectDeserializer.php
new file mode 100644
index 0000000..98faa16
--- /dev/null
+++ b/includes/Ask/Deserializers/TypedObjectDeserializer.php
@@ -0,0 +1,120 @@
+<?php
+
+namespace Ask\Deserializers;
+
+use Ask\Deserializers\Exceptions\DeserializationException;
+use Ask\Deserializers\Exceptions\InvalidAttributeException;
+use Ask\Deserializers\Exceptions\MissingAttributeException;
+use Ask\Deserializers\Exceptions\MissingTypeException;
+use Ask\Deserializers\Exceptions\UnsupportedTypeException;
+
+/**
+ * @since 0.1
+ *
+ * @file
+ * @ingroup Ask
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+abstract class TypedObjectDeserializer implements Deserializer {
+
+       /**
+        * Returns the objectType supported by the implementation.
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       protected abstract function getObjectType();
+
+       /**
+        * Returns the name of the key used for the specific type of object.
+        * For instance "descriptionType" or "sortExpressionType".
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       protected abstract function getSubTypeKey();
+
+       /**
+        * Deserializes the value serialization into an object.
+        *
+        * @since 0.1
+        *
+        * @param string $sortExpressionType
+        * @param array $valueSerialization
+        *
+        * @return object
+        * @throws DeserializationException
+        */
+       protected abstract function getDeserializedValue( $sortExpressionType, 
array $valueSerialization );
+
+       public function deserialize( $serialization ) {
+               $this->assertCanDeserialize( $serialization );
+               return $this->getDeserialization( $serialization );
+       }
+
+       protected function assertCanDeserialize( $serialization ) {
+               if ( !$this->hasObjectType( $serialization ) ) {
+                       throw new MissingTypeException( $this );
+               }
+
+               if ( !$this->hasCorrectObjectType( $serialization ) ) {
+                       throw new UnsupportedTypeException( 
$serialization['objectType'], $this );
+               }
+       }
+
+       public function canDeserialize( $serialization ) {
+               return $this->hasObjectType( $serialization ) && 
$this->hasCorrectObjectType( $serialization );
+       }
+
+       protected function hasCorrectObjectType( $serialization ) {
+               return $serialization['objectType'] === $this->getObjectType();
+       }
+
+       protected function hasObjectType( $serialization ) {
+               return is_array( $serialization )
+                       && array_key_exists( 'objectType', $serialization );
+       }
+
+       protected function getDeserialization( array $serialization ) {
+               $this->requireAttribute( $serialization, $this->getSubTypeKey() 
);
+               $this->requireAttribute( $serialization, 'value' );
+               $this->assertAttributeIsArray( $serialization, 'value' );
+
+               $specificType = $serialization[$this->getSubTypeKey()];
+               $valueSerialization = $serialization['value'];
+
+               return $this->getDeserializedValue( $specificType, 
$valueSerialization );
+       }
+
+       protected function requireAttributes( array $array ) {
+               $requiredAttributes = func_get_args();
+               array_shift( $requiredAttributes );
+
+               foreach ( $requiredAttributes as $attribute ) {
+                       $this->requireAttribute( $array, $attribute );
+               }
+       }
+
+       protected function requireAttribute( array $array, $attributeName ) {
+               if ( !array_key_exists( $attributeName, $array ) ) {
+                       throw new MissingAttributeException(
+                               $attributeName,
+                               $this
+                       );
+               }
+       }
+
+       protected function assertAttributeIsArray( array $array, $attributeName 
) {
+               if ( !is_array( $array[$attributeName] ) ) {
+                       throw new InvalidAttributeException(
+                               $attributeName,
+                               $this
+                       );
+               }
+       }
+
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I03dbd3321a7ddb66651bf98c8129cd8365bc4483
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Ask
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: Denny Vrandecic <denny.vrande...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to