Addshore has uploaded a new change for review.

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

Change subject: Add ValueValidatorFactory & Tests
......................................................................

Add ValueValidatorFactory & Tests

Basically a CP from ValueParserFactory & Tests

Bug: T104330
Change-Id: I958dc66d0f6e4ed7dabc8ed216c36679804ff924
---
A repo/includes/ValueValidatorFactory.php
A repo/tests/phpunit/includes/ValueValidatorFactoryTest.php
2 files changed, 162 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/64/222564/1

diff --git a/repo/includes/ValueValidatorFactory.php 
b/repo/includes/ValueValidatorFactory.php
new file mode 100644
index 0000000..c44891c
--- /dev/null
+++ b/repo/includes/ValueValidatorFactory.php
@@ -0,0 +1,124 @@
+<?php
+
+namespace Wikibase\Repo;
+
+use InvalidArgumentException;
+use LogicException;
+use OutOfBoundsException;
+use ValueValidators\ValueValidator;
+
+/**
+ * Builds ValueValidator objects
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Adam Shorland
+ */
+class ValueValidatorFactory {
+
+       /**
+        * Maps validator id to ValueValidator class or builder callback.
+        *
+        * @since 0.5
+        *
+        * @var array
+        */
+       protected $validators = array();
+
+       /**
+        * @since 0.5
+        *
+        * @param string|callable[] $valueValidators An associative array 
mapping validator ids to
+        *        class names or callable builders.
+        *
+        * @throws InvalidArgumentException
+        */
+       public function __construct( array $valueValidators ) {
+               foreach ( $valueValidators as $validatorId => $validatorBuilder 
) {
+                       if ( !is_string( $validatorId ) ) {
+                               throw new InvalidArgumentException( 'Validator 
id needs to be a string' );
+                       }
+
+                       if ( !is_string( $validatorBuilder ) && !is_callable( 
$validatorBuilder ) ) {
+                               throw new InvalidArgumentException( 'Validator 
class needs to be a class name or callable' );
+                       }
+
+                       $this->validators[$validatorId] = $validatorBuilder;
+               }
+       }
+
+       /**
+        * Returns the ValueValidator identifiers.
+        *
+        * @since 0.5
+        *
+        * @return string[]
+        */
+       public function getValidatorIds() {
+               return array_keys( $this->validators );
+       }
+
+       /**
+        * Returns the validator builder (class name or callable) for 
$validatorId, or null if
+        * no builder was registered for that id.
+        *
+        * @since 0.5
+        *
+        * @param string $validatorId
+        *
+        * @return string|callable|null
+        */
+       public function getValidatorBuilder( $validatorId ) {
+               if ( array_key_exists( $validatorId, $this->validators ) ) {
+                       return $this->validators[$validatorId];
+               }
+
+               return null;
+       }
+
+       /**
+        * Returns an instance of the ValueValidator with the provided id or 
null if there is no such ValueValidator.
+        *
+        * @since 0.5
+        *
+        * @param string $validatorId
+        *
+        * @throws OutOfBoundsException If no validator was registered for 
$validatorId
+        * @return ValueValidator
+        */
+       public function newValidator( $validatorId ) {
+               if ( !array_key_exists( $validatorId, $this->validators ) ) {
+                       throw new OutOfBoundsException( "No builder registered 
for validator ID $validatorId" );
+               }
+
+               $builder = $this->validators[$validatorId];
+               $validator = $this->instantiateValidator( $builder );
+
+               return $validator;
+       }
+
+       /**
+        * @param string|callable $builder Either a classname of an 
implementation of ValueValidator,
+        *        or a callable that returns a ValueValidator.
+        *
+        * @throws LogicException if the builder did not create a ValueValidator
+        * @return ValueValidator
+        */
+       private function instantiateValidator( $builder ) {
+               if ( is_string( $builder ) ) {
+                       $validator = new $builder();
+               } else {
+                       $validator = call_user_func( $builder );
+               }
+
+               if ( !( $validator instanceof ValueValidator ) ) {
+                       throw new LogicException(
+                               'Invalid validator builder, did not create an 
instance of ValueValidator.'
+                       );
+               }
+
+               return $validator;
+       }
+
+}
diff --git a/repo/tests/phpunit/includes/ValueValidatorFactoryTest.php 
b/repo/tests/phpunit/includes/ValueValidatorFactoryTest.php
new file mode 100644
index 0000000..d9f16f7
--- /dev/null
+++ b/repo/tests/phpunit/includes/ValueValidatorFactoryTest.php
@@ -0,0 +1,38 @@
+<?php
+
+namespace Wikibase\Tests\Repo;
+
+use Wikibase\Repo\ValueValidatorFactory;
+
+/**
+ * @covers Wikibase\Repo\ValueValidatorFactory
+ *
+ * @group Wikibase
+ * @group WikibaseRepo
+ * @group WikibaseRepoTest
+ *
+ * @licence GNU GPL v2+
+ * @author Adam Shorland
+ */
+class ValueValidatorFactoryTest extends \MediaWikiTestCase {
+
+       /**
+        * @dataProvider getValidatorIdsProvider
+        */
+       public function testGetValidatorIds( $valueValidators, $expected ) {
+               $valueValidatorFactory = new ValueValidatorFactory( 
$valueValidators );
+
+               $returnValue = $valueValidatorFactory->getValidatorIds();
+
+               $this->assertEquals( $expected, $returnValue );
+       }
+
+       public function getValidatorIdsProvider() {
+               return array(
+                       array(
+                               array( 'key' => 'value' ),
+                               array( 'key' )
+                       )
+               );
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I958dc66d0f6e4ed7dabc8ed216c36679804ff924
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>

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

Reply via email to