From 56fee292c37e162c03fab9eeadd6a8b9ab85c251 Mon Sep 17 00:00:00 2001
From: xue <>
Date: Mon, 4 Sep 2006 19:15:47 +0000
Subject: merge from 3.0 branch till 1387
---
framework/TComponent.php | 54 +++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 47 insertions(+), 7 deletions(-)
(limited to 'framework/TComponent.php')
diff --git a/framework/TComponent.php b/framework/TComponent.php
index 21a62abd..986a0b32 100644
--- a/framework/TComponent.php
+++ b/framework/TComponent.php
@@ -443,6 +443,32 @@ class TComponent
}
}
+/**
+ * TEnumerable class.
+ * TEnumerable is the base class for all enumerable types.
+ * To define an enumerable type, extend TEnumberable and define string constants.
+ * Each constant represents an enumerable value.
+ * The constant name must be the same as the constant value.
+ * For example,
+ *
+ * class TTextAlign extends TEnumerable
+ * {
+ * const Left='Left';
+ * const Right='Right';
+ * }
+ *
+ * Then, one can use the enumerable values such as TTextAlign::Left and
+ * TTextAlign::Right.
+ *
+ * @author Qiang Xue
+ * @version $Revision: $ $Date: $
+ * @package System
+ * @since 3.0
+ */
+class TEnumerable
+{
+}
+
/**
* TPropertyValue class
*
@@ -568,19 +594,33 @@ class TPropertyValue
/**
* Converts a value to enum type.
- * This method mainly performs sanity check of a value to make sure
- * it is a valid enumeration value. Each enumeration value is a string
- * which is case-sensistive.
+ *
+ * This method checks if the value is of the specified enumerable type.
+ * A value is a valid enumerable value if it is equal to the name of a constant
+ * in the specified enumerable type (class).
+ * For more details about enumerable, see {@link TEnumerable}.
+ *
+ * For backward compatibility, this method also supports sanity
+ * check of a string value to see if it is among the given list of strings.
* @param mixed the value to be converted.
- * @param mixed array of valid enumeration values. If this is not an array,
- * the method considers its parameters are of variable length, and the second
- * till the last parameters are enumeration values.
+ * @param mixed class name of the enumerable type, or array of valid enumeration values. If this is not an array,
+ * the method considers its parameters are of variable length, and the second till the last parameters are enumeration values.
* @return string the valid enumeration value
* @throws TInvalidDataValueException if the original value is not in the string array.
*/
public static function ensureEnum($value,$enums)
{
- if(!is_array($enums))
+ static $types=array();
+ if(func_num_args()===2 && is_string($enums))
+ {
+ if(!isset($types[$enums]))
+ $types[$enums]=new ReflectionClass($enums);
+ if($types[$enums]->hasConstant($value))
+ return $value;
+ else
+ throw new TInvalidDataValueException('propertyvalue_enumvalue_invalid',$value,$enums);
+ }
+ else if(!is_array($enums))
{
$enums=func_get_args();
array_shift($enums);
--
cgit v1.2.3