[pLog-svn] r5674 - in plog/branches/lifetype-1.2/class: data/validator test/tests/data/validator

oscar at devel.lifetype.net oscar at devel.lifetype.net
Wed Jul 18 16:29:59 EDT 2007


Author: oscar
Date: 2007-07-18 16:29:59 -0400 (Wed, 18 Jul 2007)
New Revision: 5674

Added:
   plog/branches/lifetype-1.2/class/test/tests/data/validator/arrayvalidator_test.class.php
Modified:
   plog/branches/lifetype-1.2/class/data/validator/arrayvalidator.class.php
Log:
Added Mark's patch to implement element validation in the ArrayValidator class with just one change: the element validation is now optional (to keep backwards compatibility with the rest of the API) and only peformed if an instance of a Validator class is passed to the constructor of ArrayValidator.


Modified: plog/branches/lifetype-1.2/class/data/validator/arrayvalidator.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/data/validator/arrayvalidator.class.php	2007-07-18 20:20:35 UTC (rev 5673)
+++ plog/branches/lifetype-1.2/class/data/validator/arrayvalidator.class.php	2007-07-18 20:29:59 UTC (rev 5674)
@@ -14,11 +14,46 @@
      */
     class ArrayValidator extends Validator
     {
-        function ArrayValidator()
+        var $_elementValidator;
+
+		/**
+		 * Constructor
+		 *
+		 * @param elementValidator An instance of the Validator class that will be used to validate
+		 * each one of the elements in the array, if any. If none is specified, then this class will 
+		 * only validate that the given valu is an array.
+		 *
+		 * When passing a Validator object, the given array will validate if and only if all 
+		 * the elements of the array pass the validation of the given validation class.
+		 */
+        function ArrayValidator( $elementValidator = null )
         {
             $this->Validator();
+			$this->_elementValidator = $elementValidator;
 
             $this->addRule(new ArrayRule());
         }
+
+        /**
+         * ArrayValidator's own validate, it will validate the array itself first, then
+         * validate each element.
+         *
+         * @param values The array that we're going to validate.
+         */
+        function validate($values)
+        {
+            $validateOk = parent::validate($values);
+            if( !$validateOk )
+            	return false;
+            	
+			if( $this->_elementValidator ) {
+	            foreach( $values as $value ) {
+	                if (!$this->_elementValidator->validate($value))
+	                     return false;
+	 	        }
+			}
+
+            return true;
+        }
     }
 ?>

Added: plog/branches/lifetype-1.2/class/test/tests/data/validator/arrayvalidator_test.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/test/tests/data/validator/arrayvalidator_test.class.php	                        (rev 0)
+++ plog/branches/lifetype-1.2/class/test/tests/data/validator/arrayvalidator_test.class.php	2007-07-18 20:29:59 UTC (rev 5674)
@@ -0,0 +1,48 @@
+<?php
+
+	lt_include( PLOG_CLASS_PATH."class/test/helpers/lifetypetestcase.class.php" );
+	lt_include( PLOG_CLASS_PATH."class/data/validator/arrayvalidator.class.php" );
+
+	/**
+	 * \ingroup Test
+	 *
+	 * Test case for the ArrayValidator class
+	 */
+	class ArrayValidator_Test extends LifeTypeTestCase
+	{
+		function setUp()
+		{
+			// create a username validator
+			$this->v = new ArrayValidator();
+		}
+		
+		function testEmptyArray()
+		{
+			$this->assertTrue( $this->v->validate(Array()));
+		}
+		
+		function testNotArray()
+		{
+			$this->assertFalse( $this->v->validate( "" ));
+		}
+		
+		/**
+		 * checks that by providing a validator object as the element validator,
+		 * all elements in the array are validated using the given class
+		 */
+		function testElementValidator()
+		{
+			lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
+			lt_include( PLOG_CLASS_PATH."class/data/validator/emailvalidator.class.php" );
+			
+			// valid array with integers
+			$intArrayValid = Array( 1, 2, 6, 10, 44 );
+			$v = new ArrayValidator( new IntegerValidator());
+			$this->assertTrue( $v->validate( $intArrayValid ));
+			
+			// invalid array with integers			
+			$intArrayNotValid = Array( 1, "4", "afasdf", 44 );			
+			$this->assertFalse( $v->validate( $intArrayNotValid ));
+		}
+	}
+?>
\ No newline at end of file



More information about the pLog-svn mailing list