[xoops-cvslog 1971] CVS update: xoops2jp/html/class

Zurück zum Archiv-Index

Minahito minah****@users*****
2006年 1月 27日 (金) 17:02:42 JST


Index: xoops2jp/html/class/XCube_ActionForm.class.php
diff -u xoops2jp/html/class/XCube_ActionForm.class.php:1.1.2.24 xoops2jp/html/class/XCube_ActionForm.class.php:1.1.2.25
--- xoops2jp/html/class/XCube_ActionForm.class.php:1.1.2.24	Thu Jan 26 20:19:45 2006
+++ xoops2jp/html/class/XCube_ActionForm.class.php	Fri Jan 27 17:02:42 2006
@@ -1,4 +1,6 @@
 <?php
+
+require_once XOOPS_ROOT_PATH . "/class/XCube_Validator.class.php";
 require_once XOOPS_ROOT_PATH . "/class/XCube_FormFile.class.php";
 
 //
@@ -142,7 +144,7 @@
 		
 		foreach(array_keys($this->mFormProperties) as $name) {
 			if(isset($this->mFieldProperties[$name])) {
-				$this->mFieldProperties[$name]->validate($this->getVar($name));
+				$this->mFieldProperties[$name]->validate($this->mFormProperties[$name]);
 			}
 		}
 
@@ -174,7 +176,33 @@
 	}
 }
 
-class XCube_AbstractProperty
+class XCube_PropertyInterface
+{
+	function XCube_PropertyInterface($name)
+	{
+	}
+	
+	function fetch($key = null)
+	{
+	}
+	
+	function setValue($arg0 = null, $arg1 = null)
+	{
+	}
+
+	function getValue($arg0 = null)
+	{
+	}
+	
+	/**
+	 * @return bool
+	 */
+	function isArray()
+	{
+	}
+}
+
+class XCube_AbstractProperty extends XCube_PropertyInterface
 {
 	var $mName=null;
 	var $mValue=null;
@@ -184,53 +212,76 @@
 		$this->mName=$name;
 	}
 
-	function setValue($val)
+	function setValue($value, $dmy = null)
 	{
-		$this->mValue=$val;
+		$this->mValue = $value;
 	}
 
-	function getValue($index=null)
+	function getValue($index = null)
 	{
 		return $this->mValue;
 	}
 
-	function fetch()
+	function fetch($key = null)
 	{
-		if(isset($_REQUEST[$this->mName])&&!is_array($_REQUEST[$this->mName])) {
-		    if (get_magic_quotes_gpc()) {
-				$this->mValue=stripslashes($_REQUEST[$this->mName]);
-			}
-			else {
-				$this->mValue=$_REQUEST[$this->mName];
-			}
+		$value = null;
+		
+		if ($key != null && is_array($_REQUEST[$this->mName]) && isset($_REQUEST[$this->mName][$key])) {
+			$value = $_REQUEST[$this->mName][$key];
+		}
+		elseif (isset($_REQUEST[$this->mName]) && !is_array($_REQUEST[$this->mName])) {
+			$value = $_REQUEST[$this->mName];
+		}
+		
+	    if (get_magic_quotes_gpc()) {
+			$this->mValue = stripslashes($value);
 		}
-		else
-			$this->mValue=null;
+		else {
+			$this->mValue = $value;
+		}
+	}
+	
+	function isArray()
+	{
+		return false;
+	}
+	
+	function isNull()
+	{
+		return (strlen(trim($this->mValue)) == 0);
+	}
+	
+	function toNumber()
+	{
+		return $this->mValue;
+	}
+	
+	function toString()
+	{
+		return $this->mValue;
 	}
 }
 
-class XCube_AbstractArrayProperty extends XCube_AbstractProperty
+class XCube_AbstractArrayProperty extends XCube_PropertyInterface
 {
+	var $mName = null;
+	var $mProperties = array();
+	
+	var $mPropertyClassName = null;
+	
 	function XCube_AbstractArrayProperty($name)
 	{
-		parent::XCube_AbstractProperty($name);
-		$this->mValue=array();
+		$this->mName = $name;
 	}
 	
-	function fetch()
+	function fetch($key = null)
 	{
-		if(isset($_REQUEST[$this->mName])&&is_array($_REQUEST[$this->mName])) {
-			foreach($_REQUEST[$this->mName] as $key=>$val) {
-			    if (get_magic_quotes_gpc()) {
-					$this->mValue[$key]=stripcslashes($val);
-				}
-				else {
-					$this->mValue[$key]=$val;
-				}
+		if (isset($_REQUEST[$this->mName]) && is_array($_REQUEST[$this->mName])) {
+			foreach ($_REQUEST[$this->mName] as $_key => $_val) {
+				$this->mProperties[$_key] =& new $this->mPropertyClassName($this->mName);
+				$this->mPropertyClassName->fetch($_key);
 			}
 		}
-		else
-			$this->mValue=array();
 	}
 
 	function setValue($index, $value)
@@ -245,7 +296,11 @@
 
 		return isset($this->mValue[$index]) ? $this->mValue[$index] : null;
 	}
-
+	
+	function isArray()
+	{
+		return true;
+	}
 }
 
 class XCube_BoolProperty extends XCube_AbstractProperty
@@ -253,29 +308,18 @@
 	function fetch()
 	{
 		parent::fetch();
-		if(strlen($this->mValue)>0) {
-			$this->mValue=(intval($this->mValue)>0) ? 1 : 0;
+		if (strlen(trim($this->mValue)) > 0) {
+			$this->mValue = (intval($this->mValue)>0) ? 1 : 0;
 		}
 		else {
-			$this->mValue=0;
+			$this->mValue = 0;
 		}
 	}
 }
 
 class XCube_BoolArrayProperty extends XCube_AbstractArrayProperty
 {
-	function fetch()
-	{
-		parent::fetch();
-		foreach(array_keys($this->mValue) as $key) {
-			if(strlen($this->mValue[$key])>0) {
-				$this->mValue[$key]=(intval($this->mValue)>0) ? 1 : 0;
-			}
-			else {
-				$this->mValue[$key]=0;
-			}
-		}
-	}
+	var $mPropertyClassName = "XCube_BoolProperty";
 }
 
 class XCube_IntProperty extends XCube_AbstractProperty
@@ -283,29 +327,18 @@
 	function fetch()
 	{
 		parent::fetch();
-		if(strlen($this->mValue)>0) {
-			$this->mValue=intval($this->mValue);
+		if (strlen(trim($this->mValue)) > 0) {
+			$this->mValue = intval($this->mValue);
 		}
 		else {
-			$this->mValue=null;
+			$this->mValue = null;
 		}
 	}
 }
 
 class XCube_IntArrayProperty extends XCube_AbstractArrayProperty
 {
-	function fetch()
-	{
-		parent::fetch();
-		foreach(array_keys($this->mValue) as $key) {
-			if(strlen($this->mValue[$key])>0) {
-				$this->mValue[$key]=intval($this->mValue[$key]);
-			}
-			else {
-				$this->mValue[$key]=null;
-			}
-		}
-	}
+	var $mPropertyClassName = "XCube_IntProperty";
 }
 
 class XCube_FloatProperty extends XCube_AbstractProperty
@@ -313,64 +346,76 @@
 	function fetch()
 	{
 		parent::fetch();
-		$this->mValue=floatval($this->mValue);
+		if (strlen(trim($this->mValue)) > 0) {
+			$this->mValue = floatval($this->mValue);
+		}
+		else {
+			$this->mValue = null;
+		}
 	}
 }
 
 class XCube_FloatArrayProperty extends XCube_AbstractArrayProperty
 {
-	function fetch()
-	{
-		parent::fetch();
-		foreach(array_keys($this->mValue) as $key) {
-			$this->mValue[$key]=floatval($this->mValue[$key]);
-		}
-	}
+	var $mPropertyClassName = "XCube_FloatArrayProperty";
 }
 
+/**
+ *  This class shows the property of string. Check whether a request includes control
+ * code. If it does, stop own process.
+ */
 class XCube_StringProperty extends XCube_AbstractProperty
 {
 	function fetch()
 	{
 		parent::fetch();
-		if(preg_match_all("/[\\x00-\\x1f]/",$this->mValue,$matches,PREG_PATTERN_ORDER)) {
-			foreach($matches[0] as $match) {
+		if (preg_match_all("/[\\x00-\\x1f]/", $this->mValue, $matches, PREG_PATTERN_ORDER)) {
+			foreach ($matches[0] as $match) {
 				die("Get control code :" . ord($match));
 			}
 		}
 	}
+	
+	function toNumber()
+	{
+		return intval($this->mValue);
+	}
 }
 
 class XCube_StringArrayProperty extends XCube_AbstractArrayProperty
 {
-	function fetch()
-	{
-		parent::fetch();
-		foreach(array_keys($this->mValue) as $key) {
-			if(preg_match_all("/[\\x00-\\x1f]/",$this->mValue[$key],$matches,PREG_PATTERN_ORDER)) {
-				foreach($matches[0] as $match) {
-					die("Get control code :" . ord($match));
-				}
-			}
-		}
-	}
+	var $mPropertyClassName = "XCube_StringArrayProperty";
 }
 
+/**
+ *  This class shows the property of text. Check whether a request includes control
+ * code. If it does, stop own process.
+ */
 class XCube_TextProperty extends XCube_AbstractProperty
 {
 	function fetch()
 	{
 		parent::fetch();
-		$matches=array();
-		$allow_codes=array(9,10,13);
+		$matches = array();
+		$allow_codes = array(9,10,13);
 
-		if(preg_match_all("/[\\x00-\\x09]|[\\x0b-\\x0c]|[\\x0e-\\x1f]/",$this->mValue,$matches,PREG_PATTERN_ORDER)) {
-			foreach($matches[0] as $match) {
-				if(!in_array(ord($match),$allow_codes) && ord($match)<32)
+		if (preg_match_all("/[\\x00-\\x09]|[\\x0b-\\x0c]|[\\x0e-\\x1f]/", $this->mValue, $matches,PREG_PATTERN_ORDER)) {
+			foreach ($matches[0] as $match) {
+				if (!in_array(ord($match),$allow_codes) && ord($match)<32)
 					die("Get control code :" . ord($match));
 			}
 		}
 	}
+	
+	function toNumber()
+	{
+		return intval($this->mValue);
+	}
+}
+
+class XCube_TextArrayProperty extends XCube_AbstractArrayProperty
+{
+	var $mPropertyClassName = "XCube_TextArrayProperty";
 }
 
 class XCube_FileProperty extends XCube_AbstractProperty
@@ -383,11 +428,34 @@
 	
 	function fetch()
 	{
+		if (!is_object($this->mValue)) {
+			return false;
+		}
+		
 		$this->mValue->fetch();
 		if (!$this->mValue->hasUploadFile()) {
 			$this->mValue = null;
 		}
 	}
+	
+	function isNull()
+	{
+		if (!is_object($this->mValue)) {
+			return false;
+		}
+		
+		return !$this->mValue->hasUploadFile();
+	}
+	
+	function toString()
+	{
+		return null;
+	}
+	
+	function toNumber()
+	{
+		return null;
+	}
 }
 
 class XCube_ImageFileProperty extends XCube_FileProperty
@@ -417,7 +485,7 @@
 		foreach($dependsArr as $dependName){
 			$instance =& XCube_DependClassFactory::factoryClass($dependName);
 			if($instance!==null)
-				$this->mDepends[]=&$instance;
+				$this->mDepends[$dependName]=&$instance;
 
 			unset($instance);
 		}
@@ -455,17 +523,20 @@
 		$this->mVariables[$name]=$value;
 	}
 	
-	function validate($val)
+	/**
+	 * TODO This class already has form property instance.
+	 */
+	function validate(&$form)
 	{
 		if(is_array($this->mDepends) && count($this->mDepends)>0) {
-			foreach($this->mDepends as $depend) {
-				if(!$depend->check($val,$this->mVariables)) {
+			foreach($this->mDepends as $name => $depend) {
+				if(!$depend->isValid($form, $this->mVariables)) {
 					// Error
 					// NOTICE: This is temporary until we will decide the method of managing error.
 					$this->mForm->mErrorFlag=true;
 					
 					// TEST!!
-					$this->mForm->addErrorMessage($this->renderMessage($depend->getName()));
+					$this->mForm->addErrorMessage($this->renderMessage($name));
 				}
 				else {
 					// OK
@@ -479,341 +550,21 @@
 {
 	function &factoryClass($dependName)
 	{
-		$ret=null;
-
-		// or switch?
-		$class_name="XCube_".ucfirst($dependName)."Depend";
-		if(class_exists($class_name)) {
-			$ret=new $class_name();
-		}
-
-		return $ret;
-	}
-}
-
-class XCube_AbstractDepend
-{
-	var $mName="";
-	
-	function XCube_AbstractDepend()
-	{
-	}
-	
-	function check($val,$vars)
-	{
-		if(is_array($val))
-			return $this->_checkAsArray($val,$vars);
-		else
-			return $this->_check($val,$vars);
-	}
-	
-	/**
-	 @access protected
-	 */
-	function _check($val,$vars)
-	{
-		return false;
-	}
-	
-	/**
-	 @access protected
-	 */
-	function _checkAsArray($val,$vars)
-	{
-		return false;
-	}
-	
-	function getName()
-	{
-		return $this->mName;
-	}
-}
-
-class XCube_RequiredDepend extends XCube_AbstractDepend
-{
-	var $mName="required";
-	
-	function _check($val,$vars)
-	{
-		//
-		// TODO To get null or not null, we have to add a method to form property.
-		//      I will program Validator class and fetch many code from this file. (minahito)
-		//
-		if (is_object($val)) {
-			return true;
-		}
-		else {
-			return strlen($val)!=0;
-		}
-	}
-	
-	function _checkAsArray($val,$vars)
-	{
-		if(!count($val))
-			return false;
-
-		$flag = true;
-		foreach($val as $i) {
-			$flag &= (strlen($i)!=0);
-		}
-
-		return $flag;
-	}
-}
-
-class XCube_MinlengthDepend extends XCube_AbstractDepend
-{
-	var $mName="minlength";
-	
-	function _check($val,$vars)
-	{
-		if(!empty($val))
-			return strlen($val)>=$vars['minlength'];
-		else
-			return true;
-	}
-
-	function _checkAsArray($val,$vars)
-	{
-		if(!count($val))
-			return false;
-
-		$flag = true;
-		foreach($val as $i) {
-			if(!empty($i))
-				$flag&=(strlen($i)>=$vars['minlength']);
-		}
-		
-		return $flag;
-	}
-}
-
-class XCube_MaxlengthDepend extends XCube_AbstractDepend
-{
-	var $mName="maxlength";
-	
-	function _check($val,$vars)
-	{
-		if(!empty($val))
-			return strlen($val)<=$vars['maxlength'];
-		else
-			return true;
-	}
-
-	function _checkAsArray($val,$vars)
-	{
-		if(!count($val))
-			return false;
-
-		$flag = true;
-		foreach($val as $i) {
-			if(!empty($i))
-				$flag&=(strlen($i)<=$vars['maxlength']);
-		}
-
-		return $flag;
-	}
-}
-
-class XCube_MinDepend extends XCube_AbstractDepend
-{
-	var $mName="min";
-	
-	function _check($val,$vars)
-	{
-		if(!empty($val))
-			return $val>=$vars['min'];
-		else
-			return true;
-	}
-
-	function _checkAsArray($val,$vars)
-	{
-		if(!count($val))
-			return false;
-
-		$flag = true;
-		foreach($val as $i) {
-			if(!empty($i))
-				$flag&=($i>=$vars['min']);
-		}
-
-		return $flag;
-	}
-}
-
-class XCube_MaxDepend extends XCube_AbstractDepend
-{
-	var $mName="max";
-	
-	function _check($val,$vars)
-	{
-		if(!empty($val))
-			return $val<=$vars['max'];
-		else
-			return true;
-	}
-
-	function _checkAsArray($val,$vars)
-	{
-		if(!count($val))
-			return false;
-
-		$flag = true;
-		foreach($val as $i) {
-			if(!empty($i))
-				$flag&=($i<=$vars['max']);
-		}
-
-		return $flag;
-	}
-}
-
-class XCube_IntRangeDepend extends XCube_AbstractDepend
-{
-	var $mName="intRange";
-	
-	function _check($val,$vars)
-	{
-		if(!empty($val))
-			return ($val>=$vars['min'])&&($val<=$vars['max']);
-		else
-			return true;
-	}
-
-	function _checkAsArray($val,$vars)
-	{
-		if(!count($val))
-			return false;
-
-		$flag = true;
-		foreach($val as $i) {
-			if(!empty($i))
-				$flag&=(($i>=$vars['min'])&&($i<=$vars['max']));
-		}
-
-		return $flag;
-	}
-}
-
-class XCube_EmailDepend extends XCube_AbstractDepend
-{
-	var $mName = "email";
-
-	function _check($val,$vars)
-	{
-		if(!empty($val)) {
-			return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$/i", $val);
-		}
-		return true;
-	}
-
-	function _checkAsArray($val,$vars)
-	{
-		if(!count($val))
-			return false;
-
-		$flag = true;
-		foreach($val as $i) {
-			if(!empty($i)) {
-				$flag &= $this->_check($i, $vars);
-			}
-		}
-
-		return $flag;
-	}
-}
-
-class XCube_MaskDepend extends XCube_AbstractDepend
-{
-	var $mName = "mask";
-
-	function _check($val,$vars)
-	{
-		if(!empty($val)) {
-			return preg_match($vars['mask'], $val);
-		}
-		return true;
-	}
-
-	function _checkAsArray($val,$vars)
-	{
-		if(!count($val))
-			return false;
-
-		$flag = true;
-		foreach($val as $i) {
-			if(!empty($i)) {
-				$flag &= $this->_check($i, $vars);
-			}
-		}
-
-		return $flag;
-	}
-}
-
-class XCube_ExtensionDepend extends XCube_AbstractDepend
-{
-	var $mName = "extension";
-	
-	function _check($val, $vars)
-	{
-		if (!is_a($val, "XCube_FormFile")) {
-			return true;
-		}
+		static $_cache;
 		
-		$extArr = explode(",", $vars['extension']);
-		foreach ($extArr as $ext) {
-			if (strtolower($val->getExtension()) == strtolower($ext)) {
-				return true;
-			}
-		}
-		
-		return false;
-	}
-	
-	function _checkAsArray($val,$vars)
-	{
-		if(!count($val))
-			return false;
-
-		$flag = true;
-		foreach($val as $i) {
-			if(!empty($i)) {
-				$flag &= $this->_check($i, $vars);
-			}
-		}
-
-		return $flag;
-	}
-}
-
-class XCube_MaxfilesizeDepend extends XCube_AbstractDepend
-{
-	var $mName = "maxfilesize";
-	
-	function _check($val, $vars)
-	{
-		if (!is_a($val, "XCube_FormFile")) {
-			return true;
+		if (!is_array($_cache)) {
+			$_cache = array();
 		}
 		
-		return ($val->getFileSize() < $vars['maxfilesize']);
-	}
-	
-	function _checkAsArray($val,$vars)
-	{
-		if(!count($val))
-			return false;
-
-		$flag = true;
-		foreach($val as $i) {
-			if(!empty($i)) {
-				$flag &= $this->_check($i, $vars);
+		if (!isset($_cache[$dependName])) {
+			// or switch?
+			$class_name = "XCube_" . ucfirst($dependName) . "Validator";
+			if(class_exists($class_name)) {
+				$_cache[$dependName] =& new $class_name();
 			}
 		}
 
-		return $flag;
+		return $_cache[$dependName];
 	}
 }
 


xoops-cvslog メーリングリストの案内
Zurück zum Archiv-Index