[pLog-svn] r5465 - in plog/branches/lifetype-1.2/class: config dao net test/tests/config

oscar at devel.lifetype.net oscar at devel.lifetype.net
Wed May 30 17:47:49 EDT 2007


Author: oscar
Date: 2007-05-30 17:47:49 -0400 (Wed, 30 May 2007)
New Revision: 5465

Added:
   plog/branches/lifetype-1.2/class/test/tests/config/properties_test.class.php
Modified:
   plog/branches/lifetype-1.2/class/config/properties.class.php
   plog/branches/lifetype-1.2/class/dao/blogsettings.class.php
   plog/branches/lifetype-1.2/class/net/request.class.php
Log:
The Properties class has been modified to allow to easily use the new filter classes whenever necessary. The modifications consist of:

- There is a new parameter available in the Properties::getValue($key,$defaultValue,$filterClass) that allows to specify an instance of a filter class to perform the filtering logic before the value is returned to the calling code. This third parameter is optional so as not to break all code that depends on the current signature of the Properties::getValue() method.

- The new method Properties::getFilteredValue($key,$filterClass,$defaultValue) does the same as Properties::getValue() when a third parameter is specified. It's not strictly necessary to use this method but it may make code semantically clearer.

- The new method Properties::registerFilter($key,$filterClass) permanently links the given key $key to the given filter class $filterClass so that we can make sure that every time Properties::getValue($key) is called, its value is *always* filtered through the specified filter class. This is useful in those situations where we cannot be sure where a specific value will be requested, so instead of tracking down all calls to getValue() to specify a third parameter as a filter, we can just call registerFilter() at the top of our action class and be sure that we will always obtain the filtered value, no matter where getValue() is called.

Since these changes have been made to the base Properties class, all classes extending it (BlogSettings, Request, etc) have inherited them with minor or no changes at all. The most (positively) affected class is the Request class because now we can easily do input filtering with just a few extra lines of code. 


Modified: plog/branches/lifetype-1.2/class/config/properties.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/config/properties.class.php	2007-05-30 21:34:15 UTC (rev 5464)
+++ plog/branches/lifetype-1.2/class/config/properties.class.php	2007-05-30 21:47:49 UTC (rev 5465)
@@ -11,6 +11,8 @@
 	{
 
 		var $_props;
+		
+		var $_keyFilters;
 
         /**
          * Constructor.
@@ -20,8 +22,8 @@
          */
 		function Properties( $values = null )
 		{
+			$this->_keyFilters = Array();
 			
-
             if( $values == null )
 				$this->_props = Array();
             else
@@ -45,16 +47,62 @@
          * @param key Key whose value we want to fetch
 		 * @param defaultValue value that we should return in case the one we're looking for
 		 * is empty or does not exist
+		 * @param filterClass An instance of an object implementing the FilterBase interface that
+		 * will be used to process the value before returning it.
          * @return Value associated to that key
          */
-		function getValue( $key, $defaultValue = null )
+		function getValue( $key, $defaultValue = null, $filterClass = null )
 		{
             if( !isset($this->_props[$key]) ) {
-                return $defaultValue;
+                $value = $defaultValue;
             } else {
-			    return $this->_props[$key];
+			    $value = $this->_props[$key];
             }
+
+			if( $filterClass  || isset( $this->_keyFilters[$key] )) {
+				// there's a filter class specified, so we should run the
+				// resulting value through it...
+				if( isset( $this->_keyFilters[$key] )) {
+					$filterClass = $this->_keyFilters[$key];
+					print("using filter: ".get_class($filterClass)." - key = ".$key."<br/>" );					
+				}
+					
+				$value = $filterClass->filter( $value );
+			}
+			
+			return( $value );
 		}
+		
+		/**
+		 * This method is an alias for Properties::getValue() but the filter
+		 * class is now a mandatory parameter
+		 *
+         * @param key Key whose value we want to fetch
+		 * @param filterClass An instance of an object implementing the FilterBase interface that
+		 * will be used to process the value before returning it.
+		 * @param defaultValue value that we should return in case the one we're looking for
+		 * is empty or does not exist
+         * @return Value associated to that key
+ 		 * @see Properties::getValue()
+ 		 */
+		function getFilteredValue( $key, $filterClass, $defaultValue = null )
+		{
+			return( $this->getValue( $key, $defaultVaule, $filterClass ));
+		}
+		
+		/**
+		 * Registers a filter class for the key "$key", so that 
+		 * every time Properties::getValue( "$key" ) is called to fetch the requested
+		 * value, the filter will be applied automatically without the need to specify
+		 * the filter class at every call
+		 *
+		 * @param key
+		 * @param filterClass
+		 */
+		function registerFilter( $key, &$filterClass ) 
+		{
+			$this->_keyFilters[$key] = $filterClass;
+		}
 
 		/**
 		 * Method overwritten from the Object class

Modified: plog/branches/lifetype-1.2/class/dao/blogsettings.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/dao/blogsettings.class.php	2007-05-30 21:34:15 UTC (rev 5464)
+++ plog/branches/lifetype-1.2/class/dao/blogsettings.class.php	2007-05-30 21:47:49 UTC (rev 5465)
@@ -47,7 +47,7 @@
 			$this->setValue( "time_offset", $config->getValue( "default_time_offset", DEFAULT_TIME_OFFSET ));
 		}
 		
-		function getValue( $key, $defaultValue = null )
+		function getValue( $key, $defaultValue = null, $filterClass = null )
 		{
 			// is it a plugin key?
 			if( substr( $key, 0, strlen( "plugin_" )) == "plugin_" ) {
@@ -57,7 +57,7 @@
 				if( GlobalPluginConfig::canOverride( $key ) == PLUGIN_SETTINGS_USER_CAN_OVERRIDE ) {
 					// load the value from our settings, but if it isn't available, then return whatever the global
 					// plugin settings say					
-					$value = parent::getValue( $key, GlobalPluginConfig::getValue( $key ));
+					$value = parent::getValue( $key, GlobalPluginConfig::getValue( $key ), $filterClass );
 				}
 				else {
 					$value = GlobalPluginConfig::getValue( $key );

Modified: plog/branches/lifetype-1.2/class/net/request.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/net/request.class.php	2007-05-30 21:34:15 UTC (rev 5464)
+++ plog/branches/lifetype-1.2/class/net/request.class.php	2007-05-30 21:47:49 UTC (rev 5465)
@@ -57,12 +57,13 @@
 		 *
 		 * @param key
 		 * @param defaultValue
+		 * @param filterClass
 		 * @return the value
 		 */
-		function getValue( $key, $defaultValue = null )
+		function getValue( $key, $defaultValue = null, $filterClass = null )
 		{
 			// get the value from the parent
-			$value = parent::getValue( $key, $defaultValue );
+			$value = parent::getValue( $key, $defaultValue, $filterClass );
 			
 			// now if magic quotes are enabled and the input parameter is not an array
 			// and the feature has not been disabled, then strip the slashes

Added: plog/branches/lifetype-1.2/class/test/tests/config/properties_test.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/test/tests/config/properties_test.class.php	                        (rev 0)
+++ plog/branches/lifetype-1.2/class/test/tests/config/properties_test.class.php	2007-05-30 21:47:49 UTC (rev 5465)
@@ -0,0 +1,45 @@
+<?php
+
+	lt_include( PLOG_CLASS_PATH."class/test/helpers/lifetypetestcase.class.php" );
+	lt_include( PLOG_CLASS_PATH."class/config/properties.class.php" );		
+
+	/**
+	 * \ingroup Test
+	 *
+	 * Test cases for the Properties class
+	 */
+	class Properties_Test extends LifeTypeTestCase
+	{
+		var $p;
+		
+		function setUp()
+		{
+			$this->p = new Properties();
+			$this->p->setValue( "test", "<b>this</b> has some <h1>HTML</h1> in it!" );
+		}
+		
+		/**
+		 * Tests the Properties::getValue() method using a filter class as a third
+		 * parameter
+		 */
+		function testGetValueWithFilterClass()
+		{
+			// when not using a filter class, we should get the value as is
+			$this->assertEquals( "<b>this</b> has some <h1>HTML</h1> in it!", $this->p->getValue( "test" ));
+			
+			// when using a filter class, the value should be returned filtered
+			lt_include( PLOG_CLASS_PATH."class/data/filter/htmlfilter.class.php" );
+			$this->assertEquals( "this has some HTML in it!", $this->p->getValue( "test", null, new HtmlFilter()));
+		}
+		
+		function testRegisterFilter()
+		{
+			// when not using a filter class, we should get the value as is
+			$this->assertEquals( "<b>this</b> has some <h1>HTML</h1> in it!", $this->p->getValue( "test" ));			
+			
+			// now register a filter for this key and check that the output is filtered
+			$this->p->registerFilter( "test", new HtmlFilter());
+			$this->assertEquals( "this has some HTML in it!", $this->p->getValue( "test" ));
+		}
+	}
+?>
\ No newline at end of file



More information about the pLog-svn mailing list